#P7155. [USACO20DEC] Spaceship P

    ID: 8091 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP2020USACOO2优化组合数学

[USACO20DEC] Spaceship P

Problem Description

The cow Bessie has been abducted by aliens and is now locked inside an alien spaceship. The spaceship has NN (1N601\le N\le 60) rooms, numbered 1N1\ldots N. Some pairs of rooms are connected by one-way doors (because of strange alien technology, a door may even lead from a room back to itself). However, no two doors have exactly the same starting room and ending room. In addition, Bessie has a remote control with buttons numbered 1K1\ldots K (1K601\le K\le 60).

If Bessie can complete a strange task, the aliens will release her. First, they choose two rooms ss and tt (1s,tN1\le s,t\le N), and two integers bsb_s and btb_t (1bs,btK1\le b_s,b_t\le K). They place Bessie in room ss and make her immediately press button bsb_s. Then Bessie must continue moving around the spaceship while pressing buttons. There are some rules that Bessie's actions must follow:

  • In each room, after pressing exactly one button, she must either choose a door to leave to another room (possibly returning to the same room) or stop moving.
  • Once Bessie presses a button, pressing that same button again is illegal unless she has pressed a button with a larger number in between. In other words, pressing button xx makes this button illegal, while all buttons with numbers <x<x are reset to legal.
  • If Bessie presses an illegal button, the task immediately fails and the aliens will keep her locked up.

Bessie is released only if she stops in room tt, the last button she pressed is btb_t, and she has never pressed an illegal button.

Bessie worries that she might not be able to complete this task. For QQ (1Q601\le Q\le 60) queries, each query contains a set of s,t,bs,s,t,b_s, and btb_t that Bessie thinks might be possible. She wants to know the number of room and button sequences that can lead to her release. Since the answer may be very large, output it modulo 109+710^9+7.

Constraints

  • 1N601\le N\le 60.
  • 1K601\le K\le 60.
  • 1Q601\le Q\le 60.

Input Format

The first line contains N,K,QN,K,Q.

The next NN lines each contain NN binary digits (00 or 11). If there is a door from room ii to room jj, then the jj-th digit of the ii-th line is 1; otherwise it is 0.

The next QQ lines each contain four integers bsb_s, ss, btb_t, tt, representing the starting button, starting room, ending button, and ending room.

Output Format

For each of the QQ queries, output on its own line the number of valid action sequences modulo 109+710^9+7.

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
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
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

Hint

The doors connect rooms 121\to 2, 232\to 3, 343\to 4, 454\to 5, and 666\to 6.

For the first query, Bessie must stop immediately after pressing the first button.

For the second query, the answer is clearly zero, because it is impossible to go from room 3 to room 1.

For the third query, Bessie's only choice 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 movement is unique, and she has three possible button sequences:

  • (1,2,3,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)

For the last query, Bessie has five possible button sequences:

  • (2)
  • (2,3,2)
  • (2,3,1,2)
  • (2,1,3,2)
  • (2,1,3,1,2)

Test Point Properties

  • In test points 4-7, K5K\le 5 and (bs,s)(b_s,s) is the same across all queries.
  • In test points 8-11, for all queries bs=K1b_s=K-1 and bt=Kb_t=K.
  • In test points 12-15, N,K,Q20N,K,Q\le 20.
  • In test points 16-23, there are no additional restrictions.

Problem by: Benjamin Qi.

Translated by ChatGPT 5