HDU - 5363 Key Set 【模拟】【快速幂】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363

 

Problem Description

soda has a set S with n integers {1,2,…,n} . 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 S are key set.

 

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤105) , indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤109) , 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  

 

题意:给你一个具有n个元素的集合S{1,2,…,n},问集合S的非空子集中元素和为偶数的非空子集有多少个

分析:在我看来这题就是个规律题,连续写几个就会发现 ans[i] = 2*ans[i-1]+1 ,我于是愉快地写了一个递推。却发现编译不出来。问了问查了查才知道数组最大只能开到约2e7级别,oj评测机1s运算次数也就1e8级别,所以递推还是拉倒吧。然后又发现一个规律:,mmp,一个快速幂完事。

AC代码:

#include <iostream>
using namespace std;
typedef long long ll; 
//const int MAXN = 1e9+5;
//ll int a[MAXN]={0,0,1};		// 数组最大开 1e8数量级
const int mod = 1000000007; 	// int最大3e10 
ll FastPow(ll a,ll n)
{
	ll ans=1;
	a = a%mod;
	while(n!=0) {
		if( n%2)
			ans = (ans*a)%mod;
		a = (a*a)%mod;
		n /= 2;
	}
	return ans-1;	
} 
int main()
{
	/*
	ll i;
	for(i=3;i<MAXN;i++)		// t:1e8数量级啊!  这题就tm让你用递推过不了 
		a[i] = (2*a[i-1]+1)%mod;
	ll int n,t;
	cin >> n;
	for(i=0;i<n;i++) {
		cin >> t;
		cout << a[t] << endl;
	}
	*/	
	ll n,t;
	cin >> n;
	while( n--) {
		cin >> t;
		cout << FastPow(2,t-1) << endl;
	} 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值