Description
Input
The first line contains an integer $n$ $(1 \le n \le 10^9)$, the number of integers in the set.
Output
Sample Input
Sample Output
求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+7; ll t,n; int main(){ scanf("%lld",&t); while(t--){ scanf("%lld",&n); ll k=2,ans=1; n--; while(n){ if(n&1)ans=(ans*k)%M; k=(k*k)%M; n>>=1; } printf("%lld\n",ans-1); } }
┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆