Count Step Sequences with Custom Prime Jumps
Dynamic ProgrammingAuto-graded · 26 test casesLast 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
1step, 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem asks for the number of ordered step sequences (compositions) that sum to n.
Let dp[i] denote the number of ways to reach step i. The final move into step i could be:
- A jump of size
1, coming from stepi - 1. - A special jump of size
p, wherepis a prime ending in3($p \equiv 3 \pmod{10}$) and $p \le i$, coming from stepi - p.
This yields the linear recurrence relation: $$dp[i] = dp[i-1] + \sum_{p \in \text{SpecialJumps}, p \le i} dp[i-p]$$
Algorithm Steps:
- Prime Generation: Use the Sieve of Eratosthenes up to
nto mark primality. - Filter Valid Jumps: Extract all primes $p \le n$ such that $p \equiv 3 \pmod{10}$. We can iterate directly through numbers matching
3, 13, 23, 33, ...to skip non-candidates. - Dynamic Programming: Iterate
ifrom1ton. Computedp[i]by summing transition sources and applying the modulo operation at each step.
Complexity
- Time Complexity:
- Sieve: $\mathcal{O}(n \log \log n)$ time.
- DP Transitions: There are approximately $\frac{\pi(n)}{4} \approx 2,400$ valid special jumps for $n = 10^5$. Outer loop runs $n$ times, and the inner loop breaks early when the jump exceeds $i$. Total DP time is $\mathcal{O}(n \cdot k)$, which performs well within execution limits.
- Space Complexity: $\mathcal{O}(n)$ to store prime status flags and the DP state table.
Python Reference Solution
MOD = 1_000_000_007
def count_prime_ending_jump_paths(n: int) -> int:
# 1. Precompute primes up to n via Sieve of Eratosthenes
is_prime = [True] * (n + 1)
if n >= 0:
is_prime[0] = False
if n >= 1:
is_prime[1] = False
p = 2
while p * p <= n:
if is_prime[p]:
for multiple in range(p * p, n + 1, p):
is_prime[multiple] = False
p += 1
# 2. Collect allowed prime steps ending in digit 3
special_jumps = [p for p in range(3, n + 1, 10) if is_prime[p]]
# 3. Dynamic programming for ordered sequences
dp = [0] * (n + 1)
dp[0] = 1
for current_step in range(1, n + 1):
ways = dp[current_step - 1]
for jump in special_jumps:
if jump > current_step:
break
ways += dp[current_step - jump]
dp[current_step] = ways % MOD
return dp[n]