NC222467. [USACODec2020P]Spaceship
描述
输入描述
The first line contains N,K,Q.
The next N lines each contain N bits (each 0 or 1). The j-th entry of the i-th line is 1 if there exists a door from room i to room j, and 0 if no such door exists.
This is followed by Q lines, each containing four integers bs, s, bt, t, denoting the starting button, starting room, final button, and final room respectively.
输出描述
The number of sequences for each of the Q queries modulo 109+7 on separate lines.
示例1
输入:
6 3 8 010000 001000 000100 000010 000000 000001 1 1 1 1 3 3 1 1 1 1 3 3 1 1 1 5 2 1 1 5 1 1 2 5 3 1 3 5 2 6 2 6
输出:
1 0 1 3 2 2 0 5
说明:
The doors connect rooms 1→2, 2→3, 3→4, 4→5, and 6→6.
For the first query, Bessie must stop immediately after pressing the first button.
For the second query, the answer is clearly zero because there is no way to get to room 1 from room 3.
For the third query, Bessie's only option is to move from room 1 to room 2 to room 3 while pressing buttons 1, 2, and 3.
For the fourth query, Bessie's pattern of movement is fixed, and she has three possible sequences of button presses:
(1,2,3,2,1)
(1,2,1,3,1)
(1,3,1,2,1)
For the last query, Bessie has five possible sequences of button presses:
(2)
(2,3,2)
(2,3,1,2)
(2,1,3,2)
(2,1,3,1,2)
示例2
输入:
6 4 6 001100 001110 101101 010111 110111 000111 3 2 4 3 3 1 4 4 3 4 4 1 3 3 4 3 3 6 4 3 3 1 4 2
输出:
26 49 29 27 18 22
说明:
This test case satisfies the constraints for all subtasks aside from the first.示例3
输入:
6 10 5 110101 011001 001111 101111 111010 000001 2 5 2 5 6 1 5 2 3 4 8 3 9 3 3 5 5 1 3 4
输出:
713313311 716721076 782223918 335511486 539247783
说明:
Make sure to output the answers modulo 109+7.