首页 > 代码库 > hdu 4704 Sum (费马小定理+快速幂)

hdu 4704 Sum (费马小定理+快速幂)

//(2^n-1)%mod
//费马小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m)
# include <stdio.h>
# include <algorithm>
# include <string.h>
# define mod 1000000007
using namespace std;
__int64 pow(__int64 n)
{
    __int64 p=1,q=2;
    while(n)
    {
        if(n%2)
        {
            p=p*q%mod;
        }
        n/=2;
        q=q*q%mod;
    }
    return p;
}
char str[1000100];
int main()
{

    __int64 i,n,len;
    while(~scanf("%s",str))
    {
        len=strlen(str);
        n=0;
        for(i=0;i<len;i++)
        {
            n=(n*10+str[i]-'0')%(mod-1);
        }
        printf("%I64d\n",pow(n-1));
    }
    return 0;
}