跳到正文

题解

题解:CF627A XOR Equation

Kqixyurol约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年2月18日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟
获赞
1
题目
CF627A

版本

  1. v1当前

Solution

考虑无解的情况:

a+b=2×(a and b)+a xor ba + b = 2 \times (a \ \rm and \ b) + a \ \rm xor \ b s=2×(a and b)+xs = 2 \times (a \ \rm and \ b) + x a and b=s−x2a \ \rm and \ b = \frac{s - x}{2}
  1. 如果 s−xs - x 为奇数,肯定答案为 00。
  2. 显然如果 s<xs < x,答案为 00。
  3. 已知 a xor ba \ \rm xor \ b 和 a and ba \ \rm and \ b 以后,如果说相同二进制位上都是 11,说明无解。原因是 a xor ba \ \rm xor \ b 中的 11 表示满足答案的 (a,b)(a, b) 这一位二进制位是不同的,而 a and ba \ \rm and \ b 中的 11 表示满足答案的 (a,b)(a, b) 这一位二进制位的数是相同且为 11,明显不可能有答案满足。

如果说有解的话,那么在 a xor ba \ \rm xor \ b 中每一个 11 的位置都可以让 (a,b)(a, b) 随意挑选 00 和 11,因此根据乘法原理,答案为 2cnt2 ^ {cnt},cntcnt 是 xx 二进制中的 11 的个数。

  • 注意:如果 s=xs = x,说明前面记录的答案中肯定包含了 a=0a = 0 和 b=0b = 0 这 22 种情况(其实就是 a and b=0a \ \rm and \ b = 0,那么 a,ba, b 有可能分到全 00 的情况)。

Code

C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using Int = __int128;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rnd (time(nullptr));
ll s, x, ans = 1;
ll count (ll val) {
    ll cnt = 0;
    for (; val; val -= val & -val, cnt ++);
    return cnt;
}
int main (int argc, char *argv[]) {
    *cin.tie(nullptr) << fixed << setprecision (20);
    cout.tie(nullptr) -> ios_base::sync_with_stdio (false);
    
    cin >> s >> x;
	if ((s - x) % 2 || s < x || x & s - x >> 1) return cout << "0\n", 0;
	ans = 1ll << count (x);
    if (s == x) ans -= 2;
    cout << ans << '\n';
    return 0;
}
原文