【題解】CSES 1146 Counting Bits

【題目敘述】https://cses.fi/problemset/task/1146/

#include <iostream>
using namespace std;
 
long long n, ans;
 
int main() {
    cin >> n;
    n++;
    for (int i = 60; i >= 0; i--){
        ans += n/(1LL<<(i+1))*(1LL<<(i));
        ans += max(0LL, n%(1LL<<(i+1))-(1LL<<i));
    }
    cout << ans << "\n";
}
分享本文 Share with friends