so easy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 395 Accepted Submission(s): 283
Problem Description
Given an array with
your task is: calculate xor of all f(s) , here s⊆S .
n
integers, assume
f(S)
as the result of executing xor operation among all the elements of set
S
. e.g. if
S={1,2,3}
then
f(S)=0
.
your task is: calculate xor of all f(s) , here s⊆S .
Input
This problem has multi test cases. First line contains a single integer
T(T≤20)
which represents the number of test cases.
For each test case, the first line contains a single integer number n(1≤n≤1,000) that represents the size of the given set. then the following line consists of n different integer numbers indicate elements( ≤109 ) of the given set.
For each test case, the first line contains a single integer number n(1≤n≤1,000) that represents the size of the given set. then the following line consists of n different integer numbers indicate elements( ≤109 ) of the given set.
Output
For each test case, print a single integer as the answer.
Sample Input
1 3 1 2 3
Sample Output
0 In the sample,$S = \{1, 2, 3\}$, subsets of $S$ are: $\varnothing$, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Source
问的是给定n个数的集合里边把当前集合所有子集合里边的元素异或运算之后的结果再进行异或运算,首先知道异或满足交换律,比如(a^b)^(c^d)=a^b^c^d;
又知道异或运算的性质,A^A = 0,A ^ 0=A ;
首先把集合里边的元素分为子集合,因为集合里边的每个元素都平等,我们先求出子集合里边第一个元素出现了多少次,当子集合为里边只有一个元素时只有一种情况,即剩下的n-1个元素里边选出来0个,记做c(n-1,0),子集合里边的元素只有两个时,即剩下的n-1个元素里边选出1个记做c(n-1,1),....子集合里边的元素有n个时,即剩下的n-1个元素里边选出来n-1个,记做c(n-1,n-1),所有包含第一个元素的集合的个数总和为c(n-1,0)+c(n-1,1)++++c(n-1,n-1)=2^(n-1),当n等于1 时为奇数,其余情况全为偶数,因为所有的元素地位平等,就相当于,把集合里边的所有元素先异或一遍,之后再把得到的结果异或2^(n-1)遍,所以得到,当n为1 时,直接输出所输入的数,否则输出0;
附代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int i,j,k,l,m,n,ans,p;
int main()
{
scanf("%d",&p);
while(p--)
{
scanf("%d",&n);
scanf("%d",&ans);
for(i=1;i<n;i++)
scanf("%d",&k);
if(n==1)
printf("%d\n",ans);
else
printf("0\n");
}
}