[BOJ][2225] 합분해
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/2225
2. 사용 알고리즘
DP
3. 풀이
d[k][n] : 0 ~ N 정수 k개를 더했을 때 합이 n이 되는 경우의 수
-
k번째 수가 0인 경우의 수 : d[k - 1][n - 0]
-
k번째 수가 1인 경우의 수 : d[k - 1][n - 1]
-
…
-
k번째 수가 n인 경우의 수 : d[k - 1][n - n]
∴ d[k][n] = d[k - 1][n - 0] + d[k - 1][n - 1] + … + d[k - 1][n - n];
4. 소스 코드
4-1. C++
4-1-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/2225.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
long long d[203][203];
long long solve(int k, int n) {
if (k == 1) return 1LL;
if (d[k][n] > 0) return d[k][n];
for (int i = 0; i <= n; ++i) {
d[k][n] += solve(k - 1, i);
d[k][n] %= 1000000000;
}
return d[k][n];
}
int main(void) {
ios_base::sync_with_stdio(false);
int n, k; cin >> n >> k;
cout << solve(k, n) << "\n";
return 0;
}
4-1-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/2225_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
long long d[203][203];
int main(void) {
ios_base::sync_with_stdio(false);
int n, k; cin >> n >> k;
d[0][0] = 1LL;
for (int i = 1; i <= k; ++i) {
for (int j = 0; j <= n; ++j) {
for (int l = 0; l <= j; ++l) {
d[i][j] += d[i - 1][j - l];
d[i][j] %= 1000000000;
}
}
}
cout << d[k][n] << "\n";
return 0;
}
4-2. JAVA
4-2-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/2225.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[][] d = new long[203][203];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
System.out.println(solve(k, n));
}
public static Long solve(int k, int n) {
if (k == 1) return 1L;
if (d[k][n] > 0) return d[k][n];
for (int i = 0; i <= n; ++i) {
d[k][n] += solve(k - 1, i);
d[k][n] %= 1000000000;
}
return d[k][n];
}
}
4-2-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/2225_2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[][] d = new long[203][203];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
d[0][0] = 1L;
for (int i = 1; i <= k; ++i) {
for (int j = 0; j <= n; ++j) {
for (int l = 0; l <= j; ++l) {
d[i][j] += d[i - 1][j - l];
d[i][j] %= 1000000000;
}
}
}
System.out.println(d[k][n]);
}
}
Leave a comment