洛谷 - P10118
Chthohepta · · 个人记录
题意
给定非负整数
你需要求出
其中
思路
可以从第三个点入手。
对于每一个
可以将
剩下的情况我们使二进制中最高位的
剩下的
我们发现最终答案的剩下的一都会被
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int T;
ll m, a, b;
ll w, cnt;
bool f;
ll po(ll a, ll b, ll mod){
ll ans = 1;
while(b){
if(b & 1){
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> T >> m;
while(T --){
cin >> a >> b;
a -= (b << 1);
w = 0;
cnt = 0;
for(int i = 0; i < 64; i ++){
if(a >> i & 1){
cnt ++;
w = i;
}
}
if(a <= 0 || (a & b) != 0){
cout << 0 << '\n';
continue;
}
cout << po(2, w, m) % m * po(2, cnt - 1, m) % m << '\n';
}
return 0;
}