您的位置:首页 > 大数据 > 人工智能

poj 1995 Raising Modulo Numbers ——快速幂

2013-04-02 17:47 375 查看
题目链接:http://poj.org/problem?id=1995

标准的快速幂的题目,唯一需要注意的就是:结果要用longlongint。开始不知道,第二个样例怎么也过不了,后来看了书上的代码才知道……
其实应该想到要用LL的,这种错误应该自己查出来,题目中虽然没有说Ai,Bi的范围,但是保险起见,应该用LL啊。惭愧……


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#definelsonl,m,rt<<1
#definersonm+1,r,rt<<1|1
usingnamespacestd;
typedeflonglongintLL;
constintMAXN=0x3f3f3f3f;
constintMIN=-0x3f3f3f3f;

intm;
LLpower(LLa,LLk){
LLans=1;
while(k){
if(k&1){
ans=(ans*a)%m;k--;
}
k>>=1;a=(a*a)%m;
}
returnans;
}
intmain(void){
#ifndefONLINE_JUDGE
freopen("1995.in","r",stdin);
#endif
intt;scanf("%d",&t);
inta,b,h;LLans;
while(t--){
scanf("%d%d",&m,&h);
ans=0;
while(h--){
scanf("%d%d",&a,&b);
ans+=(power(a,b)%m);
}
ans%=m;
printf("%lld\n",ans);
}

return0;
}



有点儿乱……其实代码挺好写的


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: