Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.07.13 - [BOJ] 15990. 1, 2, 3 더하기 5 #127

Closed
suhyunsim opened this issue Jul 13, 2021 · 0 comments
Closed

21.07.13 - [BOJ] 15990. 1, 2, 3 더하기 5 #127

suhyunsim opened this issue Jul 13, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jul 13, 2021

문제

핵심 아이디어

  • 같은 수를 두 번 이상 연속해서 사용하면 안됨
  • D[i][j] = i를 1, 2, 3의 합으로 나타내는 방법의 수, 마지막에 사용한 수는 j
    • D[i][1] = D[i - 1][2] + D[i - 1][3]
    • D[i][2] = D[i - 2][1] + D[i - 2][3]
    • D[i][3] = D[i - 3][1] + D[i - 3][2]
  • 정답: D[n][1] + D[n][2] + D[n][3]
  • 초기화도 다르게 -> D[1][1], D[2][2], D[3][3]은 각각 1

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q15990;

import java.util.Scanner;

public class Main {
    static final int LIMIT = 100000;
    static final long MOD = 1000000009L;
    static long[][] d = new long[LIMIT + 1][4];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 1; i <= LIMIT; i++) {
            if (i - 1 >= 0) {
                d[i][1] = d[i - 1][2] + d[i - 1][3];
                if (i == 1) {
                    d[i][1] = 1;
                }
            }
            if (i - 2 >= 0) {
                d[i][2] = d[i -2][1] + d[i - 2][3];
                if (i == 2) {
                    d[i][2] = 1;
                }
            }
            if (i - 3 >= 0) {
                d[i][3] = d[i -3][1] + d[i - 3][2];
                if (i == 3) {
                    d[i][3] = 1;
                }
            }
            d[i][1] %= MOD;
            d[i][2] %= MOD;
            d[i][3] %= MOD;
        }
        while (t-- > 0) {
            int n = sc.nextInt();
            System.out.println((d[n][1] + d[n][2] + d[n][3]) % MOD);
        }
    }
}
@suhyunsim suhyunsim added 실버 BOJ - 실버 DP 다이나믹 프로그래밍 labels Jul 13, 2021
@suhyunsim suhyunsim added this to the 7월 3주 차 milestone Jul 13, 2021
@suhyunsim suhyunsim self-assigned this Jul 13, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DP 다이나믹 프로그래밍 성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant