Under construction

Still being built. Things may change

Matrix nightmare

Time limit1sMemory limit1024 MB

Summary
Given a multivariate integer polynomial, construct a small matrix of integers and variables whose weighted traversal sum equals that polynomial, or report that none exists.
Level

Hard10 of 10

Topics
Matrix, Combinatorics, Math, Implementation
Solved
No attempts yet

Problem

As Obi-Wan would put it: "This isn't the problem statement you are looking for. Move along."

The double factorial numbers are the numbers did_i defined by the following recursive formula: d0=1d_0 = 1, and ∀i>0\forall i > 0: di=di−1⋅i!d_i = d_{i-1} \cdot i!. For example, d3=1!⋅2!⋅3!=12d_3 = 1! \cdot 2! \cdot 3! = 12.

Sequences of length nn with all elements in the set {0,…,n−1}\{0, \dots ,n-1\} are called limited sequences of order nn. For example, (0,2,0,1)(0, 2, 0, 1) is a limited sequence of order 44. The set of all limited sequences of order nn is denoted SnS_n.

The spread factor of a sequence A=(a0,…,an−1)A = (a_0, \dots , a_{n-1}) is σ(A)=∏i=0n−1∏j=i+1n−1(ai−aj)\sigma(A) = \prod_{i=0}^{n-1}\prod_{j=i+1}^{n-1}(a_i - a_j).

There is a direct isomorphism between pairs of sequences and sequences of pairs. Let A,B∈SnA, B \in S_n with A=(a0,…,an−1)A = (a_0, \dots , a_{n-1}) and B=(b0,…,bn−1)B = (b_0, \dots , b_{n-1}). The corresponding sequence of pairs ((a0,b0),…,(an−1,bn−1))((a_0, b_0), \dots ,(a_{n-1}, b_{n-1})) is denoted PA,BP_{A,B}.

Pairs of integers are ordered lexicographically: (a,b)≤(c,d)(a, b) \le (c, d) if either a<ca < c, or (a=c∧b≤da = c \land b \le d). A sequence P=(p0,…,pn−1)P = (p_0, \dots , p_{n-1}) of integer pairs is ordered lexicographically if pi≤pi+1p_i \le p_{i+1} for all ii. Let ρ(P)=\rho(P) = [if PP is ordered lexicographically then 11 else 00].

Let MM be an n×nn \times n matrix with rows and columns indexed from 00 to n−1n - 1. Its elements are mr,cm_{r,c}. A matrix is called a Z\mathbb{Z}-var matrix if each element is either an integer or a variable. The nn-step traversal weight of MM is

φ(M)=1dn−12⋅∑A=(a0,…,an−1),A∈Sn∑B=(b0,…,bn−1),B∈Sn(ρ(PA,B)⋅∣ρ(A)∣⋅ρ(B)⋅∏i=0n−1mai,bi)\varphi(M) = \frac{1}{d_{n-1}^2} \cdot \sum_{A=(a_0,\dots ,a_{n-1}), A \in S_n} \sum_{B=(b_0,\dots ,b_{n-1}), B \in S_n} \left( \rho(P_{A,B}) \cdot |\rho(A)| \cdot \rho(B) \cdot \prod_{i=0}^{n-1} m_{a_i,b_i} \right)

Given is a multivariate polynomial pp with integer coefficients. Produce any reasonably small Z\mathbb{Z}-var matrix MM such that φ(M)=p\varphi(M) = p.

Input

The first line of the input contains an integer tt, the number of test cases. Each test case is preceded by a blank line.

Each test case is a single line describing the polynomial. The variables are the letters a through z, and the syntax will be clear from the input file. Each polynomial has fewer than 5050 operations (additions, subtractions and multiplications).

Output

For each test case, output one matrix in the following format: its size nn, then all its elements in row major order. The elements may be separated by any positive amount of whitespace. The size of the matrix must not exceed 7070. All integers must be between −109-10^9 and 10910^9, inclusive.

If no such matrix exists for a given polynomial, output a single zero instead.

Examples1

  1. Example 1

    Input
    1
    
    xy
    
    Expected output
    2
    1 y
    x 0