快速幂取模拓展

#include<iostream>
#include<cstring>
using namespace std;
const long long p=1e7;
int main()
{
    char a[150];
    long long m,n,k,t,ans,lenth;
    while(cin>>a>>m>>n)
    {
        lenth=strlen(a);
        k=0;
        t=0;
        while(k<lenth)
        {
            while(k<lenth&&t<p)
            {
                t=t*10+a[k]-'0';
                k++;
            }
            t=t%n;
        }
        ans=1;
        while(m>0)
        {
            if(m%2!=0)
            ans=(ans*t)%n;
            m=m/2;
            t=(t*t)%n;
        }
        cout<<ans%n<<endl;
    }
}
原文地址:https://www.cnblogs.com/Leozi/p/10835200.html