Description xay loves or. He gives you x and s, you need to calculate how many positive integer y satisfy x or y = s. Input The first line contains two positive integers x and s . It’s guaranteed that 1 ≤ x, s < 2 31 . Output Print the number of y satisfy x or y = s .
Example
standard input
2 5
standard output
0
题意:
给出x和s求y公式 x|y=s
就是先把s和x转化为二进制
然后再一一比较
比如
x | y -> s
1 无 0
1 0,1 1
0 1 1
0 0 0
当x==1 s==1时
有两个情况
#include <iostream>
#include <cstring>
#include <algorithm>
#include<cmath>
using namespace std;
int x,s;
int main()
{
cin>>x>>s;
int tem=1,flag=0;
if(x>s) flag=1;
else
{
int k=log(s)/log(2);
for(int i=k;i>=0;i--)
{
int a=(s>>i&1);
int b=(x>>i&1);
if(a==1&&b==1) tem*=2;
else if(a==0&&b==1) flag=1;
}
}
if(x==s) tem-=1;
if(flag) cout<<0<<endl;
else cout<<tem<<endl;
return 0;
}
看到有人直接暴力过了
太神奇了
#include<iostream>
using namespace std;
int main()
{
int x, s, ans = 0;
cin>>x>>s;
for(int i=1;i<=s;i++)
{
if((x|i)==s) ans++;
}
cout<<ans<<endl;
return 0;
}
2^31次方都2e9了不知道为啥能过