题目链接: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:
Output For each test case, output the number of key sets modulo 1000000007.
|
题意:给你一个具有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;
}
}