Untitled

 avatar
user_3285173
plain_text
a month ago
716 B
4
Indexable
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
    ll n;
    cin>>n;
    ll count=0;
    ll bitc = 1;
    ll temp = n;
    while(temp>1){
        temp/=2;
        bitc++;
    }
    n+=1;
    for(ll i=0;i<bitc;i++){
        if(n<(1LL<<(i+1))){
            count-=(1LL<<(i+1))/2  - n;
        }
        else if(n%(1LL<<(i+1))==0){
            count+=(n/(1LL<<(i+1)))*(1LL<<i);
        }
        else if((n%(1LL<<(i+1)))>(1LL<<i)){
            count+=(n/(1LL<<(i+1)))*(1LL<<i) + n%(1LL<<(i+1)) - (1LL<<i);
        }
        else{
            count+=(n/(1LL<<(i+1)))*(1LL<<i);
        }

    }
    
    cout<<count;

    return 0;

        
   
}

Editor is loading...
Leave a Comment