This page is still under construction.

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

Sum of Matrix Powers

Time limit2sMemory limit512 MB

Summary
Given an N by N matrix A and K, compute A + A^2 + ... + A^K with all entries reduced modulo M.
Level

Medium7 of 10

Topics
Divide and conquer, Matrix, Math
Solved
No attempts yet

Problem

You are given an N×NN \times N matrix AA and integers KK and MM. Reducing every entry of AKA^K modulo MM is not hard.

This task asks for something else. Define the matrix SS as

S=A+A2+A3+⋯+AKS = A + A^2 + A^3 + \cdots + A^K

and print every entry of SS modulo MM.

Input

The first line contains NN, KK, and MM separated by spaces (1≤N≤501 \le N \le 50, 1≤K≤1091 \le K \le 10^9, 1≤M≤1041 \le M \le 10^4).

Each of the next NN lines contains the matrix AA, NN integers per line separated by spaces (−104≤Aij≤104-10^4 \le A_{ij} \le 10^4).

Output

Print NN lines. Each line holds the NN entries of the corresponding row of SS reduced modulo MM, separated by single spaces.

Hint

Every remainder must be at least 00 and less than MM. Entries of AA can be negative, so watch the sign of the remainder.

Examples5

  1. Example 1

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

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

    Input
    1 1000000000 10000
    2
    
    Expected output
    8750
    
  4. Example 4

    Input
    3 3 100
    -1 2 -3
    4 -5 6
    -7 8 -9
    
    Expected output
    61 42 55
    0 71 58
    39 16 29
    
  5. Example 5

    Input
    2 1 10000
    -10000 -1
    -9999 0
    
    Expected output
    0 9999
    1 0