时间限制: 1000 ms 内存限制: 65536 KB提交数: 6896 通过数: 3129
【题目描述】
输入b,p,kb,p,k的值,求bpmodkbpmodk的值。其中b,p,k×kb,p,k×k为长整型数。
【输入】
输入b,p,kb,p,k的值。
【输出】
求bpmodkbpmodk的值。
【输入样例】
2 10 9
【输出样例】
2^10 mod 9=7
#include<iostream>
#define MOD 1000000007
using namespace std;
int b,p,k;
int calculate(int p)//a*b%k=a%k*b%k%k;
{
int temp;
if(p==0)
return 1;
temp=calculate(p/2)%k;
temp=(temp*temp)%k;
if(p%2==1)
temp=(temp*b)%k;
return temp;
}
int main()
{
cin>>b>>p>>k;
int temp=b;
b%=k;
cout<<temp<<"^"<<p<<" mod "<<k<<"="<<calculate(p)<<endl;
return 0;
}