In the game "Duck, Duck, Goose", all players but one sit on the floor and form a circle. The remaining player walks around the circle calling each player "duck" until they select one sitting player and, while touching their head, call them "goose" instead. At that point, the goose chases the selecting player and our interest in the game fades.
In the new game "Duck, Duck, Geese", the walking player instead chooses a contiguous subset of at least two (but not all) sitting players to be "geese"! Furthermore, each sitting player is wearing a hat. Each hat is one of possible colors, numbered through .
For each color , the quantity of selected geese wearing a hat of color must be either or between and , inclusive.
Can you help count the number of choices that fulfill these requirements? Two choices are considered different if there is some player that is included in one choice but not the other.
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each test case starts with a line containing two integers and : the number of sitting players and hat colors, respectively. Then, lines follow. The of these lines contains two integers and , as explained above. The last line of a test case contains integers representing that the sitting player in clockwise order (starting from an arbitrary one) is wearing a hat of color .
Output Specification
For each test case, output one line containing Case #x: y
,
where is the test case number (starting from 1) and is the number of sets of
at least and at most contiguously sitting players that fulfill all the color
requirements.
Limits
Time limit: 20 seconds.
Memory limit: 1 GB.
.
.
, for all .
, for all .
Test Set 1
.
Test Set 2
.
Sample Input
3
3 2
1 1
1 1
1 1 2
5 2
1 1
1 2
1 2 1 2 2
3 3
1 2
1 2
2 2
1 1 3
Sample Output
Case #1: 2
Case #2: 9
Case #3: 1
In Sample Case #1, the total number of players chosen as geese must be . There are only three possible ways to select players. The following color configurations are possible: , , and . The first one has two players wearing hats of color , so it is not valid, but the other two are valid. Therefore the answer is .
Sample Case #2 is the one illustrated in the statement, with color being yellow and color being blue. The total number of players chosen as geese in this case must be between and , because selecting geese would require at least one color to be out of bounds. For cases with geese, the only requirement is that we do not select geese both wearing hats of color ; all such selections are valid. If choosing geese, the options are , , , , or . All but the first one are valid, adding another valid options, for a total of .
In Sample Case #3, notice that there can be hat colors that nobody is wearing. In this case, since there is only player wearing hat color and is not in range, the only valid way is to pick players wearing that hat color.
Comments