ACM Changchun 2015 J. Chip Factory
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces nn chips today, the i-th chip produced this day has a serial number si.
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
\display maxi,j,k(si+sj)⊕sk
whichi,j,k are three different integers between 1 and n. And \oplus⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input Format
The first line of input contains an integer T indicating the total number of test cases.
The first line of each test case is an integer n, indicating the number of chips produced today.
The next line has nn integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
• There are at most 10 testcases with n>100
Output Format
For each test case, please output an integer indicating the checksum number in a line.
样例输入
2 3 1 2 3 3 100 200 300
样例输出
6 400
题目来源
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <cstdlib> 9 #include <iomanip> 10 #include <cmath> 11 #include <cassert> 12 #include <ctime> 13 #include <map> 14 #include <set> 15 using namespace std; 16 #define ull unsigned long long 17 #define ll long long 18 #define N 30009 19 int num[N],tree[N][2]; 20 int t,n,a[1100]; 21 int pos; 22 //01字典树 23 void insert(int x,int rt) 24 { 25 for(int i=30;i>=0;i--) 26 { 27 int y=0; 28 if(x>>i&1) y=1; 29 if(!tree[rt][y]) tree[rt][y]=pos++; 30 rt=tree[rt][y]; 31 num[rt]++; 32 } 33 } 34 void dele(int x,int rt) 35 { 36 for(int i=30;i>=0;i--) 37 { 38 int y=0; 39 if(x>>i&1) y=1; 40 rt=tree[rt][y]; 41 num[rt]--;//tree[rt][y] 还在,并不是真正的删除 42 } 43 } 44 int solve(int x,int rt) 45 { 46 int ret=0; 47 /* 48 00001010 49 10100100是倒着来的 50 那么只要字典树里首位有1,就不可能找到10100100 51 */ 52 for(int i=30;i>=0;i--)//一定要倒过来,因为贪心,高位大,最终的结果才大 53 { 54 int y=0; 55 if(x>>i&1) y=1; 56 if(tree[rt][y^1]&&num[tree[rt][y^1]])//num[tree[rt][y^1]]才有意义 { 57 rt=tree[rt][y^1]; 58 ret+=(1<<i);//该位的异或为1 59 } 60 else{ 61 rt=tree[rt][y]; 62 } 63 } 64 return ret; 65 } 66 int main() 67 { 68 scanf("%d",&t); 69 while(t--) 70 { 71 for(int i=0;i<N;i++) 72 { 73 num[i]=0; 74 for(int j=0;j<=1;j++) 75 { 76 tree[i][j]=0; 77 } 78 } 79 pos=1; 80 scanf("%d",&n); 81 for(int i=0;i<n;i++){ 82 scanf("%d",&a[i]); 83 insert(a[i],0); 84 } 85 int ans=0; 86 //每次要删除a[i],a[j] 因为i!=j!=k 87 //当然每次查询后,还要再次插入字典树 88 for(int i=0;i<n;i++) 89 { dele(a[i],0); 90 for(int j=i+1;j<n;j++) 91 { 92 dele(a[j],0); 93 ans=max(ans,solve(a[i]+a[j],0) ); 94 insert(a[j],0); 95 } 96 insert(a[i],0); 97 } 98 printf("%d\n",ans); 99 } 100 return 0; 101 }
1 //9s 的暴力解法 2 #define N 1009 3 int t,n,a[N]; 4 int ans; 5 int solve(int x,int y,int z) 6 { 7 return (x+y)^z; 8 } 9 int main() 10 { 11 scanf("%d",&t); 12 while(t--) 13 { 14 scanf("%d",&n); 15 for(int i=0;i<n;i++) 16 { 17 scanf("%d",&a[i]); 18 } 19 int ans=0; 20 for(int i=0;i<n;i++) 21 { 22 for(int j=i+1;j<n;j++) 23 { 24 for(int k=j+1;k<n;k++) 25 { 26 ans=max(ans,solve(a[i],a[j],a[k]) ); 27 ans=max(ans,solve(a[i],a[k],a[j]) ); 28 ans=max(ans,solve(a[k],a[j],a[i]) ); 29 } 30 } 31 } 32 printf("%d\n",ans); 33 } 34 return 0; 35 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现