题目戳这里
这道题是典型的快速幂求解的问题。题意:给你一个数M和H对(Ai,Bi)(1<=i<=H),让你求(A1B1+A2B2+ … +AHBH)mod M.
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
ll stt(ll base, ll power,ll M) {
ll result = 1;
while (power > 0) {
if (power & 1) {//此处等价于if(power%2==1)
result = result * base % M;
}
power >>= 1;//此处等价于power=power/2
base = (base * base) % M;
}
return result;
}
int main()
{
ll t, H;
ll M;
scanf("%d", &t);
while(t--)
{
scanf("%lld", &M);
scanf("%d", &H);
ll a, b;
ll ans = 0;
while(H--)
{
scanf("%lld%lld", &a, &b);
ans = (ans + stt(a, b, M)) % M;
}
printf("%lld\n", ans);
}
return 0;
}