问题 H: 【例7.5】 取余运算(mod)

题目描述

输入b,p,k的值,求b p mod k的值。其中b,p,k*k为长整型数。

输入

输入b,p,k的值。

输出

求b p mod k的值。

样例输入

2 10 9

样例输出

2^10 mod 9=7

#include <iostream>
using namespace std;
int b,p,k;
int f( int p)
{
     if (p==0) return 1;
     int temp=f(p/2)%k;
     temp=(temp*temp)%k;
     if (p%2==1) temp=b*temp%k;
     return temp;
}
int main()
{
     cin>>b>>p>>k;
     int temb=b;
     b%=k;
     cout<<temb<< "^" <<p<< " mod " <<k<< "=" <<f(p)<<endl;
     return 0;
}

猜你喜欢

转载自blog.csdn.net/tiantianac/article/details/79796714