[20250707] BOJ / G2 / 1의 개수 세기 / 이강현 #421
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/9527
🧭 풀이 시간
120분
👀 체감 난이도
✏️ 문제 설명
자연수 A 부터 B 사이에 있는 모든 수를 이진수로 나타냈을 때, 생길 수 있는 1의 개수를 모두 구하기
🔍 풀이 방법
분할 정복, 수의 주기성
첫번째 비트: 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 -> 주기 2
두번째 비트: 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1 -> 주기 4
세번째 비트: 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1 -> 주기 8
네번째 비트: 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1 -> 주기 16
...
비트마다 주기성을 가지고 또 하나의 주기에서 앞쪽 반절은 0, 뒤쪽 반절은 1이므로
0부터 B까지 얻을 수 있는 1의 개수를 계산하고 거기에 0부터 A-1까지의 1의 개수를 구한 것을 빼면 정답을 얻을 수 있다.
⏳ 회고
비트 단위로 쪼갤 생각을 못했다.
기존에는 0 ~ 1, 10 ~ 11, 100 ~ 111 이런식으로 구간을 나누어서 혼란스러웠는데
좋은 풀이였던 것 같다.