HDU 4704

http://acm.hdu.edu.cn/showproblem.php?pid=4704

求(2^n)%mod的方法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
using namespace std ;

//(2^n)%mod=(2^(n%(mod-1)))%mod

const int mod=1000000007 ;

__int64 POW(int b)
{
    __int64 res=1,a=2 ;
    while(b)
    {
        if(b&1)res=(res*a)%mod ;
        a=(a*a)%mod ;
        b>>=1 ;
    }
    return res ;
}

char s[100005] ;

int main()
{
    while(~scanf("%s",s))
    {
        int len=strlen(s) ;
        __int64 n=0 ;
        for(int i=0 ;i<len ;i++)
            n=(n*10+s[i]-'0')%(mod-1) ;
        printf("%I64d
",POW(n-1)) ;    
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/3965096.html