📜  a ^ b java 代码示例

📅  最后修改于: 2022-03-11 14:52:07.708000             🧑  作者: Mango

代码示例1
/*
  Given two non-negative integers a and b. Let's calculate a^b.
  If the result is too large, divide the remainder by 10^9 + 7.
*/
import java.util.Scanner;

public class EXC {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long res = 1;
        long mod = (long)1e9 + 7;
        while(b > 0){
          if ( b % 2 != 0) res = res * a % mod;
          a = a * a % mod;
          b >>= 1;
        }
        System.out.println(res);
    }
}