http://acm.hdu.edu.cn/showproblem.php?pid=4704
题中定义的整数分解 对于n有2^(n-1)种方案 打个表就有规律
可是幂次太大 这时要用到欧拉定理 即若a与n互质 则a^f(n)%n=1 (f为欧拉函数)
现在要求(2^n)%mod 因为mod=1e9+7为一个素数 所以f(mod)=mod-1 得2^(mod-1)%mod=1 这样就找到了循环节mod-1 将给定的n对(mod-1)取模即可 即求2^((n%(mod-1)-1+(mod-1))%(mod-1))%mod
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+10;
char ch[maxn];
int n;
ll quickpow(ll a,ll b)
{
ll res;
res=1;
while(b>0){
if(b%2) res=(res*a)%mod;
a=(a*a)%mod,b/=2;
}
return res;
}
int main()
{
ll val,ans;
int i;
while(scanf("%s",ch)!=EOF){
n=strlen(ch),val=0;
for(i=0;i<n;i++){
val=(10ll*val+ch[i]-'0')%(mod-1);.
}
if(val==0){
ans=quickpow(2,mod-2);
}
else{
ans=quickpow(2,val-1);
}
printf("%lld\n",ans);
}
return 0;
}