Bessie the cow has been abducted by aliens and is now trapped inside an alien spaceship! The spaceship has?NN?(1≤N≤60)(1≤N≤60)?rooms labeled?1…N1…N, with one-way doors connecting between some pairs of rooms (due to the strange alien technology at play, it is even possible for a door to lead from a room back to itself!). However, no two doors share the same starting and end room. Additionally, Bessie has a remote with buttons numbered?1…K1…K?(1≤K≤60)(1≤K≤60).
The aliens will release Bessie if she can complete a strange task. First, they will choose two rooms,?ss?and?tt?(1≤s,t≤N)(1≤s,t≤N), and two numbers,?bsbs?and?btbt?(1≤bs,bt≤K)(1≤bs,bt≤K). They will start Bessie in room?ss?and immediately have her press button?bsbs. Bessie will then proceed to navigate the ship while pressing buttons. There are a few rules for what Bessie can do:
Bessie is released only if she stops in room?tt, the last button she pressed was?btbt, and no invalid buttons were ever pressed.
Bessie is worried that she may not be able to complete the task. For?QQ?(1≤Q≤60)(1≤Q≤60)?queries, each consisting of what Bessie considers a likely choice of?s,t,bss,t,bs, and?btbt, Bessie wants to know the number of sequences of rooms and button presses that would lead to her release. Report your answers modulo?109+7109+7?as they may be very large.
The first line contains?N,K,QN,K,Q.The next?NN?lines each contain?NN?bits (each 0 or 1). The?jj-th entry of the?ii-th line is 1 if there exists a door from room?ii?to room?jj, and 0 if no such door exists.
This is followed by?QQ?lines, each containing four integers?bsbs,?ss,?btbt,?tt, denoting the starting button, starting room, final button, and final room respectively.
The number of sequences for each of the?QQ?queries modulo?109+7109+7?on separate lines.
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→21→2,?2→32→3,?3→43→4,?4→54→5, and?6→66→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:
For the last query, Bessie has five possible sequences of button presses:
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.
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+7109+7.
Problem credits: Benjamin Qi
[/hide]
(Analysis by Danny Mittal)
Subtask 1?(K≤5K≤5)
We can do this via bitmask DP. Our states will be pairs?(μ,r)(μ,r)?where?μμ?is a bitmask of length?KK?and?rr?is a room. The bitmask?μμ?represents all sequences of button presses such that iff the?jjth bit in?μμ?is on, Bessie has pressed button?jj?at least once and has not pressed any button with a higher number than?jj?since the last time she pressed button?jj. The state?(μ,r)(μ,r)?then represents all sequences of button presses and rooms beginning in room?ss?by pressing button?bsbs, ending in room?rr, and satisfying the conditions to correspond to mask?μμ.
A transition in our DP is then moving to another (possibly the same) room as well as pressing another button. Bessie can move to room?r′r′?iff there is an edge from?rr?to?r′r′. Additionally, Bessie can press button?jj?iff the?jjth bit in the current mask?μμ?is off — otherwise, she would press button?jj?twice without pressing a button with a higher number in between.
We then must also consider how pressing a button changes?μμ. It clearly sets the?jjth bit to be on. Additionally, all lower bits are set off now that a higher button has been pressed. All higher bits are unaffected. It is important to note that the way this modification works implies that we always increase the numerical value of our bitmask when transitioning, meaning that there are no circular relations in our DP and that we should consider bitmasks in order of increasing numerical value.
Our DP then has?O(2KN)O(2KN)?states, from each of which we perform?O(KN)O(KN)?transitions, meaning that our DP runs in?O(K2KN2)O(K2KN2).
To compute the answer for each query, we simply take the sum of the DP values of?(t,μ)(t,μ)?over all bitmasks?μμ?such that the?btbtth bit is on and all lower bits are off, as these correspond precisely to the sequences where we end by pressing button?btbt. This takes?O(2K)O(2K)?time, so that our final computations take?O(2KQ)O(2KQ)?time overall and thus our overall solution takes?O(2K(KN2+Q))O(2K(KN2+Q)).
Subtask 2?(bs=K?1bs=K?1?and?bt=Kbt=K?for each query)
First note that as Bessie always presses button?KK?at the end, she cannot press button?KK?before then, as there would be no higher button that she could press in between (as button?KK?is the highest button). This also means that she cannot press button?K?1K?1?after the beginning as she could never press button?KK, the only higher button. Thus, any sequence of button presses that Bessie takes, excluding the first and last button, uses buttons with numbers at most?K?2K?2.
We can then solve this using DP.?dpa,b,xdpa,b,x?will be the number of sequences of rooms and button presses which start at room?aa, end at room?bb, and only involve pressing buttons with numbers at most?xx. We can compute transitions as follows. First, we add all of?dpa,b,x?1dpa,b,x?1?to?dpa,b,xdpa,b,x, as a sequence only using buttons with numbers at most?x?1x?1?will clearly also only use buttons with numbers at most?xx.
We then consider the case where we do press button?xx. Note that as we never press any higher buttons, we must only press button?xx?once. Let’s then fix the room?rr?that Bessie is in when she presses button?xx, and consider in isolation the part of the sequence before pressing button?xx?(the left side) and after pressing button?xx?(the right side). The left side, if it is not empty, starts at some room?aa, ends at some room?bb?such that there is an edge from?bb?to?rr, and only presses buttons with number at most?x?1x?1. Similarly, the right side, if it is not empty, starts at some room?cc?such that there is an edge from?rr?to?cc, ends at some room?dd, and only presses buttons with number at most?x?1x?1.
The number of left sides is then?dpa,b,x?1dpa,b,x?1?for each?a,ba,b?and the number of right sides is?dpc,d,x?1dpc,d,x?1?for each?c,dc,d. We only care about the endpoints?aa?and?dd, so for each?aa, we compute the sum over all?dpa,b,x?1dpa,b,x?1?such that there is an edge from?bb?to?rr, and similarly for each?dd. Note that we should account for the left side possibly being empty by adding?11?to our calculation for?a=ra=r, as in that case our overall left endpoint will just be?rr?(and similarly for the right side). Finally, for each?a,da,d, we can simply multiply these quantities together and add the result to?dpa,d,xdpa,d,x.
Each of these three computations is?O(N2)O(N2), so as we do them for each maximum number?xx?and “middle” room?rr, our DP computation is?O(N3K)O(N3K)?overall.
Given this DP, for each query to compute the answer, given that we want to start at room?ss?and end at room?tt, we loop through all second rooms?aa?(such that there is an edge from?ss?to?aa) and second-to-last rooms?bb?(such that there is an edge from?bb?to?tt) and add?dpa,b,K?2dpa,b,K?2?to our answer. This is?O(N2)O(N2)?for each query, making our overall algorithm?O(N3K+QN2)O(N3K+QN2).
Subtask 3?(N,K,Q≤20N,K,Q≤20)
We can solve this subtask by modifying our solution for the previous subtask. First note that our constraints are now low enough to allow computing our?O(N3K)O(N3K)?DP for each query. Given this, we can simply modify our DP as follows.?dpa,b,kdpa,b,k?will be defined as usual, except that?aa?can also take a special value?αα?which will indicate that we are counting sequences which start at room?ss?but must also start by pressing button?bsbs. Similarly,?bb?can take a special value?ββ?which will indicate that we are counting sequences which end at room?tt?but must also end by pressing button?btbt.
These special values?αα?and?ββ?then essentially function as normal rooms during our DP. There are only two differences: ones is that in our above loops through all?a,ba,b?and?c,dc,d, the rooms?bb?and?cc?are not allowed to be?αα?or?ββ?as that would lead to overcounting; the other is that we must properly account for the left side being empty by also adding?11?to our calculation for?a=αa=α?if we are currently at?r=sr=s?and?x=bsx=bs, and similarly for the right side.
Our answer is then simply?dpα,β,Kdpα,β,K. These modifications do not affect the asymptotic runtime of our DP, but we do now do it for each query, giving the overall algorithm a runtime of?O(QN3K)O(QN3K).
Subtask 4?(original constraints)
Our solution for this is essentially the same as for the previous subtask, noting that we don’t actually really need to compute this DP for each query. We can compute this DP just once by, instead of having the two special values?αα?and?ββ, having values?αjαj?and?βjβj?for each?1≤j≤Q1≤j≤Q?such that?αjαj?corresponds to?(s,bs)(s,bs)?for the?jjth query and?βjβj?corresponds to?(t,bt)(t,bt)?for the?jjth query. The answer to the?jjth query is then?dpαj,βj,Kdpαj,βj,K.
Considering the additional states, the runtime of our algorithm is now?O(N(N+Q)2K)O(N(N+Q)2K), which is still fast enough.
Danny's code:
import java.util.Scanner; public class LevelGraph { public static final long MOD = 1000000007; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int q = in.nextInt(); boolean[][] adj = new boolean[n][n]; for (int a = 0; a < n; a++) { String line = in.next(); for (int b = 0; b < n; b++) { adj[a][b] = line.charAt(b) == '1'; } } int[] xs = new int[q]; int[] froms = new int[q]; int[] ys = new int[q]; int[] tos = new int[q]; for (int j = 0; j < q; j++) { xs[j] = in.nextInt() - 1; froms[j] = in.nextInt() - 1; ys[j] = in.nextInt() - 1; tos[j] = in.nextInt() - 1; } long[][][] dp = new long[k][n + q][n + q]; for (int h = 0; h < k; h++) { if (h > 0) { for (int a = 0; a < n + q; a++) { for (int b = 0; b < n + q; b++) { dp[h][a][b] = dp[h - 1][a][b]; } } } for (int c = 0; c < n; c++) { long[] left = new long[n + q]; left[c] = 1; for (int j = 0; j < q; j++) { if (h == xs[j] && froms[j] == c) { left[n + j] = 1; } } if (h > 0) { for (int a = 0; a < n + q; a++) { for (int b = 0; b < n; b++) { if (adj[b][c]) { left[a] += dp[h - 1][a][b]; left[a] %= MOD; } } } } long[] right = new long[n + q]; right[c] = 1; for (int j = 0; j < q; j++) { if (h == ys[j] & tos[j] == c) { right[n + j] = 1; } } if (h > 0) { for (int a = 0; a < n; a++) { for (int b = 0; b < n + q; b++) { if (adj[c][a]) { right[b] += dp[h - 1][a][b]; right[b] %= MOD; } } } } for (int a = 0; a < n + q; a++) { for (int b = 0; b < n + q; b++) { dp[h][a][b] += left[a] * right[b]; dp[h][a][b] %= MOD; } } } } for (int j = 0; j < q; j++) { System.out.println(dp[k - 1][n + j][n + j]); } } }
[/hide]
? 2025. All Rights Reserved. 沪ICP备2023009024号-1