The Teacher's Side of Math

Time limit5sMemory limit128 MB

Summary
Given t = a^(1/m) + b^(1/n) for distinct primes a, b, compute the minimal polynomial over the integers of degree mn using resultant-style elimination.
Level

Medium7 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial equation: given a polynomial, say X2−4X+1X^2 - 4X + 1, find its roots 2±32 \pm \sqrt{3}.

If the students' task is to find the roots of a given polynomial, the teacher's task is to find a polynomial that has a given root. Ms. Galsone is an enthusiastic mathematics teacher who is bored with finding solutions of quadratic equations as simple as a+bca + b\sqrt{c}. She wants to make higher-degree equations whose solutions are a little more complicated. As usual in mathematics-class problems, she wants all coefficients to be integers and the degree of the polynomial to be as small as possible (provided it has the specified root). Please help her by writing a program that carries out the teacher's task.

You are given a number tt of the form

t=am+bnt = \sqrt[m]{a} + \sqrt[n]{b}

where aa and bb are distinct prime numbers and mm and nn are integers greater than 11.

You are asked to find tt's minimal polynomial over the integers, which is the polynomial F(X)=adXd+ad−1Xd−1+⋯+a1X+a0F(X) = a_d X^d + a_{d-1} X^{d-1} + \cdots + a_1 X + a_0 satisfying the following conditions.

  1. The coefficients a0,…,ada_0, \dots, a_d are integers and ad>0a_d > 0.
  2. F(t)=0F(t) = 0.
  3. The degree dd is the minimum among polynomials satisfying the above two conditions.
  4. F(X)F(X) is primitive; that is, the coefficients a0,…,ada_0, \dots, a_d have no common divisor greater than one.

For example, the minimal polynomial of 3+2\sqrt{3} + \sqrt{2} over the integers is F(X)=X4−10X2+1F(X) = X^4 - 10X^2 + 1. Verifying F(t)=0F(t) = 0 is as simple as the following (let α=3\alpha = \sqrt{3}, β=2\beta = \sqrt{2}).

F(t)=(α+β)4−10(α+β)2+1F(t) = (\alpha + \beta)^4 - 10(\alpha + \beta)^2 + 1

=(α4+4α3β+6α2β2+4αβ3+β4)−10(α2+2αβ+β2)+1= (\alpha^4 + 4\alpha^3\beta + 6\alpha^2\beta^2 + 4\alpha\beta^3 + \beta^4) - 10(\alpha^2 + 2\alpha\beta + \beta^2) + 1

=9+12αβ+36+8αβ+4−10(3+2αβ+2)+1= 9 + 12\alpha\beta + 36 + 8\alpha\beta + 4 - 10(3 + 2\alpha\beta + 2) + 1

=(9+36+4−50+1)+(12+8−20)αβ=0= (9 + 36 + 4 - 50 + 1) + (12 + 8 - 20)\alpha\beta = 0

Verifying that the degree of FF is in fact minimum is a bit more difficult. Fortunately, under the conditions given in this problem — aa and bb distinct primes and m,nm, n greater than one — the degree of the minimal polynomial is always mnmn. Moreover, it is always monic; that is, the coefficient of its highest-order term, ada_d, is one.

The input consists of multiple datasets, each in the following format.

a m b n

This line represents am+bn\sqrt[m]{a} + \sqrt[n]{b}. The last dataset is followed by a single line consisting of four zeros. Numbers in a single line are separated by a single space.

Every dataset satisfies the following conditions.

  1. am+bn≤4\sqrt[m]{a} + \sqrt[n]{b} \le 4.
  2. mn≤20mn \le 20.
  3. The coefficients of the answer a0,…,ada_0, \dots, a_d are between −231+1-231 + 1 and 231−1231 - 1, inclusive.

Input

The input consists of multiple datasets, each in the following format.

a m b n

This line represents am+bn\sqrt[m]{a} + \sqrt[n]{b}. The last dataset is followed by a single line consisting of four zeros. Numbers in a single line are separated by a single space.

Every dataset satisfies the following conditions.

  1. am+bn≤4\sqrt[m]{a} + \sqrt[n]{b} \le 4.
  2. mn≤20mn \le 20.
  3. The coefficients of the answer a0,…,ada_0, \dots, a_d are between −231+1-231 + 1 and 231−1231 - 1, inclusive.

Output

For each dataset, output the coefficients of its minimal polynomial over the integers F(X)=adXd+ad−1Xd−1+⋯+a1X+a0F(X) = a_d X^d + a_{d-1} X^{d-1} + \cdots + a_1 X + a_0, in the following format.

ad ad-1 ... a1 a0

Non-negative integers must be printed without a sign (++ or −-). Numbers in a single line must be separated by a single space, and no other characters or extra spaces may appear in the output.

Examples1

  1. Example 1

    Input
    3 2 2 2
    3 2 2 3
    2 2 3 4
    31 4 2 3
    3 2 2 7
    0 0 0 0
    
    Expected output
    1 0 -10 0 1
    1 0 -9 -4 27 -36 -23
    1 0 -8 0 18 0 -104 0 1
    1 0 0 -8 -93 0 24 -2976 2883 -32 -3720 -23064 -29775
    1 0 -21 0 189 0 -945 -4 2835 -252 -5103 -1260 5103 -756 -2183