POJ 1995 快速幂模板

http://poj.org/problem?id=1995

简单的快速幂问题

要注意num每次加过以后也要取余,否则会出问题

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll mod_pow(ll x,ll n,ll mod)
{
    ll res=1;
    while(n>0)
    {
        if(n&1) res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
int main()
{
    int i,j,z,h;
    //freopen("in.txt","r",stdin);
    ll num=0,m,a,b;
    scanf("%d",&z);
    for(i=1;i<=z;i++)
    {
        scanf("%lld%d",&m,&h);
        for(j=1;j<=h;j++)
        {
            scanf("%lld%lld",&a,&b);
            num+=mod_pow(a,b,m);
            num%=m;
        }
        cout<<num<<endl;num=0;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dzzy/p/5241666.html