Common Polynomial#5794

Common Polynomial

1 second
64 megabytes
Very easy

Math teacher Mr. Matsudaira is teaching expansion and factoring of polynomials to his students. Last week he instructed the students to write two polynomials (with a single variable ), and to report GCM (greatest common measure) of them as a homework, but he found it boring to check their answers manually. So you are asked to write a program to check the answers.

Hereinafter, only those polynomials with integral coefficients, called integral polynomials, are considered.

When two integral polynomials and are given, an integral polynomial is a common factor of and if there are some integral polynomials and such that A = CX and B = CY.

GCM of two integral polynomials is a common factor which has the highest degree (for , here); you have to write a program which calculates the GCM of two polynomials.

It is known that GCM of given two polynomials is unique when constant multiplication factor is ignored. That is, when and are both GCM of some two polynomials and , p \times C = q \times D for some nonzero integers and .

Input

The input consists of multiple datasets. Each dataset constitutes a pair of input lines, each representing a polynomial as an expression defined below.

  1. A primary is a variable x, a sequence of digits –, or an expression enclosed within (...). Examples: x, 99, (x+1).

  2. A factor is a primary by itself or a primary followed by an exponent. An exponent consists of a symbol ^ followed by a sequence of digits –. Examples: x^05, 1^15, (x+1)^3.

  3. A term consists of one or more adjoining factors. Examples: 4x, (x+1)(x-2), 3(x+1)^2.

  4. An expression is one or more terms connected by either + or -. Additionally, the first term of an expression may optionally be preceded with a minus sign -. Examples: -x+1, 3(x+1)^2-x(x-1)^2.

Integer constants, exponents, multiplications (adjoining), additions (+) and subtractions/negations (-) have their ordinary meanings. A sequence of digits is always interpreted as an integer constant. For example, 99 means , not 9 \times 9.

Any subexpressions of the input, when fully expanded normalized, have coefficients less than and degrees of less than . Digit sequences in exponents represent non-zero values.

All the datasets are designed so that a standard algorithm with -bit two's complement integers can solve the problem without overflows.

The end of the input is indicated by a line containing a period.

Output

For each of the dataset, output GCM polynomial expression in a line, in the format below.

c_0 x^{p_0} \pm c_1 x^{p_1} \ldots \pm c_n x^{p_n}

Where c_i and p_i (i = 0, \ldots, n) are positive integers with p_0 > p_1 > \ldots > p_n, and the greatest common divisor of \{c_i | i = 0, \ldots, n\} is .

Additionally:

  1. When c_i is equal to , it should be omitted unless corresponding p_i is ,

  2. x^0 should be omitted as a whole, and

  3. x^1 should be written as x.

Examples

Example 1

Loading
Loading