题目链接:https://vjudge.net/problem/POJ-1995
快速幂求解(A1B1+A2B2+ ... +AHBH)mod M.
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<cstring>
#define LL long long
using namespace std;
#define N 1100000
LL mod;
LL quickpow(LL n,LL k)
{
int ans=1;
while(k)
{
if(k&1)
ans=ans*n%mod;
n=n*n%mod;
k/=2;
}
return ans;
}
bool isPrime(LL n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return false;
}
return true;
}
int main()
{
int T;
while(cin>>T)
{
while(T--)
{
int n;
LL sum=0;
cin>>mod;
// int n;
cin>>n;
LL a,b;
while(n--)
{
cin>>a>>b;
sum=(sum+quickpow(a,b))%mod;
}
cout<<sum<<endl;
}
}
return 0;
}