TOJ 3176 Challenge from XOR
2014-02-25 22:51
381 查看
Description
Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator that returns one value if one, but not both, of its operands is one. It is also called Exclusive-OR. Now Mr. AngelClover wonders if it is possible to pick up some numbers from a certain number set, such that the result of Exclusive-OR is zero. And he wants the numbers to be as few as possible but there should be at least one.
Can you help him?
Input
The first line is a positive integer N, which means the number of the number set. And there are N lines followed, in each there is a single positive integer, which is the number in the set. Each number in the set is an integer between 0 and 2^16 inclusive. 0< N <=30. (Please use standard input, and don’t read or write files.)
Output
There is only one line in the output, which is the smallest number as described above. If Mr. AngelClover can NOT find such numbers after XOR is zero, output -1. (Please use standard output, and don’t read or write files.)
Sample Input
Sample Output
Hint
Numbers which are picked up are 1, 2, 3.
For 40% input data cases, N <= 15.
Source
NKPC5
题目意思就是选几个数进行异或运算结果为0的最小个数 。一开始用觉得数据不大,用深搜果断超时。
后来想想觉得应该让个数最小深搜会产生大量没用的结果而且要全部遍历,改用广搜。
Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator that returns one value if one, but not both, of its operands is one. It is also called Exclusive-OR. Now Mr. AngelClover wonders if it is possible to pick up some numbers from a certain number set, such that the result of Exclusive-OR is zero. And he wants the numbers to be as few as possible but there should be at least one.
Can you help him?
Input
The first line is a positive integer N, which means the number of the number set. And there are N lines followed, in each there is a single positive integer, which is the number in the set. Each number in the set is an integer between 0 and 2^16 inclusive. 0< N <=30. (Please use standard input, and don’t read or write files.)
Output
There is only one line in the output, which is the smallest number as described above. If Mr. AngelClover can NOT find such numbers after XOR is zero, output -1. (Please use standard output, and don’t read or write files.)
Sample Input
4 1 2 3 4
Sample Output
3
Hint
Numbers which are picked up are 1, 2, 3.
For 40% input data cases, N <= 15.
Source
NKPC5
题目意思就是选几个数进行异或运算结果为0的最小个数 。一开始用觉得数据不大,用深搜果断超时。
后来想想觉得应该让个数最小深搜会产生大量没用的结果而且要全部遍历,改用广搜。
#include <stdio.h> #include <iostream> #include <queue> using namespace std; int n; int a[40]; int ans; struct Node{ int val; int step; int bs; }; int bfs(){ queue<Node> Q; Node n1; n1.val=0; n1.step=0; n1.bs=0; Q.push(n1); while(!Q.empty()){ Node now=Q.front(); Q.pop(); for(int i=now.bs; i<n; i++){ int v=a[i]^now.val; if(v==0)return now.step+1; else{ Node n2; n2.val=v; n2.step=now.step+1; n2.bs=i+1; Q.push(n2); } } } return -1; } int main(int argc, char *argv[]) { while( scanf("%d",&n)!=EOF ){ for(int i=0; i<n; i++){ scanf("%d",&a[i]); } ans=bfs(); printf("%d\n",ans); } return 0; }
相关文章推荐
- 圣经般的话
- 大O记法
- Wix学习整理(4)——关于WiX文件格式和案例HelloWorld的分析
- lucene入门02
- 移动开发 html 跨平台
- MP3Player2.0版本
- 最常用的详细正则表达式大全
- Winform初学
- Wix学习整理(3)——关于Windows Installer和MSI
- Memcached全面剖析–PDF总结篇
- 为什么调用glPushMatrix()和glPopMatrix()
- 会计
- 矩形数组和交错数组
- 学习OpenCV——KeyPoint Matching 优化方式
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门
- 从ini格式文件中(没行是“键=值”格式)中读取配置项的值,Split字符串的处理(综合)
- STM32NET学习笔记 IP ICMP部分
- Wix学习整理(2)——HelloWorld安装添加UI
- Wix学习整理(1)——快速入门HelloWorld
- C++与Lua交互(一)