1 人解决，1 人已尝试。
1 份提交通过，共有 1 份提交。
8.9 EMB 奖励。
单点时限: 1.0 sec
内存限制: 256 MB
You have landed a lucrative contract with Amalgamated Chemical Manufacturing (ACM), to help their chemists with stoichiometry. Stoichiometry is the calculation of reactants and products in chemical reactions, based on the law of conservation of mass, which states that the total mass of the reactants equals the total mass of the products.
The relations among quantities of reactants and products typically form a ratio of positive integers. If the amounts of the separate reactants are known, then the amount of the product can be calculated, and vice-versa. The relationship of
reactants to products can be described using a soichiometric equation such as:
which can be read as: “One molecule of and two molecules of yield one molecule of and two molecules of .” The total number of atoms of each element on the left hand side of the stoichiometric equation must match the number of atoms of that element on right hand side. Your task is to write a program that, given an equation of the form:
will fill in the blanks to produce a balanced equation. For example, the above equation could be balanced as follows:
An equation is input in the form of a sequence of lines, one for each molecule in the formula (e.g., or ). Each line () has the following fields:
where is either
-1, with a sign of
+1 indicating that this molecule appears on the left of the
-1 indicating that it appears on the right. , where , is the number of / pairs following on the line. Each , where , is an element name consisting of one or two upper or lowercase letters, and each is a positive integer, . For example, the element/count pair
Fe 2 indicates that the molecule contains two atoms of the element Fe (iron). There will be no more than 10 unique elements in a single equation.
Note that an element may be repeated in a given line of input, as in
+1 6 C 1 H 5 C 1 O 1 O 1 H 1 which specifies that at least one molecule of appears on the left side of the equation. Note that can be written as .
Input ends with a line of the form
The program must output the numbers , in order, to fill in the blanks in the equation. Each number, , must be the minimum number for which the equation is balanced (i.e. there is no common factor that could reduce all of the coefficients). You may assume that every input test case has exactly one valid solution meeting these criteria.
+1 2 H 2 O 1 +1 2 C 1 O 2 -1 1 O 2 -1 3 C 6 H 12 O 6 0 0
6 6 6 1
+1 5 Be 2 C 1 O 3 O 2 H 2 +1 3 Ac 1 O 1 H 1 -1 4 Be 4 O 1 Ac 6 O 6 -1 2 H 2 O 1 -1 2 C 1 O 2 0 0
2 6 1 5 2