"topic link": https://nanti.jisuanke.com/t/25087
"Topic description":
The integer power of a number is often used in our calculations, but how can we work out the result in the time of O (log (n)).
The code in the Code box is an implementation, please analyze and fill in the missing code to find the result of X^y mod p.
Import java.util.*;
public class Main {
public static int pw (int x, int y, int p) {
if (y = = 0) {
return 1;
}
int res =//* Fill in the necessary code here */;
if ((Y & 1)!= 0) {
res = res * x% P;
}
return res;
}
public static void Main (string[] args) {
Scanner cin = new Scanner (system.in);
int x = Cin.nextint ();
int y = Cin.nextint ();
int p = cin.nextint ();
System.out.println (PW (x, Y, p));
}
}
"Answer": PW (x, Y/2, p) * PW (x, Y/2, p)% p
"Resolution":
"Code":
Import java.util.*;
public class Main {public
static int pw (int x, int y, int p) {
if (y = = 0) {return
1;
}
int res = PW (x, Y/2, p) * PW (x, Y/2, p)% p/* fill in the necessary code here * * *;
if ((Y & 1)!= 0) {
res = res * x% P;
}
return res;
}
public static void Main (string[] args) {
Scanner cin = new Scanner (system.in);
int x = Cin.nextint ();
int y = Cin.nextint ();
int p = cin.nextint ();
System.out.println (PW (x, Y, p));
}