Skip to content

Commit bef7bb9

Browse files
committed
[Gold III] Title: 훈련소로 가는 날, Time: 348 ms, Memory: 86512 KB -BaekjoonHub
1 parent 5b16e87 commit bef7bb9

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Gold III] 훈련소로 가는 날 - 18858
2+
3+
[문제 링크](https://www.acmicpc.net/problem/18858)
4+
5+
### 성능 요약
6+
7+
메모리: 86512 KB, 시간: 348 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2026년 2월 22일 16:38:37
16+
17+
### 문제 설명
18+
19+
<p style="text-align: justify;">훈련소로 가는 날 욱제는 문득 떠올렸다. 훈련소가 논산에 있는 이유는 무엇일까? 왜 why?</p>
20+
21+
<p>그것은 바로…</p>
22+
23+
<p> </p>
24+
25+
<p> </p>
26+
27+
<p> </p>
28+
29+
<p> </p>
30+
31+
<p> </p>
32+
33+
<p> </p>
34+
35+
<p>논산(non-산)은 산이 아니기 때문이다. 길이가 3인 수열 a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>가 산임은 a<sub>1</sub> < a<sub>2</sub> > a<sub>3</sub>임을 의미한다. 어떤 수열이 논산임은 수열의 인접한 세 항이 산인 경우가 없음을 의미한다. 다시 말해, 길이 N의 수열 a에 대해 2 ≤ i < N 이고 a<sub>i-1</sub> < a<sub>i</sub> > a<sub>i+1</sub>인 경우가 없다.</p>
36+
37+
<p>논산인 수열이 몇 개가 있는지 알아보자.</p>
38+
39+
### 입력
40+
41+
<p>첫째 줄에 N과 M이 주어진다.</p>
42+
43+
### 출력
44+
45+
<p>1 이상 M 이하의 정수로 이루어진 길이 N의 수열 중 논산인 것의 개수를 998,244,353으로 나눈 나머지를 출력한다.</p>
46+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Foundation
2+
3+
let MOD = 998244353
4+
5+
let NM = readLine()!.split(separator: " ").map { Int($0)! },
6+
N = NM[0],
7+
M = NM[1]
8+
9+
if N == 1 {
10+
print(M)
11+
exit(0)
12+
}
13+
14+
var dp = Array(
15+
repeating: Array(
16+
repeating: Array(repeating: 0, count: 2),
17+
count: M + 1
18+
),
19+
count: N + 1
20+
)
21+
22+
for v in 1...M {
23+
dp[1][v][0] = 1
24+
}
25+
26+
for i in 2...N {
27+
for u in 1...M {
28+
for s in 0...1 {
29+
let current = dp[i-1][u][s]
30+
if current == 0 { continue }
31+
32+
for v in 1...M {
33+
if v > u {
34+
dp[i][v][1] = (dp[i][v][1] + current) % MOD
35+
} else if v == u {
36+
dp[i][v][0] = (dp[i][v][0] + current) % MOD
37+
} else {
38+
if s == 0 {
39+
dp[i][v][0] = (dp[i][v][0] + current) % MOD
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
var answer = 0
48+
for v in 1...M {
49+
for s in 0...1 {
50+
answer = (answer + dp[N][v][s]) % MOD
51+
}
52+
}
53+
54+
print(answer)

0 commit comments

Comments
 (0)