https://codeforces.com/problemset/gymProblem/102942/D
D. XOR Game
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice and Bob are playing a game.
First Alice will choose two integers a and b (a≤b). Then Bob will take two integers c and d (1≤c≤a,1≤d≤b).
Score of Alice will be (a⊕b) and score of Bob will be (c⊕d), where ⊕ denotes the bitwise xor operation.
If the score of Bob is strictly greater than the score of Alice then Bob wins, otherwise Alice wins.
You know the two integers a and b that Alice chose. Determine if it is possible to choose some c and d for Bob to win.
Input
The first line contains an integer t (1≤t≤102) — the number of test cases in the input.
Then t test cases follow.
Each test case is a line containing two integers a and b (2≤a,b≤106,a≤b).
Output
For each test case, If Bob has a winning strategy print Yes, otherwise No.
You can print each character in any case.
Example
inputCopy
3
2 2
2 4
6 10
outputCopy
Yes
No
Yes
Note
In the first test, the score of Alice is 0. Bob can choose c=1 and d=2 to get a score of 3 to win.
In the second test, we can show that whatever Bob chooses, he will always lose.
二进制异或,找到规律就很简单:结果为0的有机会,改后不会更大的就可以
#include<stdio.h>
#include<string.h>
#define min(a,b) a<b?a:b
#define N 30
int a0[N],b0[N],sa0[N],c0[N],d0[N];
void put(int x,int*x0,int &i){
for(i=0;x;i++){
x0[i]=x%2;
x/=2;
}
}
int change(int y0[],int x0[],int i,int cnt){
memcpy(y0,x0,sizeof(int)*cnt);//注意x0字节数为8
y0[i]^=1;
int res=0;
for(int j=cnt-1;j>=0;j--){
res*=2;
res+=y0[j];
}
return res;
}
int main(){
int t;
scanf("%d",&t);
int a,b,c,d;
while(t--){
memset(a0,0,sizeof(a0));
memset(b0,0,sizeof(b0));
memset(sa0,0,sizeof(sa0));
scanf("%d%d",&a,&b);
int sa=a^b;
int cnta,cntb,cntsa;
put(a,a0,cnta);
put(b,b0,cntb);
put(sa,sa0,cntsa);
int cnt=min(cnta,cntb);
int flag=0;
for(int i=0;i<cnt;i++){
if(sa0[i]==0){
c=change(c0,a0,i,cnta);
d=change(d0,b0,i,cntb);
if(c<=a||d<=b){
printf("Yes\n");
flag=1;
break;
}
}
}
if(flag==0){
printf("No\n");
}
}
return 0;
}
/*
010
100
110
011<=010或101<=100,可以
否则,继续
*/
复习:
5/19
//发现:若有一位双1,即可改大,不然无法改大
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
FILE*fp=freopen("text.in","r",stdin);
#endif
int main(){
int t;
scanf("%d",&t);
while(t--){
int a,b;
bitset<30>ba,bb;
scanf("%d%d",&a,&b);
ba=a;
bb=b;
bool flag=false;
for(int i=0;i<30;i++){
if(ba.test(i)&&bb.test(i)){//发现:若有一位双1,即可改大,不然无法改大
flag=true;printf("Yes\n");break;
}
}
if(!flag)printf("No\n");
}
return 0;
}