http://acm.fzu.edu.cn/problem.php?pid=1752
http://acm.fzu.edu.cn/problem.php?pid=1650
给跪了。
我的快速幂会越界。。
学习学习大神们的方法。。位运算好强大~
以后干脆当模版用好了- -|||
#include <cstdio>
typedef unsigned long long LL;
LL mod;
LL mul(LL a,LL b,LL c) //用快速幂的思想求a*b%c防止越界
{
LL ret=0,tmp=a%c;
while(b)
{
if(b&1)
if((ret+=tmp)>=c)
ret-=c;
if((tmp<<=1)>=c)
tmp-=c;
b>>=1;
}
return ret;
}
void pow_mod(LL a,LL n)
{
LL ret=1;
while(n) //a^n %mod
{
if(n & 1)
ret=mul(ret,a,mod);
a=mul(a,a,mod);
n>>=1;
}
printf("%llu\n",ret);
}
int main()
{
LL a,b;
while(~scanf("%llu%llu%llu",&a,&b,&mod))
{
pow_mod(a,b);
}
return 0;
}