[20250311] BOJ / P3 / 채석장 게임 / 권혁준 #223
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/16899
🧭 풀이 시간
10분
👀 체감 난이도
✏️ 문제 설명
구사과와 큐브러버는 N개의 채석장을 가지고 있다.
i번째 채석장에는 덤프 트럭이 M[i]개 주차되어 있고, 각 덤프 트럭에는 X[i], X[i]+1, ..., X[i] + M[i] - 1개의 돌이 담겨 있다.
두 사람이 턴을 번갈아 가며 게임을 진행하고, 구사과가 선공이다.
각 사람은 자신의 턴에 덤프 트럭을 하나 고르고 그 트럭에서 돌을 1개 이상 제거한다.
더 이상 돌을 제거할 수 없는 사람이 게임을 지게 된다.
둘 다 최선을 다할 때, 누가 이기는지 구해보자.
🔍 풀이 방법
[사용한 알고리즘]
문제를 Nim game으로 환원시킬 수 있다.$\sum(M)$ 개 만큼 있고, 돌이 각각 X[1], X[1] + 1, ..., X[1] + M[1] - 1, X[2], X[2] + 1, ..., X[2] + M[2] - 1, $\cdots$ , X[N], X[N] + 1, ..., X[N] + M[N] - 1개 존재하는 Nim Game과 동일하다.
=> 돌 더미가 총
제거할 수 있는 돌의 개수는 1 이상이므로, 돌의 개수를 x라고 할 때 x의 그런디 수는 g[x] = x이다.
전체 게임의 결과는 모든 돌 더미에 대한 그런디 수의 xor값에 따라 좌우되므로, i번째 채석장에 대한 그런디 수를 빠르게 구할 수 있다면 문제를 해결할 수 있다.
결국 X[i] xor (X[i]+1) xor ... xor (X[i]+M[i]-1) 를 빠르게 구하는 게 목표가 된다.
f(x) = 1부터 x까지 모두 xor한 값이라고 정의하면, f는 매우 간단하게 구할 수 있다.
이제 f를 이용해서 모든 채석장의 그런디 수를 xor하고, 그 값이 0인지 아닌지 확인하면 된다.
⏳ 회고
...