Sum HDU - 4704
原题链接
考察:费马小定理
思路:
指数对1000000007-1取余,模板题我WA了n次.
讲讲坑点:开long long,N>=1不代表指数-1一定>=0,存在%(M-1)-1<0的情况
Code
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1000010,M = 1000000007;
char s[N];
LL qsm(LL a,LL k,int m)
{
LL res =1;
while(k)
{
if(k&1) res = (LL)res*a%m;
a = (LL)a*a%m;
k>>=1;
}
return res;
}
int main()
{
while(scanf("%s",s+1)!=EOF)
{
int len = strlen(s+1);
LL res = 0;
for(int i=1;i<=len;i++)
{
res = res*10+(s[i]-'0');
res%=(M-1);
}
res = ((res-1)%(M-1)+M-1)%(M-1);
printf("%d\n",qsm(2,res,M));
}
return 0;
}