D. Santa's Bot

Meaning of the questions: Santa Claus received a number of letters from n different children that year, of course, every child that wants to get some gifts from Santa Claus, in particular, the i-th ki kids want a different gift in as his gift, some gifts may be have more than one child.

Santa Claus is very busy, so he wants the new robot to choose some gifts to the children, unfortunately, the robot algorithm which some Bug, to choose some gifts to the children, the robot performs the following actions:
1. equal probability from the n children choose children x
2. x from the child wants kx gift moderate probability elected y gift
3. equal probability to choose a child to accept this gift z
(x, y, x) is called a robot choices
if there are children present gift y z listed , then the option is valid.
Calculate the probability of selecting effective

Input:
The first line indicates the n-th child
next n lines, i-th row represents a list of gifts for Christmas i-th child wants, ki, ai1, ai2, ... aiki,
a gift in the same list does not appear repeatedly

Output:
Probability print robot valid choices, this probability is expressed as an irreducible fraction \ (\ FRAC {X} {Y} \) , you must print \ (x \ cdot {y} ^ {- 1} mod 998244353 \)

Analysis: The title means is that there are n children, each child has a gift ki is what they want, now randomly pick a child to accept this gift, and this gift is a gift he wants to exist in a single the inquiry the probability
suppose we now pick out a child x, he singled out the probability is 1 / n, choose a gift from his wish list, the probability becomes 1 / n * 1 / k [ x], then pick a child, and in line with the probability of: 1 / the n-* 1 / k [the X-] * number (all in the wish list) / n of the gift , so that you can obtain this probability
output requires us to put this probability into irreducible fraction x / y, then find x / y mod 998244353, this effect can be employed fast power inversing, inverse fast power element is the x / y mod p stuff into this form of x * q mod p, that this division into multiplication, and they Sagami value equal
because, in the computer division multiplication become well turn into a lot of
this stuff is number theory, want to understand this question must be prefixed knowledge: fast and power modulo fast power inversion yuan

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

using LL = long long;
const int mod = 998244353;
const int N = 1e6 + 5;
vector<int> list[N];
int k[N];
//记录礼物数量
int cnt[N];
//快速模
LL qmi(int a, int b, int p)
{
    LL res = 1;
    while (b)
    {
        if (b & 1) res = res * a % p;
        a = a * (LL)a % p;
        b >>= 1;//b右移一位
    }
    return res;
}

//费马小定理
//y的模mod的乘法逆元
int Fermat(int y)
{
    return qmi(y, mod - 2, mod);
}

int main()
{
    //n个小孩
    int n;
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &k[i]);
        list[i].resize(k[i] + 1);
        for (int j = 1; j <= k[i]; ++j)
        {
            scanf("%d", &list[i][j]);
            ++cnt[list[i][j]];
        }
    }

    LL res = 0;
    //计算概率

    //只需求1/k[x] * cnt即可
    for (int i = 1; i <= n; ++i)
    {
        LL cur = 0;
        for (int j = 1; j <= k[i]; ++j)
            cur += cnt[list[i][j]];
        //求k[i]模mod的乘法逆元
        res += ((cur % mod) * Fermat(k[i])) % mod;
    }
    //再乘以1 / n * n
    res = ((res % mod)) * Fermat((1ll * n * n) % mod) % mod;

    printf("%lld\n", res);


    return 0;
}

Guess you like

Origin www.cnblogs.com/pixel-Teee/p/12127496.html