P28232 [洛谷] "Chemistry" Relative Molecular Mass
提高-
题目描述

Problem Description

When solving chemistry problems, Xiao F often calculates the relative molecular mass incorrectly, which frustrates him greatly.

Xiao F has come to you, asking for your help to calculate the relative molecular mass of a given substance.

If you haven't studied this topic, don't worry. You can understand what the problem is asking for from the examples and hints.

Input Format

Input consists of a single line, containing a string of length $L$ $(L \leq 100)$ without spaces, representing the chemical formula of the given substance.

The chemical formula includes the following:

  • Elements: such as Au (gold), Hf (hafnium). All elements and their relative atomic masses are based on the attached table.
  • Subscripts _{}: Indicates the number of atoms, ions, or groups of atoms, such as H_{2}O for $H_2O$ (water), C_{60} for $C_{60}$ (fullerene).
  • Parentheses (): Indicates a group of atoms, where the subscript applies to the entire group. For example, Ca(OH)_{2} represents $Ca(OH)_2$ (slaked lime).
  • Hydrate ~: Such as CuSO_{4}~5H_{2}O representing $CuSO_4 \cdot 5H_2O$ (blue vitriol). If a number precedes the water, it is guaranteed to be a positive integer greater than 2. If omitted, it defaults to 1. For example, in the case of blue vitriol, the number 5 indicates the number of water molecules.

Formally, the chemical formula you process adheres to the following rules:

Molecule~QuantityH_{2}O

Where the quantity or hydrate part can be omitted.

For the molecule, it satisfies:

Part_{Quantity}Part_{Quantity}...Part_{Quantity}

Where the quantity can be omitted.

For each "part" (atom, group of atoms, ion, etc.), it satisfies:

Element

Or

(Element_{Quantity}Element_{Quantity}...Element_{Quantity})

Where the quantity can be omitted.

Note that chemical formulas meeting the above conditions will not have nested parentheses; the numbers referred to as "quantity" do not exceed $10000$.

Output Format

Output a single line, containing an integer or a real number with a fractional part of .5, representing your calculation result.

The result is guaranteed to not exceed $10000$.

Sample Input and Output

Input Example #1

Au

Output Example #1

197

Input Example #2

HfO_{2}

Output Example #2

210.5

Input Example #3

Ca(OH)_{2}

Output Example #3

74

Input Example #4

CuSO_{4}~5H_{2}O

Output Example #4

250

Input Example #5

KAl(SO_{4})_{2}~12H_{2}O

Output Example #5

474

Notes

Sample Explanation

Gold $Au: 197$

Hafnium dioxide $HfO_{2}: 178.5 + 2 \times 16 = 210.5$

Calcium hydroxide (slaked lime) $Ca(OH)_2: 40 + (1 + 16) \times 2 = 74$

Copper sulfate pentahydrate (blue vitriol) $CuSO_4 \cdot 5H_2O: 64 + 32 + 16 \times 4 + 5 \times (1 \times 2 + 16) = 250$

Potassium aluminum sulfate dodecahydrate (alum) $KAl(SO_{4})_{2} \cdot 12H_{2}O$ (feel it in your heart that $474$ is correct)

Attached Table: Relative Atomic Masses of Useful Elements

Element Relative Atomic Mass
H 1
C 12
N 14
O 16
F 19
Na 23
Mg 24
Al 27
Si 28
P 31
S 32
Cl 35.5
K 39
Ca 40
Mn 55
Fe 56
Cu 64
Zn 65
Ag 108
I 127
Ba 137
Hf 178.5
Pt 195
Au 197
Hg 201

Hints

If you haven't studied this topic, the following might be helpful:

zcy teaches you how to calculate relative molecular mass

  1. Each element starts with a capital letter;
  2. The method to calculate molecular mass is to sum the product of each atom's occurrence times its relative atomic mass;
  3. ~ ( $\cdot$ ) is equivalent to addition;
  4. Subscripts are equivalent to multiplication.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
Au
样例输出
197