CF C求hack或指明正确性/kk
  • 板块学术版
  • 楼主charleshe
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/1/10 21:51
  • 上次更新2023/10/24 04:49:21
查看原帖
CF C求hack或指明正确性/kk
477258
charleshe楼主2023/1/10 21:51

RT,赛事稀里糊涂就过了……个人感觉很玄学……

#include <iostream>
#define int long long
using namespace std;
int t,n,x;
int in_it[63];
void init(){
	in_it[0]=1;
	for(int i=1;i<=62;i++) in_it[i]=in_it[i-1]*2;
}
bool work(){
	bool c[66];
	int cnt=-1,tn=n;
	while(tn){
		c[++cnt]=tn%2;
		tn/=2;
	}
	tn=n;
	for(int i=0;i<=cnt;i++){
		if(c[i]){
			tn-=in_it[i];
			if(tn==x) return true;
		}
	}
	return false;
}
signed main(){
	init();
	cin>>t;
	while(t--){
		cin>>n>>x;
		if(n<x){
			cout<<"-1\n";
			continue;
		}
		if(n==x){
			cout<<n<<endl;
			continue;
		}
		if((n|x)!=n){
			cout<<"-1\n";
			continue;
		}
		if(!work()){
			cout<<"-1\n";
			continue;
		}
		int tmp=n^x;
		for(int i=0;;i++){
			if(in_it[i]>tmp){
				if(((n+in_it[i]-tmp)&n)!=x) cout<<"-1\n";
				else cout<<n+in_it[i]-tmp<<endl;
				break;
			}
		}
	}
	return 0;
}
2023/1/10 21:51
加载中...