POJ 1995 Raising Modulo Numbers
快速幂取模
#include<cstdio> int mod_exp(int a, int b, int c) { int res, t; res = 1 % c; t = a % c; while (b) { if (b & 1) res = res * t % c; t = t * t % c; b >>= 1; } return res; } int main() { int T; scanf("%d",&T); while(T--) { int m,h; scanf("%d%d",&m,&h); int ans=0; for(int i=1; i<=h; i++) { int a,b; scanf("%d%d",&a,&b); ans=(ans+mod_exp(a,b,m))%m; } printf("%d\n",ans); } return 0; }