KatInterview

Count Step Sequences with Custom Prime Jumps

Dynamic ProgrammingAuto-graded · 26 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingmathsieve

Last updated

Find the number of unique sequences of steps to reach a target using 1-unit steps or prime steps ending in digit 3.

You are standing at step 0 of a stairwell and wish to reach step n.

From any step, you can advance upwards by taking a move of length k, where k can be:

  • standard move: exactly 1 step, or
  • special move: any prime number of steps whose last decimal digit is 3 (for example: 3, 13, 23, 43, ...).

Two jump sequences are considered distinct if their sequence of step sizes differs.

Calculate the total number of distinct jump sequences that land exactly on step n. Because the output can be very large, return the result modulo 1,000,000,007.

Constraints

  • 1 <= n <= 100000

Examples

Example 1

Input: count_prime_ending_jump_paths(1)

Output: 1

Example 2

Input: count_prime_ending_jump_paths(3)

Output: 2

Example 3

Input: count_prime_ending_jump_paths(6)

Output: 6

Example 4

Input: count_prime_ending_jump_paths(13)

Output: 89

Hints

Hint 1

Identify this as a 1D dynamic programming problem where order matters (compositions, not partitions).

Hint 2

Precompute all valid special jump sizes up to n using a Sieve of Eratosthenes.

Hint 3

To efficiently gather candidate prime jumps ending in digit 3, inspect numbers of the form 10k + 3.