#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 star. The passage stretches for hundreds of light-years.
There are many signposts along the passage. The -th signpost shows a fraction and a fraction operator or .
At the beginning, you are holding the number . 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 , indicating that there are signposts in the passage.
Lines to each contain three positive integers , meaning that the fraction on the -th signpost is and the operator is .
Here, the two operations and are represented by and , respectively.
Output Format
Output one number in a single line.
If the final result can be kept as an integer, output an integer as the result.
Otherwise, if the final result must be kept as a fraction, output a reduced fraction in the form a/b. You must ensure that is in lowest terms, i.e. .
Note that the result may be negative, and in that case you must keep the minus sign. For example, if the result is , 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 of the testdata, the operator is only addition.
For another of the testdata, the operator is only subtraction.
For of the testdata, it is guaranteed that , , , and the answer and all intermediate values (integer part / numerator / denominator) do not exceed .
Translated by ChatGPT 5