algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
3 shown · 0/261 solved

Count Shrinking Jump Sequences

GoldCommunity Beta
Asked atPhonePe
Solve problem →
3000ms256MBAdded Aug 19, 2026

You are given integers n, a, b, and k. There are n positions on a line, numbered 1 to n, you start at position a, and a fixed anchor sits at position b.

You must make exactly k jumps. A jump from the current position x may land on any position y with:

  • y != x, and
  • |x - y| < |x - b|, meaning the jump must be strictly shorter than your current distance to the anchor.

Two sequences are different if they differ at any step.

Return the number of valid sequences of exactly k jumps. Mod the result by 10^9 + 7.

Examples

Example 1

Input: n = 5, a = 2, b = 5, k = 1
Output: 3
Explanation: From position 2 the distance to the anchor is 3, so a jump may
cover distance 1 or 2: positions 1, 3, and 4 are reachable.

Example 2

Input: n = 5, a = 2, b = 5, k = 2
Output: 5
Explanation: The 3 first jumps above continue as follows: from 1 (distance 4)
positions 2, 3, 4 are reachable; from 3 (distance 2) positions 2 and 4; from 4
(distance 1) no jump is short enough. Total 3 + 2 + 0 = 5.

Constraints

  • 1≤n≤10001 \leq n \leq 10001≤n≤1000
  • 1≤a,b≤n1 \leq a, b \leq n1≤a,b≤n
  • 1≤k≤10001 \leq k \leq 10001≤k≤1000

Details

Solved by3 people
Time limit3000ms
Memory256MB
AddedAug 19, 20263 weeks ago