Dwango Programming Contest 6th -B

期望

按每个空隙计算 对于第$i$个空隙 对于第$j$个跨过这段的概率是$\frac{1}{i-j+1}$ 因为跨过当且仅当$[j+1,i]$之间都不先于$j$合并 求一个逆元前缀和即可

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, P = 1e9 + 7;
int n;
int x[maxn], inv[maxn];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> x[i];
    }
    inv[1] = 1;
    for(int i = 2; i <= n; ++i) {
        inv[i] = 1LL * (P - P / i) * inv[P % i] % P;
    }
    for(int i = 2; i <= n; ++i) {
        inv[i] = (inv[i] + inv[i - 1]) % P;
    }
    int ans = 0;
    for(int i = 1; i < n; ++i) {
        ans = (ans + 1LL * inv[i] % P * (x[i + 1] - x[i]) % P) % P;
    }
    for(int i = 1; i < n; ++i) {
        ans = 1LL * ans * i % P;
    }
    cout << ans << '\n';
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/19992147orz/p/12237096.html