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 4 1 2 3 4 Sample Output 0 1 3 7 Author zimpha@zju Source 2015 Multi-University Training Contest 6
问题描述
soda有一个集合S,其中有n个整数{1,2,…,n}。如果一个集合中的整数和是偶数,那么这个集合就称为键集合。他想知道有多少S的非空子集是键集。
输入
有多个测试用例。输入的第一行包含一个整数T(1≤T≤105),表示测试用例的数量。对于每个测试用例:
第一行包含一个整数n(1≤n≤109),即集合中整数的个数。
输出
对于每个测试用例,输出模块1000000007的密钥集数量。
样例输入
4
1
2
3.
4
样例输出
0
1
3.
7
快速幂
# include <iostream>
# include <algorithm>
# include <cstring>
# include <math.h>
# include <stdio.h>
int num[100100];
const int N = 1e9 + 7;
using namespace std;
#define ll long long
ll powerMod(ll a,ll b, ll c){//快速幂:a = 2,b 为 次方数,c为 mod
ll ans = 1;//初始值2的0次方
a = a % c;//%mod
while(b > 0){//将2的n次方,转换为2的2进制次方,从而大大提高效率
if(b&1) //判断b是否为奇数,是则这位为1
ans = (ans * a) % c;//若为奇数则ans要乘上a
b = b/2;
a = (a * a) % c;//2(1),2(10),2(100)4*4 = 2的4次方 16*16 = 2的8次方
//通过a将其转换为2的2(n)(二进制) 次方的次方
}
return ans;
}
int main(){
ll t;
cin >> t;
while(t--){
ll n;
cin >> n;
ll tot;
tot = powerMod(2,n-1,N) - 1;
cout << tot << endl;
}
return 0;
}
源