[20250822] BOJ / P5 / (Relatively) Prime / 권혁준 #714
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/34151
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
소수 p와 정수 n, m가 주어지면, gcd(a,b) = p를 만족하는 a, b에 대해 gcd(a^n, b^m)으로 가능한 서로 다른 값들의 합을 구해보자.
🔍 풀이 방법
일단 n >= m이라고 가정한다.
gcd(a,b) = p라는 것은, gcd(x,y) = 1인 어떤 x, y에 대해 a = xp, b = yp 꼴이라는 의미다.
그래서 gcd(a^n, b^m) = gcd((xp)^n, (yp)^m)이고, 이 값은 일단 p^m을 갖고 간다.
gcd(x,y) = 1이어야 하기 떄문에, 위 값으로 가능한 다른 값은 y = p^k 꼴인 경우 뿐이다.
따라서 최종 답은 p^m + p^2m + ... + p^n 꼴로 나오게 된다.
등비수열의 합 공식을 이용해서 답을 구해줬다.
거듭제곱을 빠르게 구해야 하므로 분할 정복을 사용했고, 등비수열 합 공식에서 나눗셈에 대한 모듈로 처리를 위해 페르마의 소정리를 이용했다.
⏳ 회고
좋은아침