poj 1995 Raising Modulo Numbers ——快速幂
April 2, 2013
POJ
Math
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long int LL;
const int MAXN = 0x3f3f3f3f;
const int MIN = -0x3f3f3f3f;
int m;
LL power(LL a, LL k){
LL ans = 1;
while (k){
if (k&1){
ans = (ans * a) % m; k--;
}
k >>= 1; a = (a*a)%m;
}
return ans;
}
int main(void){
#ifndef ONLINE_JUDGE
freopen("1995.in", "r", stdin);
#endif
int t; scanf("%d", &t);
int a, b, h; LL ans;
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);
}
return 0;
}
有点儿乱……其实代码挺好写的