题目地址:http://poj.org/problem?id=1995
快速幂的简单模板题
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
ll mod_pow(ll a,ll b,ll mod){
ll ans=1;
while(b){
if(b&1){
ans=ans*a%mod;
}
a=a*a%mod;
b>>=1;
}
return ans;
}
int main(){
int z;
cin>>z;
for(int i=0;i<z;i++){
ll m,h,a,b,res=0LL;
cin>>m>>h;
for(int k=0;k<h;k++){
cin>>a>>b;
res=(res+mod_pow(a,b,m))%m;
}
cout<<res<<endl;
}
return 0;
}