Description
soda has a set $S$ with $n$ integers $\{1, 2, \dots, 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 \le T \le 10^5)$, indicating the number of test cases. For each test case:
The first line contains an integer $n$ $(1 \le n \le 10^9)$, 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
1
3
7
题意:
求1 2 3 ... n 的 所有子集中和为偶数的子集个数,mod 1000000007
分析:
数学归纳法证明和为偶数的子集有2n-1-1个:
- 当n=1时,有a1=0个
- 假设n=k时,有ak=2k-1-1个子集和为偶数,
- 若k+1为偶数,则ak个子集加上这个偶数,和还是偶数,这个偶数单独一个集合,和就是这个偶数,ak+1=ak*2+1=2k-1
- 若k+1为奇数,前k个数共有2k个子集,其中一个空集和为0,和为奇数的子集有2k-1-ak=2k-1个,和为奇数的子集加上k+1这个数,和变成了偶数,因此ak+1=ak+2k-1=2k-1
综合1,2得系列1 2 ... n 和为偶数的子集有2n-1-1个
接下来用快速幂即可。
代码:
#include<stdio.h>
#define ll long long
const ll M=1e9+;
ll t,n;
int main(){
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll k=,ans=;
n--;
while(n){
if(n&)ans=(ans*k)%M;
k=(k*k)%M;
n>>=;
}
printf("%lld\n",ans-);
}
}