logo AlgoBeat OnlineJudge
登录 注册

#214233. [ICPC 2023 Seoul R] Fraction

内存限制:1024 MiB 时间限制:500 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

A basic fraction can be represented by three integers which denotes where . An extended fraction has the form of where , and may be integers between one and nine or other extended fractions. Note that a basic fraction is also an extended fraction, and the length of the fraction is finite.

Given an extended fraction, we want to express its value as irreducible fraction. For example, the irreducible fraction of is as follows.

Given a string form of an extended fraction, write a program that converts the extended fraction into the irreducible fraction.

输入格式

Your program is to read from standard input. The input starts with a line containing one integer (), where is the number of symbols which are parentheses and digits between and . The second line contains symbols, separated by a space, which represent an extended fraction.

输出格式

Your program is to write to standard output. Print exactly one line. If the answer is the line should contain two integers and , which are relatively prime to each other. Otherwise, (for example, when the input is not valid) print -1. You will need 64-bit integers to get the correct answer.

样例

样例输入 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