题解:P16822 [蓝桥杯 2026 国 Python B] 零段积分
令
考虑对相邻两段的期望贡献求和,枚举第一段的右端点
我们将
将
这个式子可以
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
ll f[N], g[N];
ll qpow(ll a, ll b) {
ll r = 1;
while (b) b & 1 && (r = r * a % mod), b >>= 1, a = a * a % mod;
return r;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, p, q;
cin >> n >> p >> q;
p = p * qpow(q, mod-2) % mod;
int ans = 0;
for (int i = 1; i <= n; i++) f[i] = (f[i-1]+1) * (mod+1-p) % mod;
for (int i = n; i >= 1; i--) g[i] = (g[i+1]+1) * (mod+1-p) % mod;
for (int i = n; i >= 1; i--) g[i] = (g[i] * qpow(p, i) + g[i+1]) % mod; // 此时 g 相当于题解中的 h
for (int i = 1; i < n-1; i++) ans = (ans + f[i] * qpow(p, mod-2-i) % mod * g[i+2]) % mod;
cout << ans << "\n";
return 0;
}