1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream>
#include <vector>
#include <bitset> #include <utility> #include <cmath>
using namespace std;
const int N=65; unsigned long long a,m,t; typedef unsigned long long ull; typedef long long ll;
bitset<N> binary_fact(ull a){ bitset<65> ret; for(ull i=0;a;a>>=1,i++){ ret[i]=a&1; } return ret; }
ull lpow(ull a,ull b,ull p){ ull ans=1; for(ull i=1;b;b>>=1,i*=2){ ans*=pow(a,(b&1)*i); ans=ans%m; } return ans; }
vector<int> alyze(const bitset<N>& a,const bitset<N>& b){ vector<int> x; for(int i=0;i<N;i++){ if(a[i]^b[i])x.push_back(i); } if(!x.empty())x.pop_back(); return x; }
ull cau(bitset<N> a,bitset<N> b,vector<int> al){ for(auto x:al){ a[x]=b[x]=0; } ull x=a.to_ullong(),y=b.to_ullong(); ull ans=((max(x,y)-min(x,y))%m)*(lpow(2,al.size(),m)); return ans%m; }
pair<ull,ull> spe_sol(bitset<N> a,bitset<N> b){ ull x=0,y=0; for(int i=0;i<N;i++){ if(b[i]==1)x+=(1ull<<i),y+=(1ull<<i); } if(x+y>a.to_ullong())return make_pair(0,0); auto t=a.to_ullong()-x-y; if(((x+t)&y)!=b.to_ullong())return make_pair(0,0); return make_pair(x+t,y); }
int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr),cout.tie(nullptr); cin>>t>>m; for(ull i=0;i<t;i++){ ull a,b; cin>>a>>b; auto z=spe_sol(binary_fact(a),binary_fact(b)); bitset<N> p=z.first,q=z.second; vector<int> k=alyze(p,q); cout<<cau(p,q,k)<<endl; } return 0;
}
|