hdu1420 Prepared for New Acmer ——快速幂

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

题目大意:

  中文题。

题目思路:

  赤裸裸的快速幂。呵呵

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 using namespace std;
 6 #define LL long long
 7 LL m;
 8 LL Po(LL a, LL b) {
 9   LL ans = 1;
10   while (b) {
11     if (b&1) {
12       ans = (ans * a) % m;
13       b--;
14     }
15     b /= 2; a = a * a % m;
16   }
17   return ans;
18 }
19 int main(void) {
20   LL n, a, b;
21 #ifndef ONLINE_JUDGE
22   freopen("1420.in", "r", stdin);
23 #endif
24   scanf("%I64d", &n);
25   while (n--) {
26     scanf("%I64d%I64d%I64d", &a, &b, &m);
27     printf("%I64d\n", Po(a, b));
28   }
29   return 0;
30 }

WA了几次……原因就是输入输出要用%I64d,用%lld或者类型不用LL就出错。题目本身是说要范围10^6,如果平方一下,显然就超int了,所以不能用int

posted on 2013-06-01 19:33  aries__liu  阅读(341)  评论(0编辑  收藏  举报