poj1995 raising modulo numbers
这题就不解析了吧......
快速幂模板而已。
1 #include <cstdio> 2 using namespace std; 3 typedef long long LL; 4 LL mo; 5 LL qpow(LL a, LL b) { 6 if(a == 0) { 7 return 0; 8 } 9 if(b == 0) { 10 return 1; 11 } 12 int ans = 1; 13 while(b) { 14 if(b & 1) { 15 ans = (ans * a) % mo; 16 } 17 a = (a * a) % mo; 18 b = b >> 1; 19 } 20 return ans; 21 } 22 23 int main() { 24 int T; 25 scanf("%d", &T); 26 while(T--) { 27 LL n, ans = 0, a, b; 28 scanf("%I64d%I64d", &mo, &n); 29 for(int i = 1; i <= n; i++) { 30 scanf("%I64d%I64d", &a, &b); 31 ans = (ans + qpow(a % mo, b)) % mo; 32 } 33 printf("%I64d\n", ans); 34 } 35 return 0; 36 }