codeforces 1491D - Zookeeper and The Infinite Zoo

题意: 1 , 2 , 3 , 4 , … 1,2,3,4, \dots 1,2,3,4,的无限数列, 当满足 u & v = v u \& v = v u&v=v 由一个数u可以向u+v连一条有向边 ,1e6的询问能否从x到y

就很懵啊,感觉自己对各种位运算不敏感但偏偏最近全是位运算hh

按照范围猜到是按位判断nlogn但是不会判…

稍微画了下特别注意了 2 n 2^n 2n ,但是是多想了(。

参考

官方的hint也很循序渐进

在这里插入图片描述

思路: 问能否从x到y,y一定大于等于x,

一个数(二进制)的所有1的位置都可以向左移动,但是不能够向右移动。u和v(二进制)的1从右向左进行一一对应,如果u中的所有1所对应的v中的1,全部都大于u中对应的1的话,那么u可以到v。

#include <bits/stdc++.h>
using namespace std;

bool judge(int x, int y) {
    
    
	int cntx = 0, cnty = 0;
	for(int i = 0; i <= 31; ++ i) {
    
    
		if(x >> i & 1) ++ cntx;
		if(y >> i & 1) ++ cnty;
		if(cnty) {
    
    
			-- cntx, --cnty;
			if(cntx < 0) return 0;
		}
	}
	return 1;
}

int main() {
    
    
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	 
	int T;
	cin >> T;
	while(T--) {
    
    
		int x, y;
		cin >> x >> y;
		if(x <= y && judge(x, y)) cout << "YES\n";
		else cout << "NO\n"; 
	}
} 

猜你喜欢

转载自blog.csdn.net/qq_39602052/article/details/114285299
zoo