Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2016 /finals /snake_and_ladder.md
wjomlex's picture
2016 Problems
07f695f verified
Some time has passed since you took in the local population of snakes, feeding
them every day and allowing them to live peacefully amongst your ladders.
Unfortunately, your neighbours have not been thrilled about living next to
billions of serpents, so they've filed complaints to the mayor! As a result,
almost all of your snakes and ladders have been forcibly evicted, leaving you
with only one of each for company.
Your ladder has **N** horizontal rungs, numbered 1 to **N** from bottom to
top. Like all ladders, it also has 2 vertical rails, with rail 1 on the left
and rail 2 on the right. To improve its appearance, you've allowed some plants
to grow all over it. Notably, there are **K** flowers at distinct locations on
the ladder, with the ith flower at the intersection of rung **R_i** and rail
**C_i**.
Your snake loves to sleep on the ladder. It always places its head at the
intersection of some rung and some rail, and from there, each subsequent
segment of its body extends up, down, left, or right to follow a rung or rail
to another intersection. The snake likes to be spread out, but hates touching
flowers, so it insists that the path that its body takes must not touch any
intersection more than once, and must not touch any intersection that has a
flower. Conveniently, the snake is exactly long enough to theoretically pass
through all intersections that don't have flowers — that is, if consecutive
rungs and rails are 1 unit apart from one another, then the snake has a length
of 2 * **N** \- **K** \- 1 units. Note that your snake may have 0 length (in
which case it'll only occupy a single intersection).
The diagram below (which corresponds to the fourth sample case) illustrates a
valid way in which the snake can arrange itself on a ladder with 4 rungs and 1
flower:
![]({{PHOTO_ID:191658972183607}})
Can you help your snake count the number of different ways in which it can
position itself on the ladder, such that it covers all of the intersections
which don't contain flowers? An arrangement of the snake is defined by an
ordered sequence of intersections that it passes through, starting from its
head. You only need to compute the answer modulo 109 \+ 7.
### Input
Input begins with an integer **T**, the number of ladders. For each ladder,
there is first a line containing the space-separated integers **N** and **K**.
Then **K** lines follow, the **i**th of which contains the space-separated
integers **Ri** and **Ci** .
### Output
For the **i**th ladder, print a line containing "Case #**i**: " followed by
the number of ways in which the snake can be arranged, modulo 109 \+ 7.
### Constraints
1 ≤ **T** ≤ 555
1 ≤ **N** ≤ 1012
0 ≤ **K** ≤ min(1000, 2 * **N** \- 1)
1 ≤ **Ri****N**
1 ≤ **Ci** ≤ 2
### Explanation of Sample
For the first ladder, the snake can place its head in any of the 4
intersections, and from each one it can arrange itself in 2 ways for 8 ways
total. For the second ladder, the flowers prevent the snake from crossing from
the top 2 intersections to the bottom 2 (or vice versa). For the third ladder,
there are 2 possible intersections at which the snake can place its head which
yield 1 valid arrangement each.