Can someone help me how to solve Ameesha loves candy problem? here is what i tried https://www.codechef.com/viewsolution/45355161

x

2x , 2x - 1 (after 1 month)

2 * 2x , 2 * 2x - 1 , 2 * (2x - 1) , 2 * (2x - 1) - 1 (after 2 month)

222x , 222x - 1 , 2 * (22x - 1) , 2 * (22x - 1) - 1âŚ

.

.

.

terms = 1 << k; (total terms after k months)

P = (terms * 2 ^ (k) * x - sum(0 to terms - 1)) / (1 << (k- 1))

You can also do this by solving the following recurrence.

\mathbb{E}[X_i] is the Expected number of candies after i months}

\mathbb{P}(I) is the probability that magic box eats one of the candies.

Solving this recurrence will get you the result I mentioned above.

this is exactly what I didâŚ still got it wrong CodeChef: Practical coding for everyone

any ideas? please tell me if you get this.

You did not divide the total candies by 2 ^ (k - 1).

Also consider the base case 0 when x = 0.

Please share the solved recurrence for better understanding along with this.

Now itâs trivial to see that \mathbb{E}[X_0]=X

On adding all the above equations.

Now since we are assured that in the K+1 th month magic box doesnât eat the extra candy so we can simply double the answer we obtained after K months. i.e.

Thanks a lot brother, cubefreak777.

Can you pls suggest more problems like it ?

Or, is there any specific resources to be better at âExpected numberâ problems.

Top Coder has a great collection of probability and expectation value problems. Almost every SRM they conduct has an expected value problem.

You can check the official editorial of this question from codeforces named as **Nastya and a** **Wardrobe** and here is the link.

except the last +1 i can understand the equation. where does this +1 come from, they say last k+1th month should not be considered but why did we do +1 for that?

Iâve solved entire recurrence here check it.

i understood the derivation brother but if i want to instinctively get a logic, i cant understand why +1 comes. so in simple terms, i understand that for each month it gets doubled so after k+1 months its value will be 2^(k+1) * x and then because the wardrobe eats 1 candy after each month , after k+1 months the loss would be 2^k (after 1st month loss is 2^0, after 2nd loss is 2^1, after k+1 months loss is 2^k). Till this its fine. but here since we also counted loss for last month, we need to again nullify the loss separately. but how is it 1 ?

I donât understand this. I mean after each month he consumes one candy so how are these powers of 2 showing up ?

Also I donât get where have you considered the notion of probability in this intuitive approach ?

thanks, got it now.

x0 = initial value ,i have taken 4 here

```
4
/ \
7 8
/ \ / \
13 14 15 16
```

x = sum of last level

k = total number of levels

answer = 2 * x / number_of_nodes at last level

ans = 2 * x/ pow(2, k - 1)

now find left which is 13 here let say âaâ and right say âbâ

the x = (a + b) * (b - a + 1)/2

is this correct ?

please check it

I think I am wrong about the logic of 2^k and +1.

Can you please tell if the formula 2^(k+1)*x - 2^(k) + 1 can be intuitivly derived. Like 2^(k+1)*x comes logically because after k+1 months it got x*2^(k+1). Similarly is it possible to tell for other 2 terms because it is difficult to solve recurrence relations during contest and it does not come intuitivly

This is the correct answer in itâs unsimplified form from the approach youâre trying. I think your answer is somewhat similar but not exactly correct.

Sorry but I donât really think thereâs an entirely intuitive way to get around it but you can try making the tree of all possible values and then take the weighted average if the leaf nodes. That will also get you the same answer. That approach is more easier to come up with if youâre not good with solving recurrence relations.