zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 79 (Rated for Div. 2) D. Santa's Bot

    链接:

    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;
    }
    
  • 相关阅读:
    Python单例模式中的4种方式
    Python list,tuple,dict,set高级变量常用方法
    python如何获取多个excel单元格的值
    两种方法实现python操作日志的封装
    numpy中函数shape的用法
    python中timer定时器常用的两种实现方法
    详解Python中argpasrse模块的基本使用
    在python中列表删除和多重循环退出
    Python的驻留机制(仅对数字,字母,下划线有效)
    python实现tail -f 功能
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12113515.html
Copyright © 2011-2022 走看看