When I was brushing the questions before, I achieved fast power, I was confused, and the competition was approaching. Although I was not as strong as my seniors (called me scumming), we can't just give up. After all, this is also the testimony of my one-semester study! Today, I will summarize the problem of fast power.
The meaning of the question is very simple: give you a number x, ask it to calculate the value of its y power modulo p! That is x^y%b
.
// Implement fast power by recursion int PW (int x, int y, int p) { if(!y){ return 1; } int res=PW(x,y/2,p)*PW(x,y/2,p)%p; if(y&1==0){//That is, y is the base res=res*x%p; } return res; }
.
.
The value of a can be reduced by this formula
int PowerMod(int a,int b,int c){ int res=1; a=a%c; while(b>0){ if(b%2==1){//When b is odd res = res * a% c; } b/=2; a=a*a%c; } return res; }
.Quick Power Baidu Library: Click to open the link