Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! B. Johnny and His Hobbies(暴力/桶)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.
There is a set SS containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer kk and replace each element ss of the set SS with s⊕ks⊕k (⊕⊕ denotes the exclusive or operation).
Help him choose such kk that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set {1,2,3}{1,2,3} equals to set {2,1,3}{2,1,3} .
Formally, find the smallest positive integer kk such that {s⊕k|s∈S}=S{s⊕k|s∈S}=S or report that there is no such number.
For example, if S={1,3,4}S={1,3,4} and k=2k=2 , new set will be equal to {3,1,6}{3,1,6} . If S={0,1,2,3}S={0,1,2,3} and k=1k=1 , after playing set will stay the same.
In the first line of input, there is a single integer tt (1≤t≤10241≤t≤1024 ), the number of test cases. In the next lines, tt test cases follow. Each of them consists of two lines.
In the first line there is a single integer nn (1≤n≤10241≤n≤1024 ) denoting the number of elements in set SS . Second line consists of nn distinct integers sisi (0≤si<10240≤si<1024 ), elements of SS .
It is guaranteed that the sum of nn over all test cases will not exceed 10241024 .
Print tt lines; ii -th line should contain the answer to the ii -th test case, the minimal positive integer kk satisfying the conditions or −1−1 if no such kk exists.
6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023
1 4 2 -1 -1 1023
看到数据范围,没啥好说的,就暴力枚举+桶排判断。
#include <bits/stdc++.h> using namespace std; int a[1027],n; int main() { int t; cin>>t; while(t--) { cin>>n; int i,k,mmax=0; bool flag=0,v1[1030],v2[1030]; for(i=0;i<=1024;i++) v1[i]=v2[i]=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); v1[a[i]]=1; mmax=max(mmax,a[i]); } for(k=1;k<=mmax;k++) { for(i=0;i<=1024;i++) v2[i]=0; for(i=1;i<=n;i++) { v2[k^a[i]]=1; } bool pd=1; for(i=1;i<=mmax;i++) { cout<<v1[i]<<' '<<v2[i]<<endl; if(v1[i]!=v2[i]) { pd=0; break; } } if(pd) { flag=1; cout<<k<<endl; break; } } if(!flag) { cout<<-1<<endl; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!