QOJ.ac

QOJ

Time Limit: 0.5 s Memory Limit: 2048 MB Total points: 100

#7948. Fraction

الإحصائيات

基本分数可以用三个整数 $(a\ b\ c)$ 表示,代表 $a + \frac{b}{c}$,其中 $1 \le a, b, c \le 9$。扩展分数的形式为 $(a'\ b'\ c')$,其中 $a', b', c'$ 可以是 1 到 9 之间的整数,也可以是其他扩展分数。注意,基本分数也是一种扩展分数,且分数的长度是有限的。

给定一个扩展分数,我们希望将其值表示为一个既约分数。例如,$((1\ 2\ 4)(5\ 2\ 3)(4\ 3\ (2\ 7\ 3)))$ 的既约分数计算如下:

$$\left(1 + \frac{2}{4}\right) + \frac{5 + \frac{2}{3}}{4 + \frac{3}{2 + \frac{7}{3}}} = \frac{991}{366}$$

给定一个扩展分数的字符串形式,编写一个程序将其转换为既约分数。

输入格式

程序从标准输入读取数据。输入的第一行包含一个整数 $n$ ($2 \le n \le 100$),表示由括号和 1 到 9 之间的数字组成的符号总数。第二行包含这些符号,以空格分隔,表示一个扩展分数。

输出格式

程序将结果写入标准输出。仅打印一行。如果答案为 $x/y$,则该行应包含两个互质的整数 $x$ 和 $y$。否则(例如,当输入无效时),打印 -1。你需要使用 64 位整数来获得正确答案。

样例

输入 1

5
( 1 2 3 )

输出 1

5 3

输入 2

8
( 1 2 ( 3 4 5 ) )

输出 2

-1

输入 3

21
( ( 1 2 4 ) ( 5 2 3 ) ( 4 3 ( 2 7 3 ) ) )

输出 3

991 366

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.