Sample Input
2
Sample Output
2
Hint
1. For N = 2, S(1) = S(2) = 1. 2.
The input file consists of multiple test cases.
题目实则求2^(n-1)%(1e9+7)
费马小定理(降幂)
由于N的范围比较大,所以需要用此方法降幂
费马小定理的内容是:a为整数,p为质数,a与p互质,则恒有a^(p-1)%p==1;
所以n中含有多个p-1,所以可以通过k=(n-1)%(p-1)进行降幂,最终求的是a^k%p;
结合快速幂便可求出结果。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
char str[1000000];
ll quick_pow(ll a,ll b) //快速幂
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%mod;
b--;
}
b/=2;
a=a*a%mod;
}
return ans;
}
int main(){
while(~scanf("%s",str)){
ll K = mod-1;
ll sum = 0;
int len = strlen(str);
for(int i = 0;i < len;i ++){
sum = sum*10+(str[i]-'0');
sum = sum%K; //费马小定理
}
ll ans;
ans = quick_pow(2,(sum-1));
printf("%lld\n",ans);
}
return 0;
}