eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Balanced Chemical Equations

Balanced Chemical Equations

One cumbersome problem in chemistry is the task of making the number of atoms balanced in a chemical equation. Our problem is concerned with this. Chemists obey these rules when they present chemical equations: \begin{enumerate} \item Each element name is abbreviated by at most two letters. The first letter is always in upper-case and the second letter if exists, is a lower-case letter (e.g. Calcium is represented by \textbf{Ca}, Oxygen by \textbf{O}, and Chlorine by \textbf{Cl}). \item Each molecule is composed of a number of atoms. To represent a molecule, we concatenate the abbreviated names of its composite atoms. For example, NaCl represents Sodium Chloride. Each atom name may be followed by a frequency number. For example, Calcium Chloride \textbf{CaCl2} consists of one atom of Calcium and two atoms of Chlorine. If the frequency is not given, it is assumed to be \textbf{1} (so \textbf{HCl} is equivalent to \textbf{H1Cl1}). For the sake of simplicity, you may assume that the frequency of an occurrence of an atom is at most \textbf{9} (so we do not have \textbf{C11H22O11} in the problem input). Note that there may be several occurrences of the same atom in the molecule formula, like \textbf{H} atom in \textbf{CH3COOH}. \item In ordinary chemical reactions, a number of molecules combine and result in a number of other molecules. For example a well known sample of neutralization is: \end{enumerate} \textbf{2HCl + CaO2H2 }→\textbf{ CaCl2 + 2H2O} This means two molecules of chlorohydric acid (\textbf{HCl}) with one molecule of Calcium Hydroxide results in one molecule of Calcium Chloride (\textbf{CaCl2}) and two molecules of water. \begin{enumerate} \item In every chemical reaction, the total number of each atom in the right side of the equation equals the total number of that atom in the left side (that is why it is called an equation!). \end{enumerate} Your task is to write a program to find appropriate coefficients \textbf{x}_1, \textbf{x}_2, …, \textbf{x}_\{M \}and \textbf{y}_1, \textbf{y}_2, …, \textbf{y}_\{N \}to balance an (imbalanced) equation like \textbf{A_1+A_2+A_3+ … +A_M }→\textbf{ B_1+B_2+B_3+ … +B_N} in the following way: \textbf{x_1 A_1+ x_\{2 \}A_2+ x_\{3 \}A_3+ … + x_\{M \}A_M }→\textbf{ y_\{1 \}B_1+ y_\{2 \}B_2+ y_\{3 \}B_3+ … + y_\{N \}BN} \InputFile The first line contains an integer \textbf{t} (\textbf{1} ≤ \textbf{t} ≤ \textbf{10}), the number of test cases. Each test case consists of a single line containing an expression like: \textbf{A_1+A_2+A_3+ … +A_\{M \}= B_1+B_2+B_3+ … +B_N} Each \textbf{A}_\{i \}or \textbf{B}_\{i \}is a formula of a molecule according to the rules given in items \textbf{1} and \textbf{2}. The input equations are given in a way that if they can be balanced, \textbf{x}_\{i \}and_\{ \}\textbf{y_i}_\{ \}coefficients can be in the range of \textbf{1 }to \textbf{9}. There are less than \textbf{10} molecules and there are less than \textbf{10} different types of atoms, in a given equation. Additionally, you may assume molecules contain no more than \textbf{3} different kinds of atoms. You may also assume that there is no blank character in the input file, and the maximum length of the input lines is \textbf{200} characters. \OutputFile The output will be one line per test case containing the list of required coefficients, separated by blank characters, in the following order: \textbf{x_\{1 \} x_\{2 \} … x_\{M \} y_\{1 \} y_\{2 \} … y_N} The coefficients should be integers in the range of \textbf{1..9}. Obviously, there may be more than one answer for a test case. In such situations, print the answer which minimizes the number: \includegraphics{https://static.e-olymp.com/content/2c/2c0bc2d95a6cadaa09d7bc3d3f701b9c9409727e.jpg} (This is an \textbf{(M+N)}-digit decimal number whose digits are \textbf{x_i} and \textbf{y_i} coefficients.) If the equation is impossible to balance, the output line should be \textbf{IMPOSSIBLE}.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
3
HCl+CaO2H2=CaCl2+H2O
HCl+H2SO4=NaCl
HCl+NaOH=NaCl+H2O
Вихідні дані #1
2 1 1 2
IMPOSSIBLE
1 1 1 1