快速幂取余

#include<iostream>
#define LL long long
using namespace std;
LL b,p,k;
inline void quickM()
{
    cin>>b>>p>>k;
    if(p==0)
     { 
       cout<<b<<"^"<<p<<" "<<"mod"<<" "<<k<<"="<<1%k;
       return;
     }
     
     cout<<b<<"^"<<p<<" "<<"mod"<<" "<<k<<"=";
    LL ans=1;
    while(p!=0)
    {
        if(p&1!=0)
         ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    cout<<ans;
}
int main()
{
    quickM();
    return 0;    
}

 

posted @ 2018-08-07 11:53  ltltlti-h  阅读(77)  评论(0编辑  收藏  举报