#P8497. [NOI2022] 移除石子

[NOI2022] 移除石子

Problem Description

You are playing a mini-game called “Removing Stones”.

There are nn piles of stones in a row. The ii-th pile contains aia_i stones. Your task is to remove all stones using the following operations:

  • Operation 1: Choose one pile and remove at least 22 stones from it.
  • Operation 2: Choose a consecutive index interval [l,r][l, r] (1lrn1 \le l \le r \le n) satisfying rl2r - l \ge 2, and remove exactly 11 stone from every pile in this interval.

You may perform the two operations in any order, any number of times, until no operation can be performed. If in the end you can remove all stones, you win.

You may already be thinking about questions like “how many essentially different ways of operating are there”, but when you actually play, you find that you keep losing. So you decide to use a trick: before the game starts, you secretly hold kk stones in your hand, and before performing any operations you can and must put these stones into one pile or several piles. You hope this will increase your chance of winning, but you also know it might make you lose a game that you could otherwise win.

Now, you may freely choose an initial position to start the game. Specifically, each aia_i can be chosen as any integer within the range [li,ri][l_i, r_i]. You want to compute, among all initial positions, for how many of them there exists at least one winning strategy. Since the answer is large, you only need to output it modulo (109+7)({10}^9 + 7). Two initial positions are different if and only if there exists at least one 1in\boldsymbol{1 \le i \le n} such that their ai\boldsymbol{a_i} are different. Note that the “initial position” here refers to the position before you put in the k\boldsymbol{k} stones.

Input Format

This problem has multiple test cases. The first line contains a positive integer TT denoting the number of test cases, followed by the test cases.

For each test case, the first line contains two integers n,kn, k, representing the number of piles and the number of stones to add. Then follow nn lines, each containing two non-negative integers li,ril_i, r_i, representing the range of the initial number of stones in each pile.

Output Format

For each test case, output one integer per line, representing the number of initial positions from which it is possible to win, modulo (109+7)({10}^9 + 7).

1
4 1
0 1
0 1
0 1
0 1

14

Hint

[Sample Explanation #1]

There are 24=162^4 = 16 possible initial positions. It can be proven that except for (0 0 0 0)(0 \ 0 \ 0 \ 0) and (1 0 0 1)(1 \ 0 \ 0 \ 1), these two initial positions cannot be won, all other initial positions have a winning strategy. For example, when the initial position is (1 0 1 0)(1 \ 0 \ 1 \ 0), you can put the 11 stone in your hand into pile 22, making the position (1 1 1 0)(1 \ 1 \ 1 \ 0). Then use Operation 2 once on the interval [1,3][1, 3].


[Sample #2]

See stone/stone2.in and stone/stone2.ans in the attachment.


[Sample #3]

See stone/stone3.in and stone/stone3.ans in the attachment.


[Sample #4]

See stone/stone4.in and stone/stone4.ans in the attachment.


[Constraints]

For 100%100\% of the testdata, it is guaranteed that T10T \le 10, 3n10003 \le n \le 1000, 0liri1090 \le l_i \le r_i \le {10}^9, and 0k1000 \le k \le 100.

Test Point ID nn \le kk \le Special Condition
131 \sim 3 55 22 ri5r_i \le 5
454 \sim 5 10001000 00 li=ril_i = r_i
686 \sim 8 100100
9119 \sim 11 00 None
121312 \sim 13 22
141514 \sim 15 100100 ri10r_i \le 10
162016 \sim 20 None

Translated by ChatGPT 5