http://codeforces.com/contest/76/problem/D
A = X + Y
B = X xor Y
异或(不进位加法):两个二进制数,对应的位置上,相同为0,不同为1
性质:a^a=0,a^0=a,满足交换律
所以 A >= B (等号的情况:对应位置上一个0,一个1,大于的情况,对应位置上存在两个1)
且A-B一定为偶数(奇+奇=偶,奇^奇=偶数;奇+偶=奇,奇^偶=奇;偶+偶=偶,偶^偶=偶)
所以(A-B)/2 是 X和Y的公共部分,且在满足条件时,X是较小的那一个,所以X=(A-B)/2;Y=A-(A-B)/2
注意A,B的范围0 ≤ A, B ≤ 2^64 - 1,超过longlong的取值范围,而在unsigned long long范围内,因此可以用unsigned long long。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
typedef long long ll;
typedef unsigned long long unll;
using namespace std;
int main ()
{
unll a,b;
cin >> a >> b;
if((a<b) || (a-b)&1) cout << "-1";
else
cout << (a-b)/2 << " " << a-(a-b)/2;
return 0;
}
数据范围
char -128 ~ +127 (1 Byte)
short -32767 ~ + 32768 (2 Bytes)
unsigned short 0 ~ 65535 (2 Bytes)
int -2147483648 ~ +2147483647 (4 Bytes)
unsigned int 0 ~ 4294967295 (4 Bytes)
long == int
long long -9223372036854775808 ~ +9223372036854775807 (8 Bytes)
double 1.7 * 10^308 (8 Bytes)unsigned int 0~4294967295
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
只出现奇数次,应用异或的性质即可。
http://acm.hdu.edu.cn/showproblem.php?pid=2095
#include <iostream>
#include <cmath>
#include <cstdio>
typedef long long ll;
using namespace std;
int main ()
{
int x, n, ans;
while(scanf("%d",&n)!=EOF)
{
ans = 0;
if(n==0)
break;
while(n--)
{
scanf("%d",&x);
ans ^= x;
}
printf("%d\n",ans);
}
return 0;
}