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

题意:

一个有向图,建边方式为:如果 u & v = v u \& v=v u&v=v,则 u u u u + v u+v u+v 有一条有向边,现在给出 q q q个询问,查询 x x x能否到 y y y

题解:

很明显, y y y必须要比 x x x大。再来看, u & v = v u \& v=v u&v=v,可以推出,在二进制表示下, v v v u u u 的子集 ,那么 v + u v+u v+u 必然会把 u u u 中部分 1 1 1 的位置往后移动,那么只需判断 y y y 的二进制下的 1 1 1的位置是否 ≥ \geq x x x中对应的 1 1 1 的位置。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod=1e9+7;
const int MAXN=5e2+5;
const int inf=0x3f3f3f3f;
bool check(int u,int v)
{
    int cnt1=0,cnt2=0;
    for(int i=0;i<=30;i++)
    {
        if((u>>i)&1) cnt1++;
        if((v>>i)&1) cnt2++;
        if(cnt2&&!cnt1)
        {
            return false;
        }
        else if(cnt2&&cnt1)
        {
            cnt1--,cnt2--;    
        }
    }
    return true;
}
int main()
{
//========================================
#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
//=========================================
    int q;
    cin>>q;
    while(q--)
    {
        int u,v;
        cin>>u>>v;
        if(u>v) cout<<"NO"<<endl;
        else if(check(u,v)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

posted @ 2021-03-30 19:17  TheBestQAQ  阅读(44)  评论(0)    收藏  举报