CSES - Aalto Competitive Programming 2024 - wk5 - Mon - Results
Submission details
Task:Sum of ones
Sender:aalto2024e_007
Submission time:2024-09-30 17:49:53 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'long long int func(long long int)':
input/code.cpp:27:8: error: redeclaration of 'long long int m'
   27 |     ll m = getmostBit(n, m);
      |        ^
input/code.cpp:26:8: note: 'long long int m' previously declared here
   26 |     ll m = getnextBit(n);
      |        ^

Code

#include<vector>
#include<iostream>
#define ll long long
using namespace std;
ll getnextBit(ll n){
        ll m = 0;
    while (n > 1) {
        n = n >> 1;
        m+=1;
    }
    return m;
}
ll getmostBit(ll n, ll m){
        ll tmp = 1 << m;
    while (n < tmp) {
        tmp = tmp >> 1;
        --m;
    }
    return m;
}
ll func(ll n){

    if (n == 0)
        return 0;
    
    ll m = getnextBit(n);
    ll m = getmostBit(n, m);
    if (n == (1LL << (m + 1)) - 1)
        return (m + 1) * (1 << m);

    n = n - (1LL << m);
    return (n + 1) + func(n) + m * (1LL << (m - 1));
}
int main(){
    int n;
    cin >> n;
    cout<<func(n)<<endl;
}