POJ 1995 Raising Modulo Numbers(快速幂)

Description
给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M.
Input
第一行为用例组数T,每组用例第一行为一个整数M,第二行为一个整数H,之后H行每行两个整数Ai和Bi
Output
输出给出公式的值
Sample Input
3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132
Sample Output
2
13195
13
Solution
快速幂裸题
Code

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll mod_pow(ll a,ll b,ll p)
{
    ll ans=1ll;
    a%=p;
    while(b)
    {
        if(b&1)
        ans=(ans*a)%p;
        a=(a*a)%p;
        b>>=1;
    } 
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll M,a,b,ans=0ll;
        scanf("%lld",&M);
        int H;
        scanf("%d",&H);
        while(H--)
        {
            scanf("%lld%lld",&a,&b);
            ans=(ans+mod_pow(a,b,M))%M;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(POJ 1995 Raising Modulo Numbers(快速幂))