取余运算
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
输入b,p,k的值,求b^p mod k的值。
Input
输入有多组数据,每组数据为一行三个数b,p,k,其中b,p,k*k为长整型数。
Output
对于每组数据输出b^p mod k的值。
Sample Input
2 10 9
Sample Output
2^10 mod 9=7
思路分析:本题为模板题目;
代码:取余运算
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
输入b,p,k的值,求b^p mod k的值。Input
输入有多组数据,每组数据为一行三个数b,p,k,其中b,p,k*k为长整型数。Output
对于每组数据输出b^p mod k的值。Sample Input
2 10 9Sample Output
2^10 mod 9=7思路分析:本题为模板题目;代码:#include<cstdio> #include<iostream> using namespace std; long long PowMod( long long a,long long b,long long c) { long long ans=1; while(b) { if(b&1) ans=a*ans%c; a=(a%c)*(a%c)%c; b/=2; } return ans; } int main() { long long k,p,m,s; while(cin>>k>>p>>m) { cout<<k<<"^"<<p<<" "<<"mod"<<" "<<m<<"="; s=PowMod(k,p,m); cout<<s<<endl; } return 0; }