hdu1420 Prepared for New Acmer ——快速幂

June 1, 2013
Hdu Math

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1420 题目大意:   中文题。 题目思路:   赤裸裸的快速幂。呵呵


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
#define LL long long
LL m;
LL Po(LL a, LL b) {
  LL ans = 1;
  while (b) {
    if (b&1) {
      ans = (ans * a) % m;
      b--;
    }
    b /= 2; a = a * a % m;
  }
  return ans;
}
int main(void) {
  LL n, a, b;
#ifndef ONLINE_JUDGE
  freopen("1420.in", "r", stdin);
#endif
  scanf("%I64d", &n);
  while (n--) {
    scanf("%I64d%I64d%I64d", &a, &b, &m);
    printf("%I64d\n", Po(a, b));
  }
  return 0;
}

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

comments powered by Disqus