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 @   !^^!  阅读(79)  评论(0)    收藏  举报
编辑推荐:
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
阅读排行:
· .net clr 8年才修复的BUG,你让我损失太多了
· 一个神奇的JS代码,让浏览器在新的空白标签页运行我们 HTML 代码(createObjectURL
· 做Docx预览,一定要做这个神库!!
· 一个开源的 Blazor 跨平台入门级实战项目
· Hangfire Redis 实现秒级定时任务、使用 CQRS 实现动态执行代码
点击右上角即可分享
微信分享提示