https://www.acmicpc.net/problem/16195
16195번: 1, 2, 3 더하기 9
각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 1,000,000,009로 나눈 나머지를 출력한다. 단, 사용한 수의 개수는 m개 이하 이어야 한다.
www.acmicpc.net
package BOJ.dp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_16195 {
private static final int LIMIT = 1_000_000_009;
private static final int NOT_VALID = -1;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
StringTokenizer st = new StringTokenizer(br.readLine());
int testCnt = stoi.apply(st.nextToken());
int[][] dp = new int[1_000+1][1_000+1];
for(int i = 0 ; i <= 1_000 ; i++){
Arrays.fill(dp[i],NOT_VALID);
}
for(int t = 0 ; t < testCnt ; t++){
st = new StringTokenizer(br.readLine());
int n = stoi.apply(st.nextToken());
int m = stoi.apply(st.nextToken());
int result = cal(n,m,dp);
System.out.println(result);
}
}
private static int cal(int n, int depth,int[][] dp) {
if(n == 0){
return 1;
}
if(depth <= 0){
return 0;
}
if(dp[n][depth] != NOT_VALID){
return dp[n][depth];
}
int result = 0;
for(int i = 1 ; i <= 3 ; i++){
if(n-i >= 0){
result += cal(n-i,depth-1,dp);
result %= LIMIT;
}
}
dp[n][depth] = result;
return result;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 중앙값 구하기 (JAVA) (0) | 2023.03.18 |
---|---|
백준 24390번 또 전자레인지야? (JAVA) (0) | 2023.03.17 |
백준 히오스 프로게이머 (JAVA) (0) | 2023.03.15 |
프로그래머스 모스부호 (JAVA) (0) | 2023.03.14 |
프로그래머스 소인수분해 (JAVA) (0) | 2023.03.13 |