-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1234.java
More file actions
68 lines (53 loc) · 2.29 KB
/
Copy pathBOJ_1234.java
File metadata and controls
68 lines (53 loc) · 2.29 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.util.*;
public class BOJ_1234 {
static long[][][][] color = new long[11][101][101][101];
static long[] factorial = new long[11];
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 r = Integer.parseInt(st.nextToken());
int g = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if((1 + N) * N / 2 > r + g + b) { // 칠해야 할 개수보다 색이 부족하면 0 출력
System.out.println(0);
System.exit(0);
}
System.out.println(solve(N, r, g, b));
}
static long solve(int N, int r, int g, int b) {
// Base Case
if (r < 0 || g < 0 || b < 0) return 0;
if (N <= 0) return 1;
if (color[N][r][g][b] != 0) return color[N][r][g][b];
// 1가지 색상을 사용할 경우
color[N][r][g][b] += solve(N-1, r - N, g, b);
color[N][r][g][b] += solve(N-1, r, g - N, b);
color[N][r][g][b] += solve(N-1, r, g, b - N);
// 2가지 색상을 사용할 경우 -> N이 2의 배수인 경우만 가능
if (N % 2 == 0) {
int count = N / 2; // 각 색상을 사용할 개수
long time = factorial(N) / (factorial(count) * factorial(N-count));
color[N][r][g][b] += solve(N-1, r-count, g-count, b) * time;
color[N][r][g][b] += solve(N-1, r, g-count, b-count) * time;
color[N][r][g][b] += solve(N-1, r-count, g, b-count) * time;
}
// 3가지 색상을 사용할 경우 -> N이 3의 배수인 경우만 가능
if (N % 3 == 0) {
int count = N / 3;
color[N][r][g][b] += solve(N-1, r-count, g-count, b-count)
* factorial(N) / (factorial(count) * factorial(count) * factorial(N-2*count));
}
return color[N][r][g][b];
}
static long factorial(int number) {
if (number == 0 || number == 1) {
return 1;
}
if (factorial[number] != 0) {
return factorial[number];
}
return number * factorial(number - 1);
}
}