Calculate a^p using binary exponentiation [Binary-Exponentiation](https://cp-algorithms.com/algebra/binary-exp.html) @param a the base for exponentiation @param p the exponent - must be greater than 0 @return a^p
(int a, int p)
| 13 | * @return a^p |
| 14 | */ |
| 15 | public static int binPow(int a, int p) { |
| 16 | int res = 1; |
| 17 | while (p > 0) { |
| 18 | if ((p & 1) == 1) { |
| 19 | res = res * a; |
| 20 | } |
| 21 | a = a * a; |
| 22 | p >>>= 1; |
| 23 | } |
| 24 | return res; |
| 25 | } |
| 26 | } |
no outgoing calls