#5413. Problem 2. Cow Circle

0

Problem 2. Cow Circle

Problem 2. Cow Circle

USACO 2026 Second Contest, Platinum

Note: The time limit for this problem is 6s, thrice the default. The memory limit for this problem is 512MB, twice the default.

Farmer John has NN (1N50001 \leq N \leq 5000) cows standing around a circular track divided into MM (1M1061 \leq M \leq 10^6) equally spaced positions, numbered 00 to M1M-1 clockwise. Cow ii is initially at position xix_i, where 0=x1<x2<<xN<M0 = x_1 < x_2 < \dots < x_N < M.

For each 1iN1 \leq i \leq N, cow ii will independently and randomly choose either to face clockwise or counterclockwise with some probability specific to that cow. Once a cow has chosen her initial direction, she begins moving continuously in that direction at a constant speed of one position per minute. Whenever two cows meet (i.e., they occupy the same space), they bounce off of each other: immediately reversing their directions and continuing to move at the same speed in that direction.

Farmer John is wondering where cow 11 will end up. For each 0i<M0 \leq i < M, find the probability that cow 11 is at position ii after KK (1K10181 \leq K \leq 10^{18}) minutes.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains TT (1T1001 \leq T \leq 100), the number of independent test cases. Each test case is specified as follows:

The first line of each test case contains NN (1N50001 \leq N \leq 5000), MM (1M1061 \leq M \leq 10^6), and KK (1K10181 \leq K \leq 10^{18}).

The second line contains NN integers p1,,pNp_1, \dots, p_N (0pi<109+70 \leq p_i < 10^9 + 7) where if aibi\frac{a_i}{b_i} is the probability cow ii goes clockwise, then pibiai(mod109+7)p_i \cdot b_i \equiv a_i \pmod{10^9+7}.

The third and final line contains NN integers x1,x2,,xNx_1, x_2, \dots, x_N.

It is guaranteed that the sum of N2N^2 over all test cases is 50002\leq 5000^2 and the sum of MM over all testcases is 106\leq 10^6.

OUTPUT FORMAT (print output to the terminal / stdout):

Output a new line for each test case. The line for each test case should be formatted as follows:

For every 0i<M0 \leq i < M, let piqi\frac{p_i}{q_i} be the probability cow 11 is in position ii at the end of KK minutes. Output MM space-separated integers piqi1(mod109+7)p_iq_i^{-1} \pmod{10^9 + 7} (where piqi1qipi(mod109+7)p_iq_i^{-1} \cdot q_i \equiv p_i \pmod{10^9+7}).

SAMPLE INPUT:


3
2 2 1
500000004 500000004 
0 1
3 3 1
500000004 500000004 500000004
0 1 2
5 10 13
500000004 1 500000004 0 500000004
0 3 4 7 9

SAMPLE OUTPUT:


500000004 500000004
500000004 250000002 250000002
0 0 0 125000001 375000003 0 125000001 375000003 0 0

For the first test case, both cows have a 12\frac{1}{2} chance of going in either direction. If both pick the same direction, they will end up swapping positions (so cow 11 ends up at 11). Otherwise, they will bounce off in the middle and return to their original positions. Therefore, there is a 12\frac{1}{2} chance for cow 11 to end up at 00 and a 12\frac{1}{2} chance for cow 11 to end up at 11.

For the second test case, all cows again have a 12\frac{1}{2} chance of going in either direction. For each combination of directions, here is where cow 11 ends up at.

  • CW, CW, CW: 11
  • CW, CW, CCW: 11
  • CCW, CCW, CCW: 22
  • CCW, CW, CCW: 22
  • CW, CCW, CW: 00
  • CW, CCW, CCW: 00
  • CCW, CW, CW: 00
  • CCW, CCW, CW: 00

SCORING: Input 2: K100,N10K \leq 100, N \leq 10.Input 3: N10N \leq 10.Inputs 4-7: N35003\sum N^3 \leq 500^3.Inputs 8-11: K<M2K < \frac{M}{2}. Inputs 12-15: No additional constraints.

Problem credits: Sujay Konda