【 HDU - 5363】Key Set(水题,快速幂,组合数学)
題干:
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 4Sample Output
0 1 3 7解題報(bào)告:
? ?這應(yīng)該算是多校里面的大水題了吧?(ps:當(dāng)時(shí)寫的快速冪也太丑陋了吧、、、)
因?yàn)榧?#xff33;中的元素是從1開始的連續(xù)的自然數(shù),所以所有元素中奇數(shù)個(gè)數(shù)與偶數(shù)個(gè)數(shù)相同,或比偶數(shù)多一個(gè)。另外我們要知道偶數(shù)+偶數(shù)=偶數(shù),奇數(shù)+奇數(shù)=偶數(shù),假設(shè)現(xiàn)在有a個(gè)偶數(shù),b個(gè)奇數(shù),則
根據(jù)二項(xiàng)式展開公式
以及二項(xiàng)式展開式中奇數(shù)項(xiàng)系數(shù)之和等于偶數(shù)項(xiàng)系數(shù)之和的定理
可以得到
最后的結(jié)果還需減去
即空集的情況,因?yàn)轭}目要求非空子集
所以最終結(jié)果為
部分題解來自這里
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; }?
總結(jié)
以上是生活随笔為你收集整理的【 HDU - 5363】Key Set(水题,快速幂,组合数学)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysqld-nt.exe - mysq
- 下一篇: 【HDU - 1241】Oil Depo