D. Zookeeper and The Infinite Zoo(Codeforces Global Round 13)

D - Zookeeper and The Infinite Zoo

https://codeforces.ml/contest/1491/problem/D

思路

点u到v存在通路,则必有v = u + d,u & d = u,由位运算性质可知d(二进制)中1的数量应小于u中且位置与u中相同,由此u与d相加相当于u中某些1左移,这些1即是d中存在的1.
由此直接按位判断v和u的1的数量即可,v中1的数量不大于u且相应位置在u的左边或不变,再根据v>=u判断即可。

Code

#include<bits/stdc++.h>
#define IO  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define req(i,a,b) for(int i=a;i>=b;i--)
using namespace std; 
 
const int inf=0x3f3f3f3f;
typedef long long ll; 
const int N = 1e5+7;
const ll mod = 1e9+7;       

int main(){ 
    IO;
    int t=1;
    cin>>t;
    while(t--){  
    	ll u,v;
    	cin>>u>>v;
    	if(u>v){
    		puts("NO");
    		continue;
    	}
    	int flag=1;
    	int x=0,y=0;
    	for (ll i = 0; i < 31; ++i)
    	{
    		if(u>>i & 1ll)x++;
    		if(v>>i & 1ll)y++;
    		if(y){
    			if(!x){
    				flag=0;break;
    			}
    			x--;
    			y--;
    		}
    	}
    	if(flag)puts("YES");
    	else puts("NO");
    }
    return 0;
}
posted @ 2021-03-03 22:10  !^^!  阅读(85)  评论(0)    收藏  举报