Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 23. Two sequences of steps are different if their steps differ in order or kind. In how many ways can the counter reach exactly 23?

  1. A. 23
  2. B. 42727
  3. C. 85455
  4. D. 74

Correct answer: C. 85455

Count with ways[y] += ways[x] for each allowed step from x, in increasing order of x; the total is 85455. Leaving out doubling gives 28657, and leaving out the +2 step gives 74.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic