输入b,p,k的值,编程计算bp mod k的值。其中的b,p,k*k为长整型数(2^31范围内)。
b p k
输出b^p mod k=?
=左右没有空格
2 10 9
2^10 mod 9=7
#include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; LL b,p,k,i,j; LL powe(LL m,LL n,LL c) { LL r=1,base=m; while(n) { if(n&1) r=r*base%c; base=base*base%c; n>>=1; } return r; } int main() { ios::sync_with_stdio(false); cin>>b; cin>>p; cin>>k; LL ans=powe(b,p,k); cout<<b<<"^"<<p<<" mod "<<k<<"="<<ans; }
原文:http://www.cnblogs.com/ruojisun/p/6384020.html