2389 - 取余运算
输入b,p,k的值,求bp mod k的值。其中b,p,k。
Input
一行,b, p, k,以空格隔开。(1000 <= p <= 2^64 - 1)
Output
一行,输出形式为b^p mod k=xxx。 (等号前后无空格)
其中xxx为计算出的结果。
Examples
Input
2 10 9
Output
2^10 mod 9=7
Hint
A*B%K = (A%K )*(B% K )%K
P=2 * (P/2) + P%2,如19=2 * (19/2) + 19%2=2*9+1
Solution C++
#include<cstdio> #include<iostream> using namespace std; long long n,p,mm,ans,q; int main(){ cin>>n>>p>>mm; printf("%lld^%lld mod %lld=",n,p,mm); ans=1; q=n; while (p){ if (p & 1) ans=ans*q %mm; p>>=1; q=q*q % mm; } cout<<ans<<endl; return 0; }
Hint
A*B%K = (A%K )*(B% K )%K
P=2 * (P/2) + P%2,如19=2 * (19/2) + 19%2=2*9+1