#P8774. [蓝桥杯 2022 省 A] 爬树的甲壳虫

[蓝桥杯 2022 省 A] 爬树的甲壳虫

Problem Description

A beetle wants to climb a tree of height nn. It starts at the root, at height 00. When it tries to climb from height i1i-1 to height ii, it falls back to the root with probability PiP_{i}. Find the expected time it takes to climb from the root to the top of the tree.

Input Format

The first line contains an integer nn, the height of the tree.

The next nn lines each contain two integers xi,yix_{i}, y_{i}, separated by a space, meaning Pi=xiyiP_{i}=\frac{x_{i}}{y_{i}}.

Output Format

Output one line containing one integer, representing the answer. The answer is a rational number; output its value modulo the prime 998244353998244353. For a rational number ab\frac{a}{b}, its value modulo a prime PP is the integer cc such that 0c<P0 \leq c<P and cba(modP)c \cdot b \equiv a\pmod P.

1
1 2
2
3
1 2
3 5
7 11
623902744

Hint

Constraints:

For 20%20\% of the testdata, n2n \leq 2, 1xi<yi201 \leq x_{i}<y_{i} \leq 20.

For 50%50\% of the testdata, n500n \leq 500, 1xi<yi2001 \leq x_{i}<y_{i} \leq 200.

For all testdata, 1n1051 \leq n \leq 10^5, 1xi<yi1091 \leq x_{i}<y_{i} \leq 10^{9}.

Lanqiao Cup 2022 NOI Qualifier A Group, Problem E.

Translated by ChatGPT 5