快速幂

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

ull b,p,k;

void ksm(ull b,ull p)
{
    ull a=b,c=p;
    ull ans=1;
    if(p==0) ans=1;
    else
    while(p)
    {
        if(p%2==1) ans=ans*b%k;
        b=b*b%k;
        p/=2;
    } 
    ans=ans%k;
    printf("%llu^%llu mod %llu=%llu
",a,c,k,ans);
}

int main()
{
    cin>>b>>p>>k;
    
    ksm(b,p);
    
    return 0;
    
}
原文地址:https://www.cnblogs.com/yxr001002/p/14063463.html