Educational Codeforces Round 79 (Rated for Div. 2) D. Santa's Bot

北慕城南 提交于 2020-03-21 10:26:47

链接:

https://codeforces.com/contest/1279/problem/D

题意:

Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of ki different items as a present. Some items could have been asked by multiple kids.

Santa is really busy, so he wants the New Year Bot to choose the presents for all children. Unfortunately, the Bot's algorithm of choosing presents is bugged. To choose a present for some kid, the Bot does the following:

choose one kid x equiprobably among all n kids;
choose some item y equiprobably among all kx items kid x wants;
choose a kid z who will receive the present equipropably among all n kids (this choice is independent of choosing x and y); the resulting triple (x,y,z) is called the decision of the Bot.
If kid z listed item y as an item they want to receive, then the decision valid. Otherwise, the Bot's choice is invalid.

Santa is aware of the bug, but he can't estimate if this bug is really severe. To do so, he wants to know the probability that one decision generated according to the aforementioned algorithm is valid. Can you help him?
就是先选一个人,再从这个人的k个礼物选一个,再重选一个人,可以与第一个重复,如果这个人想要的礼物中有之前选的礼物,就是一个好的选择。
计算出现好的选择的概率。

思路:

算出取出每个值的概率,再遍历每个人,这个人的每个值被取出的概率乘上选择这个人的概率再累加一下

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 998244353;
const int MAXN = 1e6+10;

int P[MAXN];
int n;
vector<int> vec[MAXN];

LL PowMod(LL a, LL b, LL p)
{
    LL res = 1;
    while(b)
    {
        if (b&1)
            res = res*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return res;
}

LL GetP(LL x, LL y)
{
    return x*PowMod(y, MOD-2, MOD)%MOD;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    int k, val;
    for (int i = 1;i <= n;i++)
    {
        cin >> k;
        for (int j = 1;j <= k;j++)
        {
            cin >> val;
            vec[i].push_back(val);
        }
    }
    LL pone = GetP(1, n);
    for (int i = 1;i <= n;i++)
    {
        int sum = vec[i].size();
        for (auto v: vec[i])
            P[v] = (P[v]+pone*GetP(1, sum)%MOD)%MOD;
    }
    LL ans = 0;
    for (int i = 1;i <= n;i++)
    {
        for (auto v: vec[i])
            ans = (ans + pone*P[v]%MOD)%MOD;
    }
    cout << ans << endl;

    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!