hdu 1420(快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1420

题目很简单,暴力1A,可应该有更好的方法。。。搜了一下,可以用快速幂。。。我怎么没想到呢。。。如果数据强一点。。估计我又要跪了。。。orz..

View Code
 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 
 5 __int64 Pow(__int64 a,__int64 b,__int64 c){
 6     __int64 p=1,q=a;
 7     while(b){
 8         if(b&1){
 9             p=(p*q)%c;
10         }
11         b>>=1;
12         q=(q*q)%c;
13     }
14     return p;
15 }
16 
17 int main(){
18     int _case;
19     scanf("%d",&_case);
20     while(_case--){
21         __int64 a,b,c;
22         scanf("%I64d%I64d%I64d",&a,&b,&c);
23         printf("%I64d\n",Pow(a,b,c));
24     }
25     return 0;
26 }

 

posted @ 2013-04-05 10:45  ihge2k  阅读(192)  评论(0编辑  收藏  举报