Codeforces 248E Piglet's Birthday dp

Piglet's Birthday

dp [i] [j] represents the current location, as well as the probability of the j not eaten honey on the i-th shelf.

j is not going to increase, so the complexity is right. But I feel I calculate the accuracy of the process is not very OK, ah, how have before. .

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-10;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q, a[N], sum[N];
double ans, C[N][107];

double dp[N][107], f[107];

void addPot(int id, int k) {
    sum[id] += k;
}

void subPot(int id, int k) {
    double tmp = dp[id][0];
    memset(f, 0, sizeof(f));
    for(int i = 0; i <= 100; i++) {
        for(int j = 0; j <= k; j++) {
            f[i] += dp[id][i + j] * C[i + j][j] * C[sum[id] - i - j][k - j] / C[sum[id]][k];
        }
    }
    for(int i = 0; i <= 100; i++) {
        dp[id][i] = f[i];
    }
    ans += dp[id][0] - tmp;
    sum[id] -= k;
}

int main() {

    for(int i = 0; i < N; i++) {
        for(int j = C[i][0] = 1; j <= min(100, i); j++) {
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }

    scanf("%d", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        sum[i] = a[i];
        dp[i][a[i]] = 1;
        ans += dp[i][0];
    }

    scanf("%d", &q);

    while(q--) {

        int u, v, k;
        scanf("%d%d%d", &u, &v, &k);

        subPot(u, k);
        addPot(v, k);

        printf("%.12f\n", ans);
    }
    return 0;
}

/*
*/

 

Guess you like

Origin www.cnblogs.com/CJLHY/p/11120105.html