zoukankan      html  css  js  c++  java
  • D. Santa's Bot

    Santa Claus has received letters from nn different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the ii-th kid asked Santa to give them one of kiki 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 xx equiprobably among all nn kids;
    • choose some item yy equiprobably among all kxkx items kid xx wants;
    • choose a kid zz who will receive the present equipropably among all nn kids (this choice is independent of choosing xx and yy); the resulting triple (x,y,z)(x,y,z) is called the decision of the Bot.

    If kid zz listed item yy 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?

    Input

    The first line contains one integer nn (1n1061≤n≤106) — the number of kids who wrote their letters to Santa.

    Then nn lines follow, the ii-th of them contains a list of items wanted by the ii-th kid in the following format: kiki ai,1ai,1 ai,2ai,2 ... ai,kiai,ki (1ki,ai,j1061≤ki,ai,j≤106), where kiki is the number of items wanted by the ii-th kid, and ai,jai,j are the items themselves. No item is contained in the same list more than once.

    It is guaranteed that i=1nki106∑i=1nki≤106.

    Output

    Print the probatility that the Bot produces a valid decision as follows:

    Let this probability be represented as an irreducible fraction xyxy. You have to print xy1mod998244353x⋅y−1mod998244353, where y1y−1 is the inverse element of yy modulo 998244353998244353 (such integer that yy1y⋅y−1 has remainder 11 modulo 998244353998244353).

    Examples
    input
    Copy
    2
    2 2 1
    1 1
    
    output
    Copy
    124780545
    
    input
    Copy
    5
    2 1 2
    2 3 1
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 998244353;
    const int N = 1e6+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    inline int qpow(int x, ll n) {
        int r = 1;
        while (n > 0) {
            if (n & 1) 
                r = 1ll * r * x % mod;
            n >>= 1;
            x = 1ll * x * x % mod;
        }
        return r;
    }
    inline int Inv(int x) {
        return qpow(x, mod - 2);
    }
    vector<int> a[N],cnt(N,0);
    int main()
    {
        int n;
        cin >> n;
    
        for (int i = 0; i < n; i++)
        {
            int t;
            cin >> t;
            a[i].resize(t);
            for (int j = 0; j < t; j++)
            {
                int tmp;
                cin >> tmp;
                cnt[tmp]++;
                a[i][j] = tmp;
            }
        }
        
        //for (int i = 2; i < N; i++)
        //    inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod;
        ll res = 0;
        ll down = Inv(n)%mod;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < a[i].size(); j++)
            {
                res += (((cnt[a[i][j]] % mod)*down%mod)*down%mod)*Inv(a[i].size())%mod;
                res %= mod;
            }
        }
        cout << res << endl;
        return 0;
    }
    3 2 4 3
    2 1 4
    3 4 3 2
    
    output
    Copy
    798595483
    

     选一对小朋友,和一个礼物,判断都喜欢的概率

  • 相关阅读:
    VisualStudio 2012中的单元测试
    基于.Net 写我自己的Ajax后台框架AjaxFramework
    MongoDB的真正性能实战百万用户
    基于MMSeg算法的中文分词类库
    Visual Studio 2012的新插件Code Digger (类似Pex)
    Visual Studio 2010 单元测试之一普通单元测试
    Django系列教程:一、Django的安装和入门
    Cocos2dx 入门调研总结
    教程:建立自己的私有云
    如何让你的作业在Hadoop集群中真正实现分布式运行
  • 原文地址:https://www.cnblogs.com/dealer/p/12831866.html
Copyright © 2011-2022 走看看