概率DP初见之2020牛客寒假算法基础集训营第二场C题 算概率

虽然给出的每个题目的概率是在模数意义下,但是一样算,至于为什么原因就是就是这样。。。。哈哈哈

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2000 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
LL p[maxn];
LL dp[maxn][maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    dp[0][0] = 1;
    // 1一个题不做 做错一个的概率 是 0
    // 很不抽象
    for (int i = 1; i <= n; i++)
    {
        dp[i][0] = dp[i - 1][0] * (mod + 1 - p[i]) % mod;
        // 当前题目也没做对的概率
        for (int j = 1; j <= i; j++)
        {
            dp[i][j] = (dp[i - 1][j] * (mod + 1 - p[i]) + dp[i - 1][j - 1] * p[i]) % mod;
            // 前i个题做对j个题目的概率是前i-1个题目做对j题的概率乘第i个题目不对的概率+前i-1个题做对j-1个的概率乘第i个题目做对的概率
        }
    }
    for (int i = 0; i <= n; i++)
        cout << dp[n][i] << " ";
    return 0;
}
发布了68 篇原创文章 · 获赞 24 · 访问量 9752

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/104215775
今日推荐