2015 Multi-University Training
Contest-5
Key Set
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 711 Accepted Submission(s): 436
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
41234
Sample Output
0137
Source
2015 Multi-University Training Contest 6
分析:
题目大意:给出一个数n,问从1~n的集合中能找出多少个
子集满足集合中全部元素和为偶数、如果用排列组合的方式一个个计
算的话,由于测试数据太多必定超时,多算几个数观察可得如下规律:
F(n)=F(n-1)*2+1 F(n)=2^(n-1)-1
因此至少有两种思路来解决此问题,递推打表or公式计算。
1、递推打表:如果递推的话就是采用打表的形式,把所有的F(n)
2、计算出来,因为n<10^9,所以打表注定内存超限。在没想到解
决办法的情况下放弃此种思路。
3、公式计算:计算的话也存在一个问题,由于过于庞大的n值,
F(n)=2^(n-1)-1的结果int型保存不下,更别说取余运算,然后使用快速幂取余。一次AC(得益于刘成豪学长的提醒,以及提供的模板)
#include<iostream>using namespace std;
__int64 quickpow(__int64 m, __int64 n, __int64 k)
{
__int64 b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m) % k;
n = n >> 1;
m = ( (m%k)*(m%k) )% k;
}
return b;
}
int main()
{
__int64 t;
cin >> t;
while (t--)
{
__int64 n;
cin >> n;
cout << quickpow(2, n - 1, 1000000007)-1 << endl;
}
return 0;
}