快速幂运算的简单题
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map>
#include<iostream>
#include<cstdio>
using namespace std;
long long qpowmod(long long x, int n, int mod)
{
long long res = 1;
while (n > 0)
{
if (n & 1)
{
res *= x;
res %= mod;
}
x *= x;
x %= mod;
n >>= 1;
}
return res;
}
bool isprime(int a)
{
for (int i = 2; i *i <= a;i++)
{
if (a % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int p, a;
#ifdef _DEBUG
freopen("d:\\in.txt", "r", stdin);
#endif
int Z;
scanf("%d\n", &Z);
for (int i = 0; i < Z;i++)
{
int mod;
scanf("%d\n", &mod);
int n;
scanf("%d\n", &n);
long long sum = 0;
for (int j = 0; j < n; j++)
{
int a, b;
scanf("%d %d\n", &a, &b);
sum += qpowmod(a, b, mod);
sum %= mod;
}
printf("%I64d\n", sum);
}
return 1;
}