1843C - Sum in Binary Tree - CodeForces Solution


bitmasks trees

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline 

for _ in range(int(input())):
    n = int(input())
    ans = 0
    while (n>0):
        ans += n
        n //= 2
    print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
const int mod = 1e9 + 7;

void solve(){
    ll n;cin>>n;
    ll cnt = 1;
    ll temp = n;
    while(n != 1) { 
    	cnt += (n%2 != 0);
    	n /= 2;
    }
    cout << 2*temp - cnt << "\n";
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll TC;cin>>TC;
    while(TC--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!