codeforces 1156F Card Bag 概率dp

Card Bag

状态只会从a小转移到a大,随便dp就好了。

#include<bits/stdc++.h>
using namespace std;

const int N = 5000 + 7;
const int mod = 998244353;

int n, a[N], sum[N], dp[N][N], sum_dp[N];
int inv[N];

int main() {
    inv[1] = 1;
    for(int i = 2; i < N; i++) {
        inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
    }
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        int x; scanf("%d", &x);
        a[x]++;
    }
    for(int i = 1; i < N; i++) {
        sum[i] = sum[i - 1] + a[i];
    }
    dp[0][0] = 1;
    for(int j = 1; j <= n; j++) {
        sum_dp[0] = dp[0][j - 1];
        for(int i = 1; i < N; i++) {
            sum_dp[i] = (sum_dp[i - 1] + dp[i][j - 1]) % mod;
        }
        for(int i = 1; i < N; i++) {
            dp[i][j] = 1LL * sum_dp[i - 1] * inv[n - j + 1] % mod * a[i] % mod;
        }
    }
    int ans = 0;
    for(int i = 1; i < N; i++) {
        if(a[i] < 2) continue;
        for(int j = 1; j < n; j++) {
            ans += 1LL * (a[i] - 1) * inv[n - j] % mod * dp[i][j] % mod;
            if(ans >= mod) ans -= mod;
        }
    }
    printf("%d\n", ans);
    return 0;
}

/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/11615265.html

时间: 2025-01-14 19:00:55

codeforces 1156F Card Bag 概率dp的相关文章

HDU 4336 Card Collector(概率DP)

 题意:有n种卡片,吃零食的时候会吃到一些卡片,告诉你在一袋零食中吃到每种卡片的概率,求搜集齐每种卡片所需要买零食的袋数的期望. 思路:先状态压缩,然后概率DP 用d[i]表示由状态i到目标需要再买多少包,则状态转移方程为d[i] = p'*(d[i]+1) + sigma(d[ i | (1 << j) * p[i] ),然后相同项移到左边,最后就可以得到答案. #include<cstdio> #include<cstring> #include<cmat

codeforces 482c 状压+概率DP

题意:给出N个不同的串,长度一样,别人随机选一个串,你要询问他那个串某一个位置是什么字符直到能确定那个串才能停止,问询问次数的期望. 题解:50个串20个位置容易想到状压,把字符串长度状压先考虑能否在某一个状态确定哪些字符串能确定哪些不能确定,需要2^m*m次,然后时间上不能再乘以n不然会爆,想想只要我知道到达某一个猜位置状态的概率dp[i],再知道相对应有哪些字符串可以确定和不可以确定,用f[i]来表示,那么对于不能确定的字符串相当于就要再猜一步,那么加上这个状态的概率就行了,不会再需要乘以n

codeforces Name That Tune (概率dp)

题意: D - Name That Tune Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 499D Appoint description:  System Crawler  (2015-01-05) Description It turns out that you are a great fan of rock b

HDU4336 Card Collector 概率DP求期望+状压

题目大意:要集齐N张卡片,每包干脆面出现每种卡片的概率已知,问你集齐N张卡片所需要的方便面包数的数学期望(N<=20). solution: 由于N<=20,我们可以考虑状压,设dp[S]表示牌的状态为S时的需要的方便面包数的数学期望. 那么,对于每一个状态,考虑枚举每一张牌i(摸到了i),此时: ① 如果S中不含i,dp[S]+=(dp[S|(1<<i-1)]+1)*p[i]. ② 如果S中已经包含i,那么算到下面的情况中去. 但是注意到,上述情况是已经保证了摸到牌,但是其实可以

Codeforces 148D Bag of mice (概率dp)

D. Bag of mice time limit per test:2 seconds memory limit per test:256 megabytes The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, wh

Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃圾,大哥拿来了一袋老鼠,其中有w只白老鼠和b只黑老鼠.胡小兔先抓,先抓到白老鼠的人赢. 每次学姐抓完老鼠之后,总会有另外一只老鼠从袋子里自己跑出来(这只老鼠不算任何人抓的),而胡小兔抓老鼠时则不会发生这样的事. 每次袋子里的每只老鼠被抓到的概率相等,当有一只老鼠跑出来的时候,每只老鼠跑出来的几率也相

HDU 4336——Card Collector——————【概率dp】

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3407    Accepted Submission(s): 1665Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

Bag of mice(概率DP)

Bag of mice  CodeForces - 148D The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed

HDU 4336 Card Collector(动态规划-概率DP)

Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. As a