백준

백준 1629 - Java

으엉어엉 2024. 11. 23. 11:57
728x90

 

 

 

import java.util.Scanner;

public class BJ1629 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        sc.close();

        System.out.println(modularExponentiation(A, B, C));
    }

    private static long modularExponentiation(long base, long exp, long mod) {
        if (exp == 1) {
            return base % mod;
        }

        long half = modularExponentiation(base, exp / 2, mod);
        half = (half * half) % mod;

        if (exp % 2 != 0) {
            half = (half * base) % mod;
        }

        return half;
    }
}
728x90

'백준' 카테고리의 다른 글

백준 1916 - Java  (0) 2024.11.25
백준 1991 - Java  (0) 2024.11.24
백준 16953 - Java  (0) 2024.11.22
백준 15663 - Java  (0) 2024.11.21
백준 11725 - Java  (1) 2024.11.20