The problem
RECR-P2 counted the ways to go up a footbridge of n steps with moves of 1, 2 or 3. Now the allowed moves are given in the list steps, and n can be enormous. Return the number of different sequences of moves that end exactly at the top, modulo m (the remainder after dividing by m).
ways_mod(4, [1, 2, 3], 1000) # 7
ways_mod(10, [1, 2], 10 ** 9) # 89
ways_mod(7, [2], 100) # 0
ways_mod(0, [1, 2, 3], 1000) # 1
Limits
nis from 0 to 10¹⁸ (a 1 followed by 18 zeros).stepsholds 1 to 10 different moves, each from 1 to 10.mis from 1 to about 10¹⁸.- A loop over every step is far too slow for n = 10¹⁸. The big tests have 2 seconds each.
Builds on MATH-3 Fast exponentiation.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
13 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | No steps to go up | Not run | |
| 3 | m is 1 | Not run | |
| 4 | Fibonacci | Not run | |
| 5 | Only 2s, odd n | Not run | |
| 6 | Only 2s, even n | Not run | |
| 7 | Moves of 3 and 5 | Not run | |
| 8 | The first 20 | Not run | |
| 9 | A million steps | Not run | |
| 10 | 10¹⁸ steps | Not run | |
| 11 | Ten moves, a huge m | Not run | |
| 12 | Unusual moves | Not run | |
| 13 | The last digit of a Fibonacci number | Not run |