foj 1001 Duplicate Pair【简单位运算】
http://acm.fzu.edu.cn/problem.php?pid=1001
题目大意;找到1~n-1一共n个数中出现过两次的那个数。
先预处理,异或操作,假设所有的数都出现了一次;然后再依次异或这n个数,结果就是所有出现过奇数次的数的异或和,而只有答案要求的那个数(加上预处理)是出现奇数次的,所以结果就是所要的答案。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> using namespace std; template <class T> void checkmin(T &t,T x) {if(x < t) t = x;} template <class T> void checkmax(T &t,T x) {if(x > t) t = x;} template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;} template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;} typedef pair <int,int> PII; typedef pair <double,double> PDD; typedef long long ll; #define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++) int a[1000100]; void init() { for(int i=1;i<=1000000;i++) a[i] = a[i-1]^i; } int main() { int n , aa; init(); while(~scanf("%d",&n)) { int ans = a[n-1]; while(n--) { scanf("%d",&aa); ans ^= aa; } printf("%d\n" , ans); } return 0; }