Time Limit:2800MS Memory Limit:3000K
哎,这题太神了,各种卡内存和时间。。。
数据太多,输入要用 getchar()
//#include <iostream>
#include<cstdio>
#include<cstring>
#define M 2000000
//using namespace std;
bool f[M]; // 所能开的最大数组
int main()
{
int n;
unsigned long long x;
char ch;
while(scanf("%d",&n)==1)
{
memset(f,false,sizeof(f)); // 如果手动归 0 ,会超时!
for(int i=0;i<n;i++)
{
x=0;
while((ch=getchar())==' ' || ch=='\n');
x=ch-'0';
while( (ch=getchar())>='0' && ch<='9')
x=x*10+ch-'0';
if(f[x%M]){
printf("%llu\n",x);
}else f[x%M]=true;
}
// puts("cao");
}
return 0;
}<strong>
</strong>
方法二:
我们可以在输入的过程中得到3个数,即min(最小的数),max(最大的数),XOR(所有数的异或值),显然答案等于 XOR ^ [min ^ (min + 1) ^ ... ^ max],这里 ^ 表示异或运算。此时问题变成了需要求区间[min, max]所有的数的异或值,这是OJ上的一道陈题(链接),这里简单说下一种求解区间[a, b]所有的数的异或值的解法:
假设x是偶数,那么x ^ (x+1) 就等于1,也即x和(x+1)只有最低位不同。好了,我们会发现区间[a, b]是由大量的这样的x和x+1构成(除了边界情况)。
所以我们只需要从a和b的奇偶性入手即可,这里只考虑其中一种情况,即a是奇数,b是偶数,那么我们总可以把区间[a, b]划分为:
a, (a+1, a+2), (a+3, a+4), ..., (b-2, b - 1), b
显然中间有括号的两个数的异或值为1,假设这样的配对的数有y个,那么答案就是a ^ (y & 1) ^ b,不是吗?(其他情况可以参考着来。)
//#define SPJ
#ifdef SPJ
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#endif
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define out(x) cout << (x) << endl
#define out2(x,y) cout << (x) << " " << (y) << endl
unsigned long long gao(unsigned long long l, unsigned long long r)
{
if( l == r )
return l;
unsigned long long res = 0;
if( l & 1LL ) {
res ^= l;
l ++;
}
if( !(r & 1LL) ) {
res ^= r;
r --;
}
if( l > r )
return res;
unsigned long long last = ((r - l + 1LL) >> 1) & 1LL;
return (res ^ last);
}
int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
int n, i;
unsigned long long mi, ma, res, ans;
unsigned long long val;
char ch;
while( scanf("%d", &n) == 1 ) {
//scanf("%llu", &val);
while( (ch = getchar()) == ' ' || ch == '\n' ) ;
val = ch - '0';
while( (ch = getchar()) >= '0' && ch <= '9' )
val = val * 10 + (ch - '0');
ans = mi = ma = val;
for(i = 1; i < n; i ++) {
//scanf("%llu", &val);
while( (ch = getchar()) == ' ' || ch == '\n' ) ;
val = ch - '0';
while( (ch = getchar()) >= '0' && ch <= '9' )
val = val * 10 + (ch - '0');
if( mi > val )
mi = val;
if( ma < val )
ma = val;
ans ^= val;
}
res = gao(mi, ma);
//dbg3(mi, ma, res);
printf("%llu\n", (res ^ ans));
}
return 0;
}