fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static long binPow(long a, long b, int m) {
  5. a %= m;
  6. if (b == 0) return 1;
  7. else if (b % 2 == 0) {
  8. return binPow((a * a) % m, b / 2, m);
  9. }
  10. else return (a * binPow(a, b - 1, m)) % m;
  11. }
  12. public static void main(String[] args) {
  13. Scanner scanner = new Scanner(System.in);
  14. long a = scanner.nextLong();
  15. long b = scanner.nextLong();
  16. int m = scanner.nextInt();
  17. System.out.println(binPow(a, b, m));
  18. }
  19. }
Success #stdin #stdout 0.06s 2184192KB
stdin
2 10 1000
stdout
24