This page is still under construction.

Parts of this page are still being built. What you see may change.

Strong-box

Time limit1sMemory limit128 MB

Summary
Given the current knob and bolt positions and the coupling matrix modulo a prime, find the knob configuration that sets all bolts to zero.
Level

Medium7 of 10

Topics
Math, Matrix, Number theory, Implementation
Solved
No attempts yet

Problem

ByteGuy owns a strong-box secured by a lock with nn knobs. Each knob, and each of the nn bolts hidden inside the lock, can be in one of pp positions numbered 00 to p−1p-1, where pp is prime.

The lock opens exactly when every bolt is at position 00.

Turning knob ii forward by one position (from 00 to 11, from 11 to 22, ..., and from p−1p-1 back to 00) rotates bolt jj by ci,jc_{i,j} positions: if bolt jj was at position ll, it moves to (l+ci,j) mod p(l + c_{i,j}) \bmod p.

ByteGuy has forgotten the combination. A 3D scanner lets him read the current position of every hidden bolt, and the lock is built so that exactly one final knob configuration opens it.

Given the current knob positions, the current bolt positions, and the coupling values ci,jc_{i,j}, output the knob configuration that opens the lock.

Input

The first line contains two integers: the number of knobs nn with 1≤n≤3001 \le n \le 300, and the prime number of positions pp with 3≤p≤400003 \le p \le 40000.

The second line contains nn integers in the range 0…p−10 \ldots p-1: the current positions of the knobs.

The third line contains nn integers in the range 0…p−10 \ldots p-1: the current positions of the bolts.

Each of the next nn lines describes one knob. Line ii contains nn integers ci,0,ci,1,…,ci,n−1c_{i,0}, c_{i,1}, \ldots, c_{i,n-1} with 0≤ci,j<p0 \le c_{i,j} < p.

Output

Output one line with nn integers in the range 0…p−10 \ldots p-1, separated by single spaces: the final knob positions that open the lock.

Examples4

  1. Example 1

    Input
    2 3
    1 1
    2 2
    1 0
    0 1
    
    Expected output
    2 2
    
  2. Example 2

    Input
    1 5
    0
    1
    3
    
    Expected output
    3
    
  3. Example 3

    Input
    4 7
    3 5 2 5
    0 0 0 0
    0 6 1 6
    4 2 3 3
    3 1 2 5
    6 6 5 0
    
    Expected output
    3 5 2 5
    
  4. Example 4

    Input
    5 11
    7 4 10 1 5
    5 10 6 10 4
    1 0 0 0 0
    0 1 0 0 0
    0 0 1 0 0
    0 0 0 1 0
    0 0 0 0 1
    
    Expected output
    2 5 4 2 1