#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 LL;
typedef pair<LL, bool> PII;
const int N = 62;
int T, P;
ll A, B, res;
int a[N], b[N];
LL f[N][2];
bool can[N][2];
bool st[N][2];
inline LL F(const LL& x) { // 手写个绝对值
return x < 0 ? -x : x;
}
inline PII dfs(const int& u, const int& k) {
if (u == N) return {0, 1};
if (st[u][k]) return {f[u][k], can[u][k]};
st[u][k] = true;
if (!k) {
if (a[u] && !b[u]) {
const PII& t1 = dfs(u + 1, 1);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = t1.first, can[u][k] = 1};
}
else if (!a[u] && b[u]) {
const PII& t1 = dfs(u + 1, 0);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = F(t1.first + (1ll << u)) + F(t1.first - (1ll << u)), can[u][k] = 1};
}
else if (!a[u] && !b[u]) {
const PII& t1 = dfs(u + 1, 0);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = t1.first, can[u][k] = 1};
}
}
else {
if (a[u] && b[u]) {
const PII& t1 = dfs(u + 1, 1);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = t1.first, can[u][k] = 1};
}
else if (!a[u] && !b[u]) {
const PII& t1 = dfs(u + 1, 1);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = F(t1.first + (1ll << u)) + F(t1.first - (1ll << u)), can[u][k] = 1};
}
else if (!a[u] && b[u]) {
const PII& t1 = dfs(u + 1, 0);
if (!t1.second) return {f[u][k] = 0, can[u][k] = 0};
return {f[u][k] = t1.first, can[u][k] = 1};
}
}
return {f[u][k] = 0, can[u][k] = 0};
}
ll solve() {
scanf("%lld%lld", &A, &B);
if (A <= 8e5 && B <= 8e5) {
res = 0;
for (int a = 0; a <= A / 2; ++ a ) {
int b = A - a;
(res += ((a & b) == B) * (b - a) % P) %= P;
}
return res % P;
}
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
memset(st, 0, sizeof st);
memset(can, 1, sizeof can);
for (int i = 0; i < N; ++ i ) {
b[i] = A >> i & 1;
a[i] = B >> i & 1;
}
PII res = dfs(0, 0);
if (res.second) return (res.first / 2) % P;
return 0;
}
signed main() {
scanf("%d%d", &T, &P);
while (T -- ) printf("%lld\n", solve());
return 0;
}剪贴板
剪贴板 pjt8rhnb
剪贴板信息
剪贴板信息
- 收录
- 更新