|시간 제한||메모리 제한||제출||정답||맞힌 사람||정답 비율|
|1 초||128 MB||0||0||0||0.000%|
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:
Your task is to write a program to find appropriate coefficients x1, x2,..., xM and y1, y2,..., yN to balance an (imbalanced) equation like
A1 + A2 + A3 + ... + AM -> B1 + B2 + B3 + ... + BN
in the following way:
x1A1 + x2A2 + x3A3 + ... + xMAM -> y1B1 + y2B2 + y3B3 + ... + yNBN
The first line contains an integer t ( 1 ≤ t ≤ 10), the number of test cases. Each test case consists of a single line containing an expression like:
A1 + A2 + A3 + ... + AM = B1 + B2 + B3 + ... + BN
Each Ai or Bi is a formula of a molecule according to the rules given in items 1 and 2.
The input equations are given in a way that if they can be balanced, xi and yi coefficients can be in the range of 1 to 9. There are less than 10 molecules and there are less than 10 different types of atoms, in a given equation. Additionally, you may assume molecules contain no more than 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 200 characters.
The output will be one line per test case containing the list of required coefficients, separated by blank characters, in the following order:
x1 x2 ... xM y1 y2 ... yN
The coefficients should be integers in the range of 1..9. Obviously, there may be more than one answer for a test case. In such situations, print the answer which minimizes the number:
(This is an (M + N)-digit decimal number whose digits are xi and yi coefficients.) If the equation is impossible to balance, the output line should be `IMPOSSIBLE'.
3 HCl+CaO2H2=CaCl2+H2O HCl+H2SO4=NaCl HCl+NaOH=NaCl+H2O
2 1 1 2 IMPOSSIBLE 1 1 1 1