快速幂

#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\n",a,c,k,ans);
}

int main()
{
    cin>>b>>p>>k;
    
    ksm(b,p);
    
    return 0;
    
}

 

posted @ 2020-11-30 20:36  yxr~  阅读(41)  评论(0编辑  收藏  举报