poj1995

快速幂

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
using namespace std;

#define LL long long

int m, h;

LL power(LL a, LL b, LL m)
{
if (a == 0)
return 0;
if (b == 0)
return 1;
a
%= m;
LL x
= a;
LL ret
= 1;
for (; b > 0; b >>= 1, x = x * x % m)
if (b & 1)
ret
= ret * x % m;
return ret;
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
scanf(
"%d%d", &m, &h);
LL ans
= 0;
for (int i = 0; i < h; i++)
{
LL a, b;
scanf(
"%lld%lld", &a, &b);
ans
= (ans + power(a, b, m)) % m;
}
printf(
"%lld\n", ans);
}
return 0;
}
原文地址:https://www.cnblogs.com/rainydays/p/2162115.html