Under construction

Still being built. Things may change

Lord of the Characteristic Polynomials (2)

Time limit5sMemory limit1024 MB

Summary
Given an n by n matrix whose entries are a + b*sqrt(D), find each coefficient of its characteristic polynomial as p + q*sqrt(D), reduced modulo M.
Level

Hard8 of 10

Topics
Matrix, Math, Number theory
Solved
No attempts yet

Problem

For an n×nn \times n matrix AA, the determinant is defined as follows, where aija_{ij} is the entry in row ii and column jj. SnS_n is the set of permutations of {1,⋯ ,n}\{1, \cdots, n\}, so {1,⋯ ,n}={σ(1),⋯ ,σ(n)}\{1, \cdots, n\} = \{\sigma(1), \cdots, \sigma(n)\} holds for every σ∈Sn\sigma \in S_n.

det⁡A=∑σ∈Snsgn(σ)⋅(∏i=1naiσ(i))\det A = \sum_{\sigma \in S_{n}} \mathrm{sgn}(\sigma) \cdot \left(\prod_{i = 1}^{n} a_{i \sigma(i)}\right)

The characteristic polynomial ϕA(x)\phi_{A}(x) of AA is a polynomial of degree nn in xx, defined as ϕA(x)=det⁡(xI−A)=cnxn+cn−1xn−1+⋯+c0\phi_{A}(x) = \det(xI - A) = c_{n}x^{n} + c_{n-1}x^{n-1} + \cdots + c_{0}.

In this problem the entries are special. You are given an integer D≠1D \neq 1 that is not divisible by any perfect square greater than 1. Every entry of AA has the form a+bDa + b\sqrt{D} with integers aa and bb, so each entry is an element of the set

Z[D]={a+bD∣a,b∈Z}\mathbb{Z}[\sqrt{D}] = \{a + b\sqrt{D} \mid a, b \in \mathbb{Z} \}

The coefficients c0,⋯ ,cnc_{0}, \cdots, c_{n} of ϕA(x)\phi_{A}(x) are also elements of Z[D]\mathbb{Z}[\sqrt{D}], and this can be proven. Each ci=pi+qiDc_{i} = p_{i} + q_{i}\sqrt{D} has unique integers pip_{i} and qiq_{i}. Given an integer MM, write a program that outputs the remainder of each pip_{i} and qiq_{i} divided by MM.

Input

The first line contains the size nn of the matrix, the integer MM used for division, and the integer DD.

Each of the next nn lines contains 2n2n integers. If the 2j−12j-1-th number on line ii is xx and the 2j2j-th number is yy, then aij=x+yDa_{ij} = x + y\sqrt{D}.

Output

Print n+1n+1 lines. On line ii, print the remainders of pip_{i} and qiq_{i} divided by MM, separated by a space.

The remainder of an integer aa divided by MM is the smallest non-negative integer bb such that a−ba - b is a multiple of MM.

Constraints

  • 1≤n≤1001 \le n \le 100
  • 1≤M≤1091 \le M \le 10^{9}
  • −109≤D≤109-10^{9} \le D \le 10^{9}
  • D≠1D \neq 1, and DD is not divisible by any perfect square greater than 1.
  • For all 1≤i,j≤n1 \le i, j \le n, if aij=x+yDa_{ij} = x + y\sqrt{D}, then 0≤x,y<M0 \le x, y < M.

Hints

  • For each x∈Z[D]x \in \mathbb{Z}[\sqrt{D}], there are unique integers a,ba, b such that x=a+bDx = a + b\sqrt{D}.
  • (a+bD)+(c+dD)=(a+c)+(b+d)D(a + b\sqrt{D}) + (c + d\sqrt{D}) = (a + c) + (b + d)\sqrt{D}
  • (a+bD)(c+dD)=(ac+bdD)+(ad+bc)D(a + b\sqrt{D})(c + d\sqrt{D}) = (ac + bdD) + (ad + bc)\sqrt{D}

Examples1

  1. Example 1

    Input
    3 107 -5
    3 1 4 1 5 9
    2 6 5 3 5 8
    9 7 9 3 2 3
    
    Expected output
    26 69
    2 31
    97 100
    1 0