杂题——小e跳房子

小e跳房子

题目描述

e最近迷上了“跳房子”的游戏,房子在一条长度为n的横轴上,1是起点,n是终点。

e从起点开始,每次可以往右跳一格(即使得当前的坐标位置+1),但是由于每个房子上面的摩擦系数μ不同,每次从房子i起跳时,有p i的概率因为脚滑回到起点(确实有点逆天),请问小e跳到终点的期望次数是多少?

最后的结果对109+7取模。

输入格式

第一行一个整数T表示测试用例个数。(1≤T≤1000)

对于每组测试用例:

一行一个整数n(1≤∑n≤106)。

接下来n−1行,每行两个整数表示(0≤a i<bi≤109,p i=a i/bi)。

输出格式

对于每组测试用例,输出一个整数表示答案。

输入样例1

1
2
3
4
5
6
7
8
9
2
3
1 2
3 4
5
1 8
2 7
3 5
1 9

输出样例1

1
2
12
375000015

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// using i128 = __int128_t;
const ll p = 1e9 + 7;

ll qpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}

void solve() {
int n;
cin >> n;
vector<ll> t(n + 1);
for (int i = 1; i < n; ++i) {
ll a, b;
cin >> a >> b;
a = b - a;
ll inv = qpow(a, p - 2);
t[i] = inv * b % p;
}
ll ans = t[1];
for (int i = 2; i < n; ++i) {
ans = (ans + 1) * t[i] % p;
}
cout << ans << "\n";
}

inline void STDIO() {
ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
return;
}
int main() {
STDIO();
int _ = 1;
cin >> _;
while (_--) solve();
return 0;
}