Skip to content

Commit

Permalink
24-07-23 1의 개수 세기
Browse files Browse the repository at this point in the history
  • Loading branch information
makehard23 committed Jul 23, 2024
1 parent 1cc6092 commit 9255a94
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
| 13차시| 2024.05.21 | LIS | [가장 긴 증가하는 부분 수열 2](https://www.acmicpc.net/problem/12015) | - |
| 15차시| 2024.06.01 | 기하 | [다각형의 면적](https://www.acmicpc.net/problem/2166) | - |
| 16차시 | 2024.06.04 | MST | [별자리 만들기](https://www.acmicpc.net/problem/4386) | - |
| 20차시 | 2024.07.22 | LIS | [가장 긴 증가하는 부분 수열 5](https://www.acmicpc.net/problem/4386) | - |
| 20차시 | 2024.07.22 | LIS | [가장 긴 증가하는 부분 수열 5](https://www.acmicpc.net/problem/14003) | - |
| 21차시 | 2024.07.23 | 수학 | [1의 개수 세기](https://www.acmicpc.net/problem/9527) | - |
---
48 changes: 48 additions & 0 deletions yuyu0830/수학/9527.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <math.h>

using namespace std;

typedef unsigned long long int ll;

ll arr[10000] = {0, };
int cnt = 1;

// 0 ~ t 까지의 1의 개수를 반환
ll f(ll t) {
int ptLog = cnt, tmp = 0;
ll pt = pow(2, ptLog - 1), ans = 0;

while (pt) {
if (t & pt) {
ans += arr[ptLog - 1] + 1;
ans += pt * tmp++;
}
pt >>= 1;
ptLog--;
}

return ans;
}

int main() {
ll a, b, tmp = 1; cin >> a >> b;

// b까지 arr 배열 누적합 연산
while (b > tmp) {
arr[cnt] = (arr[cnt - 1] * 2) + tmp;
tmp *= 2; cnt++;
}

ll ZeroToAVal = f(a); // 0 ~ a - 1 까지의 1의 개수
ll bVal = f(b); // 0 ~ b 까지의 1의 개수
ll aVal = 0; // a 의 1의 개수

ll pt = pow(2, cnt);
while (pt) {
if (a & pt) aVal++;
pt >>= 1;
}

printf("%lld\n", bVal - ZeroToAVal + aVal);
}

0 comments on commit 9255a94

Please sign in to comment.