996B
程序员文章站
2022-04-17 18:32:26
...
https://ac.nowcoder.com/acm/contest/996/B
快速幂模板
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ans;
ll a, b, mod;
ll quick_pow(ll a,ll b,ll mod)
{
ll ans = 1,base = a;
while(b)
{
if(b&1)
ans = ans * base % mod;
base = base * base % mod;
b >>= 1;
}
return ans % mod;
}
int main()
{
int T;
cin >> T;
while(T--)
{
ans = 0;
cin>>mod;
int n;
cin>>n;
for (int i = 1;i <= n;i++)
{
scanf("%lld%lld",&a,&b);
ans = (ans + quick_pow(a, b, mod)) % mod;
}
printf("%lld\n", ans);
}
}
推荐阅读