Counting Teleportations
Practice
3.7 (3 votes)
Dynamic programming
Algorithms
Medium Hard
Advanced
Problem
93% Success 1317 Attempts 50 Points 2.5s Time Limit 1024MB Memory 1024 KB Max Code

There is a row of \(N\) houses labeled \(1 \dots N\) from left to right. You start in house \(1\), and you can move only by teleporting. In order to teleport from house \(X\) to house \(Y\), you must wait at least \(|X-Y|\) units of time (possibly more) before instantaneously teleporting to house \(Y\). It is not allowed to teleport from a house to the same house.

There are \(K\) parties in the houses at various points in time, and you want to attend all of them. The \(i^{th}\) party takes place at house \(x_i\) and at time \(t_i\), and to attend this party, you must reach house \(x_i\) at exactly time \(t_i\). You are allowed to revisit houses, but you are not allowed to wait at a house until a party there begins. There may be multiple parties at a house, although at different times, and there may be multiple parties at a single time, although at different houses.

Define a teleportation sequence as an ordered list of pairs \((a,b)\) indicating the position and time of reaching of a house respectively, ordered in chronological order. The pair \((1,0)\) is the beginning of every teleportation sequence.

How many distinct teleportation sequences are there which allow you to attend all the parties, modulo \(1,000,000,007\) \((10^9 + 7)\)?

Input Format : 

The first line of input contains three space-separated integers, \(N, T\) (the time of the last party), and \(K\).

The next \(K\) lines of input each contain two space-separated integers, \(x_i\) and \(t_i\).

Output Format :

Print the number of distinct teleportation sequences which visit all the parties, modulo \(1,000,000,007\) \((10^9 + 7)\).

Constraints :

\(1 \le N,T,K \le 5000\)

\(1 \le x_i \le N\)

\(1 \le t_i \le T\)

There will be at least one party taking place at time \(T\).

It is guaranteed that there are no two indices \(i\) and \(j\)\(i \neq j\), such that \(x_i = x_j\) and \(t_i = t_j\) both hold true.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
Dynamic ProgrammingDigit DpAlgorithmsMedium-HardDigit DPAdvanced
Points:50
1 votes
Tags:
Dynamic ProgrammingAlgorithmsMedium-HardAdvanced
Points:50
18 votes
Tags:
Dynamic ProgrammingMedium-Hard