[20251024] BOJ / P4 / 홍준이의 교집합 / 권혁준 #1219
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/12992
🧭 풀이 시간
30분
👀 체감 난이도
✏️ 문제 설명
N개의 1차원 구간이 있다.
i번째 구간은 L[i]부터 R[i]까지 포함하는 구간이고, 구간의 길이는 R[i] - L[i] + 1이다.
서로 다른 구간들을 k개 골랐을 때, 가능한 모든 경우의 수에 대한 k개 구간의 교집합의 길이 합을 구해보자.
🔍 풀이 방법
구간을 고르는 순서는 상관 없다. -> 조합을 써야겠다.
구간의 시작과 끝을 별개의 event로 쪼개고 정렬 후 스위핑 하면서 prev, cur 좌표와 겹치는 개수 cnt를 관리했다.
이전 cnt가 k 이상이었다면?
답에 (cur-prev) * comb[cnt][k] 만큼 더해주기
C[n] = comb[n][k]라고 하면,
C[k+x] = C[k+x-1] * (k+x) / x 이고, 페르마의 소정리에 의해
C[k+x] = (C[k+x-1] * (k+x) * x^(MOD-2)) % MOD 가 되어서 O(NlogMOD)에 배열 C를 모두 채울 수 있다.
⏳ 회고
문제 지문이 무섭게 생겼는데 속은 그렇지 않음