#abc253h. Ex - We Love Forest
Ex - We Love Forest
Score : points
问题描述
我们有一个图 ,包含 个顶点,编号为 到 ,且当前有 条边。你得到两个长度为 的序列: 和 。
你将执行以下操作 次:
- 随机均匀选择一个索引 ()。在 中添加一条连接顶点 和 的无向边。
注意,即使 已经存在一条或多条连接顶点 和 的边,上述操作也会添加一条新的边。换句话说,最终得到的 可能包含多条边。
对于每个 ,求出在第 次操作后, 是森林的概率对 取模的结果。
什么是森林?
没有环的无向图被称为森林。森林不一定连通。
概率对 取模的定义
我们可以证明所求概率始终是一个有理数。此外,在本题的约束条件下,可以保证当所求概率表示为不可约分数 时, 不会被 整除。
因此,我们可以唯一确定一个整数 ,满足 (包含边界),使得 。请输出这个 值。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have a graph with vertices numbered through and edges. You are given sequences of length .
You will perform the following operation times.
- Choose uniformly at random. Add to an undirected edge connecting Vertices and .
Note that the operation above will add a new edge connecting Vertices and even if already has one or more edges between them. In other words, the resulting may contain multiple edges.
For each , find the probability, modulo , that is a forest after the -th operation.
What is a forest?
An undirected graph without a cycle is called a forest. A forest is not necessarily connected.
Definition of probability modulo
We can prove that the sought probability is always a rational number. Moreover, under the Constraints of this problem, it is guaranteed that, when the sought probability is represented by an irreducible fraction , is indivisible by .
Then, we can uniquely determine an integer between and (inclusive) such that . Print this .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the probability, modulo , that is a forest after the -th operation.
Sample Input 1
3 2
1 2
2 3
Sample Output 1
1
499122177
Let us denote by the edge connecting Vertices and .
After the -st operation, will have:
- Edge with a probability of ;
- Edge with a probability of .
In both cases, is a forest, so the answer for is .
After the -nd operation, will have:
- Edges and with a probability of ;
- Edges and with a probability of ;
- Edges and with a probability of .
is a forest only when has Edges and . Therefore, the sought probability is ; when represented modulo , it is , which should be printed.
Sample Input 2
4 5
1 2
1 2
1 4
2 3
2 4
Sample Output 2
1
758665709
918384805
update @ 2024/3/10 10:48:57