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?
InputThe first line contains one integer nn (1≤n≤1061≤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 (1≤ki,ai,j≤1061≤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=1nki≤106∑i=1nki≤106.
OutputPrint 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 x⋅y−1mod998244353x⋅y−1mod998244353, where y−1y−1 is the inverse element of yy modulo 998244353998244353 (such integer that y⋅y−1y⋅y−1 has remainder 11 modulo 998244353998244353).
Examples input Copy2 2 2 1 1 1output Copy
124780545input 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 2output Copy
798595483
选一对小朋友,和一个礼物,判断都喜欢的概率
标签:int,Bot,Santa,mod,include,kid 来源: https://www.cnblogs.com/dealer/p/12831866.html