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
Compute A + A^2 + ... + A^B for an N by N matrix A, with B up to 1e11, printing every entry modulo 1000.
Level

Medium7 of 10

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

Problem

You are given an N×NN \times N matrix AA. Write a program that computes the matrix obtained by adding every power of AA from the first up to the BBth:

S=A1+A2+⋯+ABS = A^1 + A^2 + \cdots + A^B

The entries can grow very large, so print each entry of SS modulo 1,000.

Input

The first line contains the matrix size NN and the exponent bound BB, separated by a space. (2≤N≤52 \le N \le 5, 1≤B≤100,000,000,0001 \le B \le 100{,}000{,}000{,}000)

Each of the next NN lines contains NN entries of AA, separated by spaces. Every entry is 0 or a natural number at most 1,000.

Output

Print the matrix SS on NN lines. Line ii contains the NN entries of row ii of SS, each taken modulo 1,000 and separated by a space.

Examples3

  1. Example 1

    Input
    2 5
    1 2
    3 4
    
    Expected output
    313 914
    871 184
    
  2. Example 2

    Input
    3 3
    1 2 3
    4 5 6
    7 8 9
    
    Expected output
    499 614 729
    132 391 650
    765 168 571
    
  3. Example 3

    Input
    5 10
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    
    Expected output
    23 0 0 0 23
    23 0 0 0 23
    23 0 0 0 23
    23 0 0 0 23
    23 0 0 0 23