标签:
1 #include <iostream> 2 using namespace std; 3 4 long long power(long long a, long long b, long long m); 5 6 int main() 7 { 8 //freopen("acm.acm","r",stdin); 9 int test; 10 long long sum; 11 long long a; 12 long long b; 13 long long m; 14 long long n; 15 int i; 16 cin>>test; 17 while(test --) 18 { 19 sum = 0; 20 cin>>m; 21 cin>>n; 22 for(i = 0; i < n; ++ i) 23 { 24 cin>>a>>b; 25 sum += power(a,b,m); 26 } 27 28 cout<<sum%m<<endl; 29 } 30 } 31 32 long long power(long long a, long long b, long long m)//快速幂取模加速~ 33 { 34 long long ret = 1; 35 while (b) 36 { 37 if (b & 1)//如果把b化成二进制的话,是否 最低位是1 38 { 39 ret *= a; 40 ret %= m; 41 } 42 a *= a; 43 a %= m; 44 b >>= 1; //右移 45 } 46 return ret; 47 }
标签:
原文地址:http://www.cnblogs.com/gavinsp/p/4566603.html