题意:(A1B1+A2B2+ ... +AHBH)mod M.
思路:快速幂算出每一个,然后加在一起,这里快速幂最好不要用递归,容易爆,还是二进制思想吧
A^x = A^(bn*2^n + bn-1*2^n-1......+b0)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long int64;
int64 quick_mod(int64 a,int64 n,int64 m)
{
int64 A = a;
int64 sum = 1;
while(n)
{
if(n&1)
sum = (sum * A) % m;
A = A * A % m;
n = n >> 1;
}
return sum;
}
int main()
{
int64 t;
scanf("%I64d",&t);
while(t--)
{
int64 m,H,A,B;
scanf("%I64d",&m);
scanf("%I64d",&H);
int64 sum = 0;
for(int64 i = 0; i < H; i++)
{
scanf("%I64d%I64d",&A,&B);
sum = ( sum + quick_mod(A,B,m) ) % m;
}
printf("%I64d\n",sum);
}
return 0;
}