Bestcoder83 1001

来源:互联网 发布:复旦博士后待遇 知乎 编辑:程序博客网 时间:2024/05/03 11:57

zxa and set

Accepts: 466
Submissions: 538
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

zxa has a set A={a1,a2,⋯,an}, which hasnnn elements and obviously (2^n-1) non-empty subsets.

For each subset B={b1,b2,⋯,bm}(1≤m≤n) ofA, which hasmmm elements, zxa defined its value as min(b1,b2,⋯,bm).

zxa is interested to know, assuming that SoddS_{odd}Sodd represents the sum of the values of the non-empty sets, in which each set B is a subset of A and the number of elements in B is odd, and SevenS_{even}Seven represents the sum of the values of the non-empty sets, in which each set B is a subset ofA and the number of elements in B is even, then what is the value of ∣Sodd−Seven∣, can you help him?

Input

The first line contains an positive integer T, represents there are T test cases.

For each test case:

The first line contains an positive integer nnn, represents the number of the set A is n.

The second line contains nnn distinct positive integers, repersent the elements a1,a2,⋯,an.

There is a blank between each integer with no other extra space in one line.

1≤T≤100,1≤n≤30,1≤ai≤10^9

Output

For each test case, output in one line a non-negative integer, repersent the value of∣Sodd−Seven|

Sample Input
311031 2 341 2 3 4
Sample Output
1034

通过观察样例猜测答案即max(ai)\max(a_i),可以对小规模的数据进行模拟验证,实际答案就是max(ai)\max(a_i),时间复杂度O(n)O(n)

由于aia_iai互不相同,因此可以考虑每个元素在哪些子集中是最小值。

aia_iAA中第kk大的元素,以aia_ii为最小值的集合只能包含不小于aia的元素,因此这样的集合有2k−12^{k-1}个。

如果k=1k=1,则只有aa_iai为最小值;否则,上述集合中包含最大元素的子集和不包含最大元素的子集的元素个数奇偶性不同,对答案的贡献相互抵消。因此有Sodd−Seven=max(ai)S_{odd}-S_{even}=\max(a_i)

实际上,aia_不需要互不相同,也可以得到答案是max(ai)\max(a_i)的结论。


#include<stdio.h>int main(){int n,m,i, j, k;scanf("%d", &n);while (n--){scanf("%d", &m);for (i = 1; i <= m; i++){scanf("%d", &k);}printf("%d\n", k);}return 0;}


0 0
原创粉丝点击