HDU 5650 so easy 数学

so easy

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5650

Description

Given an array with
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.

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}

Hint

题意

给你一个集合S,然后定义F(s)表示这个集合所有元素的异或和。

然后求所有S的子集的F(s)的异或和

题解:

考虑每个数的贡献,显然当n>1的时候,每个数在2^(n-1)个集合里面,所以贡献为0

当n=1的时候,答案就是x啦

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1050;
int a[maxn];
void solve()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    if(n==1)printf("%d\n",a[1]);
    else printf("0\n");
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5324451.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞