快速幂模板
long long quickpow(int x,int N)
{
long long res = x;
long long ans = 1;
while(N)
{
if(N&1)
{
ans=ans*res;
}
res=res*res;
N=N>>1;
}
return ans;
}
需要取余时自行取余
下面是例题
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<stack>
#include<cstring>
using namespace std;
#define M 1000000007
long long quickpow(int x,int N)
{
long long res = x;
long long ans = 1;
while(N)
{
if(N&1)
{
ans=(ans*res)%M;
}
res=(res*res)%M;
N=N>>1;
}
return ans;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int m;
cin>>m;
long long ans=quickpow(2,m-1)-1;
cout<<ans<<endl;
}
return 0;
}