#P4711. 「化学」相对分子质量

    ID: 3404 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟字符串递归Special JudgeO2优化洛谷月赛

「化学」相对分子质量

Description

When doing chemistry problems, Xiao F always miscalculates the relative molecular mass, which makes him very distressed.

Xiao F came to you for help. Please compute the relative molecular mass of the given substance.

If you have not studied the related content, that is fine. You can understand what is required from the samples and the hints.

Input Format

Input consists of one line: a string of length LL (L100)(L \leq 100) without spaces, representing the chemical formula of the given substance.

The chemical formula includes only the following:

  • Elements: such as Au (gold), Hf (hafnium). All occurring elements and their relative atomic masses are given in the appendix.
  • Subscript _{}: indicates the count of some atom, ion, or group. For example, H_{2}O means H2O\text{H}_2\text{O} (water), and C_{60} means C60\text{C}_{60} (buckminsterfullerene).
  • Parentheses (): indicate a group, and a subscript applies to the content inside the group. For example, Ca(OH)_{2} means Ca(OH)2\text{Ca(OH)}_2 (slaked lime).
  • Hydrate ~: for example, CuSO_{4}~5H_{2}O means CuSO45H2O\text{CuSO}_4 \cdot 5\text{H}_2\text{O} (chalcanthite). If there is a number before water, it is guaranteed to be a positive integer greater than 22. If this part is omitted, it defaults to 11. In the example above, the 5 indicates the number of water molecules.

Formally, the chemical formula you process satisfies the following rules:

Molecule ~ Count H_{2}O

The count or the hydrate part may be omitted.

For the molecule, it satisfies:

Part _{ Count } Part _{ Count } ... Part _{ Count }

The count may be omitted.

For each “Part” (atom, group, ion, etc.), it satisfies:

Element

or

( Element _{ Count } Element _{ Count } ... Element _{ Count })

The count may be omitted.

Please note that chemical formulas satisfying the above conditions will not contain nested parentheses. The number referred to as “Count” above does not exceed 1000010000.

Output Format

Output one line containing either an integer or a real number whose fractional part is .5, which is your computed result.

It is guaranteed that the result does not exceed 1000010000.

Au
197
HfO_{2}
210.5
Ca(OH)_{2}
74
CuSO_{4}~5H_{2}O
250
KAl(SO_{4})_{2}~12H_{2}O
474

Hint

Sample explanations

Gold Au:197\text{Au} : 197.

Hafnium dioxide HfO2:178.5+2×16=210.5\text{HfO}_2: 178.5 + 2 \times 16 = 210.5.

Calcium hydroxide (slaked lime) Ca(OH)2:40+(1+16)×2=74\text{Ca(OH)}_2 : 40 + (1 + 16) \times 2 = 74.

Copper(II) sulfate pentahydrate (chalcanthite) $\text{CuSO}_4 \cdot 5\text{H}_2\text{O}: 64 + 32 + 16 \times 4 + 5 \times (1 \times 2 + 16) = 250$.

Potassium aluminum sulfate dodecahydrate (alum) KAl(SO4)212H2O\text{KAl(SO}_4)_2 \cdot 12\text{H}_2\text{O} (Indeed, 474474 is correct.).

Appendix: Possible relative atomic masses

元素 相对原子质量
H\text{H} 11
C\text{C} 1212
N\text{N} 1414
O\text{O} 1616
F\text{F} 1919
Na\text{Na} 2323
Mg\text{Mg} 2424
Al\text{Al} 2727
Si\text{Si} 2828
P\text{P} 3131
S\text{S} 3232
Cl\text{Cl} 35.535.5
K\text{K} 3939
Ca\text{Ca} 4040
Mn\text{Mn} 5555
Fe\text{Fe} 5656
Cu\text{Cu} 6464
Zn\text{Zn} 6565
Ag\text{Ag} 108108
I\text{I} 127127
Ba\text{Ba} 137137
Hf\text{Hf} 178.5178.5
Pt\text{Pt} 195195
Au\text{Au} 197197
Hg\text{Hg} 201201

Tips

If you have not studied the related content, the following may help:

zcy teaches you how to compute relative molecular mass

  1. Each element starts with an uppercase letter.
  2. To compute the molecular mass, multiply the number of occurrences of each atom by its relative atomic mass and sum them up.
  3. ~( \cdot ) is equivalent to addition.
  4. A subscript is equivalent to multiplication.

Translated by ChatGPT 5