题干:
soda has a set SS with nn integers {1,2,…,n}{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 SS are key set.
Input
There are multiple test cases. The first line of input contains an integer TT (1≤T≤105)(1≤T≤105), indicating the number of test cases. For each test case:
The first line contains an integer nn (1≤n≤109)(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
解题报告:
这应该算是多校里面的大水题了吧?(ps:当时写的快速幂也太丑陋了吧、、、)
因为集合S中的元素是从1开始的连续的自然数,所以所有元素中奇数个数与偶数个数相同,或比偶数多一个。另外我们要知道偶数+偶数=偶数,奇数+奇数=偶数,假设现在有a个偶数,b个奇数,则
根据二项式展开公式
以及二项式展开式中奇数项系数之和等于偶数项系数之和的定理
可以得到
最后的结果还需减去
即空集的情况,因为题目要求非空子集
所以最终结果为
部分题解来自这里
AC代码:
#include<bits/stdc++.h>
#define mod 1000000007
typedef long long ll;using namespace std;
ll n;
ll qpow(ll a,ll k) {ll ans = 1;while(k > 0) {if(k&1) {ans*=a;}ans%=mod;a*=a;a%=mod;k>>=1;}return ans%mod;
}
int main() {int t;ll ans;cin>>t;while(t--) {scanf("%lld", &n);ans = qpow(2,n%mod-1);printf("%lld\n",ans-1);}return 0;
}