[CF2160C]Reverse XOR题解

time limit per test

2 seconds

memory limit per test

256 megabytes

Given a positive integer x, let f(x) be the positive integer formed by reversing the binary representation of x without leading zeroes. For example, if x=12=11002, then f(x)=00112=3.

You are given an integer n. Please determine if there exists a positive integer x such that x⊕f(x)=n∗.

∗Here, ⊕ denotes the bitwise XOR operation.

有道 翻译

给定一个正的整数 x ,设 f(x) 是通过反转 x 的二进制表示而不带前导零而形成的正整数。例如,如果 x=12=11002 ,那么 f(x)=00112=3 。

你得到一个整数 n 。请确定是否存在 x⊕f(x)=n ∗ 的正整数 x 。

∗ 这里, ⊕ 表示按位异或操作

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains an integer n (0≤n<230).

有道 翻译

输入** **

每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。下面是测试用例的描述。

每个测试用例的第一行包含一个整数 n ( 0≤n<230 )。

Output

For each test case, output YES if there exists a positive integer x such that x⊕f(x)=n, and NO otherwise.

You can output the answer in any case. For example, the strings "yEs", "yes", and "Yes" are also recognized as positive responses.

有道 翻译

** **输出

对于每个测试用例,如果存在 x⊕f(x)=n 对应的正整数 x ,则输出YES,否则输出NO。

在任何情况下都可以输出答案。例如,字符串“yEs”、“yEs”和“yEs”也被识别为积极响应。

Example

Input

Copy

 

6

0

3

6

8

10

11

Output

Copy

 

YES

YES

YES

NO

YES

NO

Note

In the first case, when x=1, f(x)=1, and x⊕f(x)=0. Thus, the answer is YES.

In the second case, when x=2, f(x)=1, and x⊕f(x)=3. Thus, the answer is YES.

In the fourth test case, we can show there is no x that satisfies x⊕f(x)=8, so the answer is NO.

有道 翻译

注意

在第一种情况下,当 x=1 , f(x)=1 和 x⊕f(x)=0 。因此,答案是肯定的。

在第二种情况下,当 x=2 , f(x)=1 和 x⊕f(x)=3 。因此,答案是肯定的。

在第四个测试用例中,我们可以证明没有 x 满足 x⊕f(x)=8 ,因此答案是no。

思路

找规律,可以发现,该数二进制回文,特判出解。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,a[100005],b[100005],c[100005],lk=0,kl=0;
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		if(n==0){
			cout<<"YES"<<endl;
		}
		else{
			while(n!=0&&n%2==0){
				n/=2;
			}
			//cout<<n<<endl;
			if(n==0){
				cout<<"NO"<<endl;
			}
			else{
				lk=0;
				while(n!=0){
					a[++lk]=n%2;
					n/=2;
				}
				kl=0;
				for(int i=1;i<=lk;i++){
					//cout<<a[i]<<" ";
					if(a[i]!=a[lk-i+1]){
						kl=1;
						break;
					}
				}
				if(kl==0&&(a[(lk+1)/2]==0||lk%2==0)){
					cout<<"YES"<<endl;
				}
				else{
					cout<<"NO"<<endl;
				}
			}			
		}

	}
    return 0;
}

posted @ 2025-10-13 08:01  bz02_2023f2  阅读(2)  评论(0)    收藏  举报  来源