洛谷P3600 隨機數生成器(期望dp 組合數)

自為風月馬前卒發表於2019-02-25

題意

題目連結

Sol

一條重要的性質:如果某個區間覆蓋了另一個區間,那麼該區間是沒有用的(不會對最大值做出貢獻)

首先不難想到列舉最終的答案(x)。這時我們需要計算的是最大值恰好為(x)的概率。

發現不是很好搞,我們記(P(x))表示最大值(leqslant x)的概率,那麼恰好為(x)的概率為(P(x) – P(x – 1))

計算概率可以直接用定義:合法的方案/總方案((x^n))

考慮如何計算合法方案:我們直接去列舉在詢問區間中有多少個點(leqslant x),設(g(j))表示選出(j)(leqslant x)的點且覆蓋了所有詢問區間的方案,顯然這樣可以做到不重不漏。

接下來直接dp計算(g[j]),設(f[i][j])表示覆蓋了前(i)個位置,放了(j)個點的方案數,且(i)位置必須放的方案數。

(f[i][j] = sum_{fr[k] + 1 leqslant fl[i]} f[k][j – 1])

(fr[i])表示覆蓋了(i)區間的最右區間的編號,(fl[i])表示覆蓋了(i)的最左區間的編號

轉移的時候拿單調棧搞一下

複雜度(O(n^2 logn))

#include<bits/stdc++.h> 
#define Pair pair<LL, LL>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2001, mod =666623333, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << `
`;}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N, X, Q, flag[MAXN], fl[MAXN], fr[MAXN], g[MAXN], cnt, tmp[MAXN], f[MAXN][MAXN], que[MAXN], top, sum[MAXN];
int fp(int a, int p) {
    int base = 1;
    while(p) {
        if(p & 1) base = mul(base, a);
        a = mul(a, a); p >>= 1;
    }
    return base;
}
int inv(int x) {
    return fp(x, mod - 2);
}
int solve(int x) {//find the probability that max <= x
    int now = 0;
    for(int i = 1; i <= N; i++) add2(now, mul(g[i], mul(fp(x, i), fp(X - x, N - i))));//choose j point staticfiac
    return mul(now, inv(fp(X, N)));
}
Pair q[MAXN];
signed main() {
    Fin(a);
    N = read(); X = read(); Q = read();
    for(int i = 1; i <= Q; i++) q[i].fi = read(), q[i].se = read();
    for(int i = 1; i <= Q; i++) 
        for(int j = 1; j <= Q; j++) 
            if(!flag[j] && (i != j) && (q[i].fi <= q[j].fi && q[i].se >= q[j].se)) //不加!flag[j]會wa,因為可能左右端點都相同 
                flag[i] = 1;
    for(int i = 1; i <= Q; i++) if(!flag[i]) q[++cnt] = q[i];
    Q = cnt;
    sort(q + 1, q + Q + 1);
    memset(fl, 0x3f, sizeof(fl));
    for(int i = 1; i <= Q; i++)
        for(int j = 1; j <= N; j++)
            if(q[i].fi <= j && q[i].se >= j) chmin(fl[j], i), chmax(fr[j], i);
            else if(q[i].se < j) chmax(fr[j], i);
    for(int i = 1; i <= N; i++) if(q[fr[i]].se < i) fl[i] = fr[i] + 1;

    /*
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= min(i, N); j++)
            for(int k = 0; k < i; k++)
                if(fr[k] + 1 >= fl[i]) add2(f[i][j], f[k][j - 1]);
    */
    f[0][0] = 1;
    int l = 1, r = 1; que[1] = 0; sum[0] = 1;
    for(int i = 1; i <= N; i++) {
        while(l <= r && fr[que[l]] + 1 < fl[i]) {
            for(int j = 0; j <= N; j++) 
                add2(sum[j], -f[que[l]][j]);
            l++;
        }
        for(int j = 1; j <= min(i, N); j++) f[i][j] = sum[j - 1];
        for(int j = 1; j <= N; j++) add2(sum[j], f[i][j]);
        que[++r] = i;
    }
    for(int i = 1; i <= N; i++)
        if(fr[i] == Q)
            for(int j = 1; j <= N; j++)
                add2(g[j], f[i][j]);
    LL ans = 0;
    for(int i = 1; i <= N; i++) tmp[i] = solve(i);
    for(int i = 1; i <= X; i++) add2(ans, mul(i, add(tmp[i], -tmp[i - 1])));
    cout << ans;
    return 0;
}
/*
32 4
*/

相關文章