hdu5650 so easy 组合数
2016-03-27 08:58
309 查看
so easy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 161 Accepted Submission(s): 130
Problem 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 ndifferent
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
BestCoder Round #77 (div.2)
因为偶数个相同的数异或得0,奇数个相同的数异或得自身。
发现一个规律,每个元素在所有子集中出现的次数都一样,而且都是2^(n-1),比如样例
是C(1, 1) + C(2, 1) + C(2, 2) = 2 ^ (3 - 1)
当n = 4的时候再推一推发现为C(1, 1) + C(3, 1) + C(3, 2) +C(3, 3) = 2^(4 - 1) = 8
其实就是个杨辉三角的关系
1 2 ^ (1 - 1) = 1
1 1 2^(2 -1) = 2
1 2 1 2^(3 - 1) = 4
1 3 3 1 2 ^ (4 - 1) = 8
.....................
当n为1的时候,也就是只有一个元素的时候输出自身,其他情况所有元素在所有子集中出现的次数都是偶数,最后异或的结果肯定为0
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue> #include <stack> using namespace std; long long a[1005]; int main() { int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); } if (n == 1) { printf("%I64d\n", a[0]); } else { puts("0"); } } return 0; }
相关文章推荐
- oracle11g 在azure云中使用rman进行实例迁移
- 基于javascript实现九九乘法表
- Git Visual Studio gitignore 去除不需要的dll Nuget包等等文件
- 【bzoj4043】【Cerc2014】【Vocabulary】【dp+预处理】
- 学习进度03
- 第五周项目1-三角形类雏形(1)
- 在matlab上安装svm
- 在matlab上安装svm
- 六款小巧的HTTP Server
- 鲈鱼
- 图片测试
- LINUX磁盘加密之CRYPTO
- Android之——通过文件名获取资源id
- ios-其他之【1】-证书
- Java设计模式之策略模式
- APP隐私政策
- 注意代码的规范和可读性
- Mvc4系列文章
- 迷惑摘要
- 华硕X84L无线驱动查找