Problem 617B

Правка en7, от ebanner, 2016-11-27 21:15:47

This is a post describing my solution for 617B - Chocolate.

For this problem, the key is viewing the act of breaking the candy bar as a sequential process with a number of decisions at each stage. Once we know how many decisions there are at each stage, we can apply the rule of product and multiply these numbers to arrive at the total number of possible outcomes.

The sequential process proceeds as follows:

  1. Start at the left side of the candy bar
  2. Keep marching until we get between two nuts
  3. Make a break somewhere between these two nuts
  4. Go back to step 2

In step 3, we can make multiple decisions. If there are n zeros between the two nuts, then we can make a break so as to give n zeros to the left nut and 0 zeros to the right nut, or we can make a break so as to give n-1 zeros to the left nut and 1 zeros to the right nut, and so on. In total, we have n+1 decisions for this particular break.

To help visualize this process, consider the following diagram on the input sequence 10101:

We first consider nuts 1 and 2 and see that there is only one zero in between them. Hence we can make a total of 2 different breaks, as pictured by the two branches. After making this first break, we proceed to nuts 2 and 3. Once again, there is only a single zero between these two nuts; hence the number of breaks we can make here is 2. We get the total number of breaks by multiplying these numbers for a total of 2*2 = 4 possible breaks.

One edge case that is captured in my algorithm, but is nonetheless useful to consider are when there are leading and trailing zeros. Note here there is only one legal break we can make. For leading zeros, the only legal break point is before the first zero. For the trailing zeros, the only legal break point is after the last zero. This is because any other break would result in a piece of chocolate with zero nuts. As a preprocessing step, I simply stripped leading and trailing zeros, but one can equivalently keep them and simply include them in the product as multiplication by 1 leaves the result unchanged.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en8 Английский ebanner 2016-11-27 21:35:36 798 Tiny change: 'e two nuts; hence the' - (published)
en7 Английский ebanner 2016-11-27 21:15:47 1063 Tiny change: ' candy bar and march right\n2. Keep ' -
en6 Английский ebanner 2016-11-27 21:03:32 28
en5 Английский ebanner 2016-11-27 21:02:48 633
en4 Английский ebanner 2016-11-27 20:14:55 159
en3 Английский ebanner 2016-11-27 20:12:12 730
en2 Английский ebanner 2016-11-27 20:06:29 8 Tiny change: 'YD2/pub?w=960&h=720)' -
en1 Английский ebanner 2016-11-26 22:55:10 507 Initial revision (saved to drafts)