Codeforces 627 A. XOR Equation (数学)

题目链接:http://codeforces.com/problemset/problem/627/A

题意:

        告诉你s 和 x,a + b = s    a xor b = x   a, b > 0。 让你求符合条件的a b有多少对

思路:

        a + b = s , a ^ b = x   ==>   s - x = (a & b) * 2 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 int a[100], b[100];
 5 int main()
 6 {
 7     LL s, x;
 8     cin >> s >> x;
 9     LL y = s - x;
10     if(y % 2 || x > s) {
11         cout << 0 << endl;
12     } else {
13         y /= 2;
14         int pos1 = 0, pos2 = 0, cnt = 0, fuck = y ? 1: 0;
15         for( ; x; ++pos1, x >>= 1) {
16             if(x & 1) {
17                 ++cnt;
18                 a[pos1] = 1;
19             }
20         }
21         for( ; y; ++pos2, y >>= 1) {
22             if(y & 1)
23                 b[pos2] = 1;
24         }
25         int ok = 1;
26         for(int i = 0; i < max(pos1, pos2); ++i) {
27             if(a[i] && b[i]) {
28                 ok = 0;
29                 break;
30             }
31         }
32         if(ok) {
33             if(fuck) {
34                 cout << (1LL << ((LL)cnt)) << endl;
35             } else {
36                 cout << (1LL << ((LL)cnt)) - 2 << endl;
37             }
38         } else {
39             cout << 0 << endl;
40         }
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/Recoder/p/5939191.html