eolymp
bolt
Try our new interface for solving problems
Məsələlər

Common Polynomial

Common Polynomial

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 \textbf{x}), and to report \textbf{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 \textbf{A} and \textbf{B} are given, an integral polynomial \textbf{C} is a common factor of \textbf{A} and \textbf{B} if there are some integral polynomials \textbf{X} and \textbf{Y} such that \textbf{A = CX} and \textbf{B = CY}. \textbf{GCM} of two integral polynomials is a common factor which has the highest degree (for \textbf{x}, here); you have to write a program which calculates the \textbf{GCM} of two polynomials. It is known that \textbf{GCM} of given two polynomials is unique when constant multiplication factor is ignored. That is, when \textbf{C }and \textbf{D} are both \textbf{GCM} of some two polynomials \textbf{A} and \textbf{B}, \textbf{p}×\textbf{C} = \textbf{q}×\textbf{D} for some nonzero integers \textbf{p} and \textbf{q}. \InputFile The input consists of multiple datasets. Each dataset constitutes a pair of input lines, each representing a polynomial as an expression defined below. \begin{enumerate} \item A \textit{primary} is a variable \textbf{x}, a sequence of digits \textbf{0}--\textbf{9}, or an expression enclosed within (...). Examples: \textbf{x}, \textbf{99},\textbf{(x+1)}. \item A \textit{factor} is a primary by itself or a primary followed by an exponent. An exponent consists of a symbol \textbf{^}followed by a sequence of digits \textbf{0}--\textbf{9}. Examples: \textbf{x^05}, \textbf{1^15}, \textbf{(x+1)^3}. \item A \textit{term} consists of one or more adjoining factors. Examples: \textbf{4x}, \textbf{(x+1)(x-2)}, \textbf{3(x+1)^2}. \item An \textit{expression} is one or more terms connected by either \textbf{+} or \textbf{-}. Additionally, the first term of an expression may optionally be preceded with a minus sign \textbf{-}. Examples: \textbf{-x+1}, \textbf{3(x+1)^2-x(x-1)^2}. \end{enumerate} Integer constants, exponents, multiplications (adjoining), additions (\textbf{+}) and subtractions/negations (\textbf{-}) have their ordinary meanings. A sequence of digits is always interpreted as an integer constant. For example, \textbf{99} means \textbf{99}, not \textbf{9}×\textbf{9}. Any subexpressions of the input, when fully expanded normalized, have coefficients less than \textbf{100} and degrees of\textbf{x} less than \textbf{10}. Digit sequences in exponents represent non-zero values. All the datasets are designed so that a standard algorithm with \textbf{32}-bit two’s complement integers can solve the problem without overflows. The end of the input is indicated by a line containing a period. \OutputFile For each of the dataset, output \textbf{GCM} polynomial expression in a line, in the format below. \textbf{c_0x^p_0 ± c_1x^p_1...± c_nx^p_n} Where \textbf{c_i} and \textbf{p_i} (\textbf{i = 0}, ..., \textbf{n}) are positive integers with \textbf{p_0} > \textbf{p_1} > ... > \textbf{p_n}, and the greatest common divisor of \textbf{\{c_i | i = 0, ..., n\}} is \textbf{1}. Additionally: \begin{enumerate} \item When \textbf{c_i} is equal to \textbf{1}, it should be omitted unless corresponding \textbf{p_i} is \textbf{0}, \item \textbf{x^0} should be omitted as a whole, and \item \textbf{x^1} should be written as \textbf{x}. \end{enumerate}
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
-(x^3-3x^2+3x-1)
(x-1)^2
x^2+10x+25
x^2+6x+5
x^3+1
x-1
.
Çıxış verilənləri #1
x^2-2x+1 
x+5 
1
Mənbə Asia Regional Contest, Japan, AIZU, October 25-27, 2008