一个数的整数次幂,是我们在计算中经常用到的,但是怎么可以在 O(log(n)) 的时间内算出结果呢?
代码框中的代码是一种实现,请分析并填写缺失的代码,求 x^y mod p 的结果。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 5e4+10;
#define inf 0x3f3f3f3f
int x,p,y;
int pw(int x,int y,int p){
if(y==0){
return 1;
}
int res=pw(x*x,y>>1,p);
cout<<res<<endl;
if(y&1){
res=res*x%p;
}
return res;
}
int main(){
int x,y,p;
cin>>x>>y>>p;
cout<<pw(x,y,p)<<endl;
return 0;
}