We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

I came up with a nice formula for solving the problem.

We start off with a self-referential formula:

t = money + wrappers
t = floor(n/c) + floor((t-1)/m)

The reason we use t-1 over t is because the wrapper you acquire from your last exchanged chocolate is not part of your available resources.

We then solve for t:

t - floor((t - 1) / m) = floor(n/c)
ceil(t - (t - 1) / m) = floor(n/c)
ceil((mt - t + 1) / m) = floor(n/c)

We can convert this to an inequality. The idea is that mt - t + 1 must be at between floor(n/c)-1 (non-inclusive) and floor(n/c) (inclusive) multiples of m.

@ansonete's solution isn't actually fully correct unless you do the floor of that entire equation, as (floor(n/c) - 1) / (m - 1) represents a possible non-integer value.

I came to the same formula you did using the same principle.

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

I came up with a nice formula for solving the problem.

We start off with a self-referential formula:

The reason we use

`t-1`

over`t`

is because the wrapper you acquire from your last exchanged chocolate is not part of your available resources.We then solve for t:

We can convert this to an inequality. The idea is that

`mt - t + 1`

must be at between`floor(n/c)-1`

(non-inclusive) and`floor(n/c)`

(inclusive) multiples of`m`

.Note there can be multiple values of

`t`

that are within this range. We select the largest, because we want the maximum amount of chocolatesAnother form of this is what @ansonete described below:

@ansonete's solution isn't actually fully correct unless you do the floor of that entire equation, as (floor(n/c) - 1) / (m - 1) represents a possible non-integer value.

I came to the same formula you did using the same principle.

The first line is wrong:

It should be