https://www.acmicpc.net/problem/17291
17291번: 새끼치기
실험실에서 새로운 종의 벌레 한 마리가 탄생하였다. 벌레는 스스로 분열하며, 분열하면 자기 자신과 같은 벌레를 한 마리 만들어 내게 된다. 벌레가 분열하는 규칙은 다음과 같다. 벌레는 기준
www.acmicpc.net
package BOJ.dp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.function.Function;
public class BOJ_17291 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(br.readLine());
int[] dp = new int[n+1];
dp[1] = 1;
cal(n,dp);
// System.out.println(Arrays.toString(dp));
int result = 0;
int length = 3;
if(n % 2 == 0){
length = 2;
}
for(int i = n ; i >= Math.max(0,n - length) ; i--){
result += dp[i];
}
System.out.println(result);
}
private static int cal(int num, int[] dp){
if(num <= 0){
return 0;
}
if(dp[num] != 0){
return dp[num];
}
int length = 3;
if(num % 2 == 0){
length = 4;
}
int result = 0;
for(int i = 1 ; i <= length ; i++){
result += cal(num - i,dp);
}
dp[num] = result;
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 1063번 킹 (JAVA) (1) | 2023.02.02 |
---|---|
백준 25644번 최대 상승 (JAVA) (0) | 2023.02.01 |
백준 16918번 붐버맨 (JAVA) (0) | 2023.01.30 |
백준 26169번 세 번 이내에 사과를 먹자 (JAVA) (0) | 2023.01.29 |
백준 15991번 1,2,3 더하기 6 (JAVA) (0) | 2023.01.28 |