题目链接:http://poj.org/problem?id=1995
题目大意:求出∑(Ai^Bi)mod M.
分析:简单的费马小定理快速幂取模,对于每一个Ai,Bi,分别求出Ai^B mod M然后相加再取模即可。
实现代码如下:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
LL quick_mod(LL a,LL b,LL m)
{
LL ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=ans*a%m;
b--;
}
b>>=1;
a=a*a%m;
}
return ans;
}
int main()
{
LL t,a,b,m,h;
cin>>t;
while(t--)
{
scanf("%lld%lld",&m,&h);
LL ans=0;
for(int i=0;i<h;i++)
{
scanf("%lld%lld",&a,&b);
ans=(ans+quick_mod(a,b,m))%m;
}
printf("%lld\n",ans);
}
return 0;
}