POJ1995 Raising Modulo Numbers 快速幂
//POJ1995 2020/10/07 23:25
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int T, n, mod;
int add (int x, int y) {
return (0ll + x + y) % mod;
}
int mul (int x, int y) {
return (1ll * x * y) % mod;
}
int fpow (int x, int y) {
if (x == 0) {
return 0;
}
int ret = 1;
while (y) {
if (y & 1) {
ret = mul (ret, x);
}
x = mul (x, x);
y >>= 1;
}
return ret;
}
int main () {
cin >> T;
while (T--) {
int ans = 0;
cin >> mod >> n;
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
ans = add (ans, fpow (x, y));
}
cout << ans << endl;
}
}
没啥好解释的。