File size: 3,753 Bytes
07f695f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<p>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.</p>
<p>Your ladder has <strong>N</strong> horizontal rungs, numbered 1 to <strong>N</strong> 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 <strong>K</strong> flowers at distinct locations on the ladder, with the ith flower at the intersection of rung <strong>R_i</strong> and rail <strong>C_i</strong>.</p>
<p>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 * <strong>N</strong> - <strong>K</strong> - 1 units. Note that your snake may have 0 length (in which case it'll only occupy a single intersection).</p>
<p>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:</p>
<center>
<img src="{{PHOTO_ID:191658972183607}}" />
</center>
<p>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 10<sup>9</sup> + 7.<p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of ladders.
For each ladder, there is first a line containing the space-separated integers <strong>N</strong> and <strong>K</strong>.
Then <strong>K</strong> lines follow, the <strong>i</strong>th of which contains the space-separated integers
<strong>R<sub>i</sub></strong> and <strong>C<sub>i</sub></strong> .
</p>
<h3>Output</h3>
<p>
For the <strong>i</strong>th ladder, print a line containing "Case #<strong>i</strong>: " followed by the number of ways in which the snake can be arranged, modulo 10<sup>9</sup> + 7.
</p>
<h3>Constraints</h3>
<p>
1 ≤ <strong>T</strong> ≤ 555 <br />
1 ≤ <strong>N</strong> ≤ 10<sup>12</sup> <br />
0 ≤ <strong>K</strong> ≤ min(1000, 2 * <strong>N</strong> - 1) <br />
1 ≤ <strong>R<sub>i</sub></strong> ≤ <strong>N</strong> <br />
1 ≤ <strong>C<sub>i</sub></strong> ≤ 2 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
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.
</p>
|