您的位置:首页 > 其它

hdu_1420 Prepared for New Acmer

2013-07-03 10:01 351 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1420

分析:快速幂的方法,即(a)^50=(a^2)^50,再将a=a^2。。。当指数为奇数时a^25,可以拿出一个a,a*a^24;让一个S=1,储存移除的a,S*=a.到最后的时候会S*a^2,然后a=a^2,S*a,a的指数为1了,S*=a,所以最后S存储着总值。

我的代码:

#include<stdio.h>
typedef __int64 LL;

LL qmum(LL a,LL b,LL m)
{
LL s=1;
LL low=a%m;
while(b)
{
if(b&1) s=(s*low)%m; //当指数为奇数。
b=b>>1; //当指数除以二。
low=(low*low)%m;
/* 上面的三行代码等价于下面的。
if(b%2==1)
{
s=(s*low)%m;
b--;
}
else
{
low=(low*low)%m;
b/=2;
}
*/
}
return s%m;

}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{

LL a,b,mod; //a<=1000000,如果用int 两个a一乘就会溢出,所以要用64位的。
scanf("%I64d%I64d%I64d",&a,&b,&mod);
printf("%I64d\n",qmum(a, b, mod));

}

return 0;
}



总结:第一次没有用__int64,就果断WA了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: