아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Fraction

시간 제한0.5초메모리 제한2048 MB

요약
토큰으로 주어진 중첩 확장 분수를 해석해 기약분수의 분자와 분모를 출력하고, 입력이 잘못되면 -1을 출력한다.
난이도

보통10점 중 5점

유형
스택, 구현, 수학, 정수론
정답자
아직 제출이 없습니다

문제

A basic fraction can be represented by three integers (a,b,c)(a\\, b\\, c) which denotes a+bca + \frac{b}{c} where 1≤a,b,c≤91 ≤ a, b, c ≤ 9. An extended fraction has the form of (a′,b′,c′)(a'\\, b'\\, c') where a′a', b′b' and c′c' 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 ((1,2,4)(5,2,3)(4,3(2,7,3)))\left((1\\, 2\\, 4)(5\\, 2\\, 3)\left(4\\, 3 (2\\, 7\\, 3) \right)\right) is as follows.

(1+24)+5+234+32+73=991366\left(1 + \frac{2}{4}\right) + \displaystyle\frac{5 + \displaystyle\frac{2}{3}}{4 + \displaystyle\frac{3}{2 + \displaystyle\frac{7}{3}}} = \displaystyle\frac{991}{366}

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 nn (2≤n≤1002 ≤ n ≤ 100), where nn is the number of symbols which are parentheses and digits between 11 and 99. 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 xx/yy, the line should contain two integers xx and yy, 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.

예제3

  1. 예제 1

    입력
    5
    ( 1 2 3 )
    
    예상 출력
    5 3
    
  2. 예제 2

    입력
    8
    ( 1 2 ( 3 4 5 )
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    21
    ( ( 1 2 4 ) ( 5 2 3 ) ( 4 3 ( 2 7 3 ) ) )
    
    예상 출력
    991 366