B. Dreamoon Likes Sequences

Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:

You are given two integers d,md,m, find the number of arrays aa, satisfying the following constraints:

  • The length of aa is nn, n1n≥1
  • 1a1<a2<<and1≤a1<a2<⋯<an≤d
  • Define an array bb of length nn as follows: b1=a1b1=a1, i>1,bi=bi1ai∀i>1,bi=bi−1⊕ai, where ⊕ is the bitwise exclusive-or (xor). After constructing an array bb, the constraint b1<b2<<bn1<bnb1<b2<⋯<bn−1<bn should hold.

Since the number of possible arrays may be too large, you need to find the answer modulo mm.

Input

The first line contains an integer tt (1t1001≤t≤100) denoting the number of test cases in the input.

Each of the next tt lines contains two integers d,md,m (1d,m1091≤d,m≤109).

Note that mm is not necessary the prime!

Output

For each test case, print the number of arrays aa, satisfying all given constrains, modulo mm.

Example
input
Copy
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
output
Copy
1
3
5
11
17
23
29
59
89
0

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 998244353;
const int N = 2e5+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m%n);
}

int main()
{
    vector<ll> a(33,1);
    for (int i = 1; i <= 32; i++)
    {
        a[i] = a[i - 1] * 2;
    }
    int T;
    cin >> T;
    while (T--)
    {
        ll d, m;
        cin >> d >> m;
        ll n=upper_bound(a.begin(), a.end(), d) - a.begin()-1;
        ll res=1;
        for (int i = 0; i < n; i++)
        {
            res *= (a[i] + 1)%m;
            res%=m;
        }
        res = res*(d - a[n] + 2) - 1;
        res = (res+m) % m;
        cout << res << endl;
    }
    return 0;
}

 

 

posted @ 2020-05-12 00:54  DeaL57  阅读(146)  评论(0编辑  收藏  举报