refactor: update FastPower
This commit is contained in:
parent
fbffeb658c
commit
1041b02e6b
@ -1,4 +1,4 @@
|
|||||||
package src.main.java.com.Others;
|
package src.main.java.com.others;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
@ -7,14 +7,13 @@ import java.math.BigInteger;
|
|||||||
* FastPower aims to calculate quickly in this circumstances with time complexity O(log k),
|
* FastPower aims to calculate quickly in this circumstances with time complexity O(log k),
|
||||||
* where k is the index.
|
* where k is the index.
|
||||||
*
|
*
|
||||||
* @author DDullahan
|
|
||||||
*/
|
*/
|
||||||
public class FastPower {
|
public class FastPower {
|
||||||
public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) {
|
public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) {
|
||||||
BigInteger ans = BigInteger.ONE;
|
BigInteger ans = BigInteger.ONE;
|
||||||
while (!k.equals(BigInteger.ZERO)) {
|
while (!k.equals(BigInteger.ZERO)) {
|
||||||
int odd = k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO);
|
int odd = k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO);
|
||||||
if(odd > 0){
|
if (odd > 0) {
|
||||||
ans = ans.multiply(n).mod(mod);
|
ans = ans.multiply(n).mod(mod);
|
||||||
}
|
}
|
||||||
k = k.divide(BigInteger.valueOf(2));
|
k = k.divide(BigInteger.valueOf(2));
|
||||||
|
Loading…
Reference in New Issue
Block a user