Skip to content

Commit cd44ef3

Browse files
authored
Merge pull request #1505 from AlgorithmWithGod/0224LJH
[20251125] BOJ / P5 / 플레이리스트 / 이종환
2 parents a9d372e + 880aff9 commit cd44ef3

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
10+
// dp[i][j]: i번째까지 k개의 숫자로 채운 경우
11+
// -> dp[i]
12+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
static long[][] dp;
14+
static int songCnt, minDis, totalLen;
15+
16+
static final long DIVISOR = 1_000_000_007l;
17+
18+
public static void main(String[] args) throws IOException {
19+
20+
init();
21+
process();
22+
print();
23+
24+
}
25+
26+
private static void init() throws IOException{
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
songCnt = Integer.parseInt(st.nextToken());
29+
minDis = Integer.parseInt(st.nextToken());
30+
totalLen = Integer.parseInt(st.nextToken());
31+
dp = new long[totalLen+1][songCnt+1];
32+
dp[1][1] = songCnt;
33+
}
34+
35+
private static void process() throws IOException {
36+
for (int i = 2; i <= totalLen; i++) {
37+
if (i <= minDis) {
38+
// 아직 총 길이가 간격보다 적은 경우.
39+
// 그냥 i개의 노래로 줄세우는 것과 동일하다.
40+
dp[i][i] = (dp[i-1][i-1] * (songCnt-i+1))%DIVISOR;
41+
continue;
42+
}
43+
44+
//적어도 minDis개 만큼의 부분배열에서는 겹치는게 없다는 것을 고려하자
45+
for (int j = 1; j <= songCnt; j++) {
46+
if (dp[i-1][j] == 0) continue;
47+
48+
//이전에 있는 것과 동일한 것을 선택하는 경우
49+
//이 경우, curSongCnt - minDis개의 선택지가 존재하게 된다.
50+
dp[i][j] += dp[i-1][j] * (j - minDis);
51+
dp[i][j] %= DIVISOR;
52+
53+
// 이전과 다른 것을 선택하는 경우
54+
if (j == songCnt) continue;
55+
dp[i][j+1] += dp[i-1][j] * (songCnt - j);
56+
dp[i][j+1] %= DIVISOR;
57+
}
58+
}
59+
60+
}
61+
62+
63+
64+
65+
66+
67+
68+
69+
private static void print() {
70+
System.out.println(dp[totalLen][songCnt]);
71+
}
72+
73+
}
74+
```

0 commit comments

Comments
 (0)