HDU - 5363 Key Set

Key Set
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

soda has a set   with   integers  . A set is called key set if the sum of integers in the set is an even number. He wants to know how many nonempty subsets of   are key set.

Input

There are multiple test cases. The first line of input contains an integer    , indicating the number of test cases. For each test case: 
The first line contains an integer    , the number of integers in the set.

Output

For each test case, output the number of key sets modulo 1000000007.

Sample Input

        
        
4 1 2 3 4

Sample Output

        
        
0 1 3 7

题意:如果一个集合中所有元素加起来是一个偶数,则这个集合是key set.注意输出结果要对1000000007取余!变量还要用longlong或__int64...
这道题实际上就是求2^(n-1)-1的值,由于数据较大,要用到快速幂方法,链接:http://blog.csdn.net/ah_yeah/article/details/52004280
对有一个有n个元素的集合A,其非空子集有2^n-1个。不妨从该集合中任意取出一个奇数,则剩下的(n-1)个元素共有非空子集2^(n-1)-1个。在这些集合各自元素相加的和中: 和为偶数的自然就是key set;而 和为奇数的,加上取出的那个奇数,就成了key set.如此一来集合A的所有元素都用上了,因此它的key set个数就是2^(n-1)-1!!!(其实观察上面的数据就可使推出,上面那些论证也是问度娘的。。)
#include<cstdio>
#define mod 1000000007
long long quickpow(int n){
	long long ans=1,base=2;
	while(n){
		if(n&1)
			ans=(base*ans)%mod;
		base=(base*base)%mod;
		n>>=1;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	long long n;
	while(t--){
		scanf("%lld",&n);
		printf("%lld\n",quickpow(n-1)-1);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值