How to solve this codeforces problem?

Link to the problem is… Problem - C - Codeforces

Take a look at this

https://discuss.codechef.com/questions/76409/can-anyone-explain-logic-to-solve-this

1 Like

in this question if u sum all the numbers then a number will formed that has x no of digits =1 set bits means that ans=x…
but the problem is no which we get after sum of all the no can be very large beacuse 2^(n) n can be 10000…
so what we are going to do is this we make a array
arr[100000]={0};
ex–the pow of 2’s are—>3,3,5

if no is 2^(3) we will set the arr[3]++; -->arr[3]=1;
if this no again comes arr[3]++ ;----->arr[3]=2 means 2^(3) comes two times…
now 2^(5) arr[5]++;

now we have array 1-index based 0 0 2 0 1
now see here if u have 2^(3) two times u can repersent it 2.(2^(3))—>2^(4)
if arr[i] is even
then: arr[i+1]+=arr[i]/2;
arr[i]=0;

now think othe thing if arr[i] is odd
means 2^(3) three times sum is 3.2^(3)—>2.2^(3)+2^(3)—>2^(4)+2^(3) ;
means current bit is also set and increse next bit value to arr[i]/2;
now we can say that
if arr[i] is odd
then:: arr[i+1]+=arr[i]/2;
arr[i]=1;

we are doing a simple work repersenting the binary number manually
at last in our array each value of array either have 0 or 1 no other value ,means we have succcessfully repersented the sum of powers of two(as in question) as a array from left to right

so simply calculte number of set value of the array :: arr[i]==1;
thats our ans==…
now u can done the example by yourself this is the main hurdle in this question that
how to repersent large sum of the numbers which is in form of 2^(x)…
i have put my best here…i guess u will definetly understand this …
happy to help…if u find any difficulty although.

1 Like

got the idea thanks :slight_smile: