【51nod】位移运算【位运算】
·

思路:
我们发现只要b去掉末尾的0后是a的子串就是yes
考虑a&b,如果等于b,那么b上有1的位置a上也是1
但是有特例
所以我们用a^b来判断,如果a xor b 的最高位1比b的最高位1大,那么就是yes
codecodecode
#include<iostream>
#include<cstdio>
using namespace std;
int t;
int lowbit(int x)
{
return x&(-x);
}
int main()
{
scanf("%d", &t);
while(t--)
{
int a, b;
scanf("%d%d", &a, &b);
if(!b)
{
printf("Yes\n");
continue;
}
while(!(b&1)) b>>=1;
int flag=0;
for(; a>=b; a>>=1)
if(((a&b)==b)&&(((a^b)==0)||lowbit(a^b)>b))
{
flag=1;
break;
}
if(flag)
printf("Yes\n");
else printf("No\n");
}
return 0;
}
更多推荐
所有评论(0)