Search

1의 개수 세기

Created
2021/03/23 01:58
문제 번호
9527
카테고리
수학
비트마스킹

Memo

Code

제출 날짜

@3/3/2020

메모리

2168 KB

시간

0 ms
#include <iostream> #include <cmath> #define lld long long #define endl "\n" int getLengthOfNumber(lld number) { int count = 0; while(number >0) { number = number >> 1; count++; } return count; } lld solution(lld number) { if(number == 0){ return 0; } int lengthOfNumber = getLengthOfNumber(number); lld subtractNumber = (lld)pow(2,lengthOfNumber - 1); lld countOfHighestPlaceOnes= number - subtractNumber + 1; lld countOfZeroToSubtractNumber = (lengthOfNumber- 1) * (lld)pow(2, lengthOfNumber - 2); lld remainNumber = solution(number - subtractNumber); return countOfHighestPlaceOnes + countOfZeroToSubtractNumber+ remainNumber; } int main() { lld A; lld B; lld numberOfOnes; std::cin >> A >> B; numberOfOnes = solution(B)- solution(A - 1); std::cout << numberOfOnes; return 0; }
C++