XOR equation
The positive integers A and B have a sum of s and a bitwise XOR of x. How many possible values is there for the ordered pair (a, b)? Input
The first line of the input contains integers s and x (2≤s≤1012, 0≤x≤1012), the sum and bitwise XOR of the PAI R of positive integers, respectively. Output
Print A single integer, the number of solutions to the given conditions. If no solutions exist, print 0. Examples input Copy
9 5
Output
4
Input Copy
3 3
Output
2
Input Copy
5 2
Output
0
Note
In the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2).
In the second sample, the only solutions is (1, 2) and (2, 1).
Test instructions: a+b=s,a^b=x, ask how many (positive integers) there are such a B
Ideas: ① deep Search, x binary a bit for 1 description of a, B corresponds to a bit different, x a bit is 0, indicating that a, B corresponds to 1 or 0 at the same time. Deep search can not be satisfied with the corresponding bits of s equal. Because there is a large amount of non-understanding, it does not time out.
②a+b = (a&b) *2+a^b so a&b = (s-x)/2, so if S-x is less than 0 or odd, there must be no such a, b
Then enumerate x and (a&b) each bit can, if (a&b) i = = 1,xi = = 1, there must be no such a, B (definitely)
if (a&b) i = = 0,xi = = 0, there is only one case on the B-bit that is 0
if (a&b) i = = 0,xi = = 1, corresponds to a b bit on two occasions 0,1 and 1,0
Deep Search Code:
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack > #include <cstring> #include <string> #include <vector> #include <cmath> #include <map
> #define MEM (A, B) memset (A,b,sizeof (a)) #define MOD 1000000007 using namespace std;
typedef long Long LL;
const int MAXN = 5E5+5;
Const double ESP = 1e-7;
const int FF = 0X3F3F3F3F;
Map<int,int>::iterator it;
ll X,s;
int Max_len;
int a[520],b[520];
ll DFS (int pos,int jw) {if (pos > MAX_LEN&&JW = = 0)//If deep search and carry is 0, indicating that this result is feasible return 1;
ll tmp = 0; if (b[pos] = = 0)//If this bit a, B, or 0, then A = B. = 0 or a = b = 1 {if ((0+0+jw)%2 = = A[pos])//Determine if the sum is equal to S, the same as tmp+= Dfs (POS
+1,JW/2);
if ((1+1+jw)%2 = = A[pos]) tmp+= dfs (pos+1, (1+1+JW)/2); } else {if ((1+0+jw)%2 = = A[pos]) tmp+= dfs (pos+1, (1+JW)/2) *2;//This bit on a here is 0,b this bit is 1, or a This bit is 1,b this bit is 0.
Two cases, so * *} return TMP;
} int main () {cin>>s>>x;
ll ans = s = = x?-2:0; int cnt = 0;
while (s)//gets each bit of the binary of s {a[++cnt] = s%2;
S/= 2;
} Max_len = max (max_len,cnt);
CNT = 0;
while (x)//gets each bit of the binary of x {b[++cnt] = x%2;
X/= 2;
} Max_len = max (max_len,cnt);//record both the longest value ans + = DFS (1,0);
cout<<ans<<endl;
return 0; }
Number theory code:
A+b = (a&b) *2+a^b
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#define MEM (b) memset (A, B, sizeof (a))
#define MOD 1000000007
using namespace std;
typedef long long LL;
const int MAXN = 5e5+5;
Const double ESP = 1e-7;
const int FF = 0X3F3F3F3F;
Map<int,int>::iterator it;
ll S,x;
int main ()
{
cin>>s>>x;
if (s-x< 0| | (s-x) &1)
{
cout<<0<<endl;
return 0;
}
ll ans = 1;
ll and = (s-x) >>1;
for (int i = 0;i<= 40;i++)
{
int tmp1 = (and>>i) &1;
int tmp2 = (x>>i) &1;
if (TMP1&&TMP2)
{
cout<<0<<endl;
return 0;
}
if (Tmp1 = = 0&&TMP2 = = 1)
ans*= 2;
}
if (s = = x)
ans-= 2;
cout<<ans<<endl;
return 0;
}