#P8584. 探索未知

探索未知

Background

Attached file:

https://luogu-ipic.oss-cn-shanghai.aliyuncs.com/watujir2-jdi92.zip

Problem Description

In the year 2102, humans have built an interstellar passage from Earth to the Virgo α\alpha star. The passage stretches for hundreds of light-years.

There are many signposts along the passage. The ii-th signpost shows a fraction aibi\dfrac{a_i}{b_i} and a fraction operator ++ or -.

At the beginning, you are holding the number 00. You walk backward along the passage. Each time you reach a signpost, you use the number in your hand and the fraction on the signpost, apply the operator on the signpost to compute a new number, and then hold this new number (discarding the old one). Note that if the computed number is not an integer, it will be kept as a reduced fraction; otherwise, it will be kept as an integer.

Now you want to know what number you will be holding when you reach the end of the passage.

Input Format

The first line contains one positive integer nn, indicating that there are nn signposts in the passage.

Lines 22 to n+1n+1 each contain three positive integers ai,bi,optia_i, b_i, opt_i, meaning that the fraction on the ii-th signpost is aibi\dfrac{a_i}{b_i} and the operator is optiopt_i.

Here, the two operations ++ and - are represented by 11 and 22, respectively.

Output Format

Output one number in a single line.

If the final result can be kept as an integer, output an integer ansans as the result.

Otherwise, if the final result must be kept as a fraction, output a reduced fraction ab\dfrac{a}{b} in the form a/b. You must ensure that ab\dfrac{a}{b} is in lowest terms, i.e. gcd(a,b)=1\gcd(a,b)=1.

Note that the result may be negative, and in that case you must keep the minus sign. For example, if the result is 114514-\dfrac{11451}{4}, you should output -11451/4.

3
3 5 1
4 7 2
7 3 1
248/105
4
6 7 1
8 3 2
9 14 2
5 17 1
-1541/714
3
7 4 1
3 8 2
3 8 2

1

Hint

For 20%20\% of the testdata, the operator is only addition.

For another 20%20\% of the testdata, the operator is only subtraction.

For 100%100\% of the testdata, it is guaranteed that 1n1031 \leq n \leq 10^3, 0a10000 \leq a \leq 1000, 0<b10000 < b \leq 1000, and the answer and all intermediate values (integer part / numerator / denominator) do not exceed 2×1092 \times 10^9.

Translated by ChatGPT 5