알고리즘 & 자료구조/알고리즘

기초수학-연습 문제 풀이 2-2

죽밥죽밥화이팅 2024. 8. 7. 18:23


public class Practice4 {
    final static int mod = (int) 1e9 + 7;

    public static int solution(long n) {
        return (int) (recursion(5, (n + 1) / 2) * recursion(4, n / 2) % mod);
    }

    public static long recursion(long x, long y) {
        if (y == 0) {
            return 1;
        }

        long p = recursion(x, y / 2);
        return p * p * (y % 2 > 0 ? x : 1) % mod;
    }

    public static void main(String[] args) {
        // Test code
        System.out.println(solution(1)); // 5
        System.out.println(solution(2)); // 20
        System.out.println(solution(3)); // 100
        System.out.println(solution(4)); // 400
        System.out.println(solution(50)); // 564908303
    }
}

 

 

 


public class Practice5 {
    static StringBuffer sb;

    public static void solution(int n) {
        sb = new StringBuffer();

        hanoi(n, 1, 2, 3);
        System.out.println(sb);
    }

    public static void hanoi(int n, int start, int mid, int to) {
        if (n == 1) {
            sb.append(start + " " + to + "\n");
            return;
        }

        hanoi(n - 1, start, to, mid);
        sb.append(start + " " + to + "\n");
        hanoi(n - 1, mid, start, to);
    }


    public static void main(String[] args) {
        // Test code
        solution(2);
        System.out.println();
        //1 2
        //1 3
        //2 3

        solution(3);
        System.out.println();
        //1 3
        //1 2
        //3 2
        //1 3
        //2 1
        //2 3
        //1 3

        solution(4);
        //1 2
        //1 3
        //2 3
        //1 2
        //3 1
        //3 2
        //1 2
        //1 3
        //2 3
        //2 1
        //3 1
        //2 3
        //1 2
        //1 3
        //2 3
    }
}