#arc170e. E - BDFS
E - BDFS
Score: points
问题陈述
给定整数 和 。
存在一个包含 个顶点和 条边的图,其中每个顶点被标记为 到 。第 条边双向连接顶点 和 。在这里,顶点 指的是顶点 。
执行以下算法以获得长度为 的序列 :
-
将长度为 的整数序列 设置为 。同时,将数字对的序列 设置为 。当 不为空时,重复以下过程:
-
让 是 的第一个元素。移除这个元素。
-
如果 ,那么设置 ,并且对于每个与顶点 相邻且 的顶点 ,执行以下过程。如果有多个这样的 满足条件,按照顶点编号的升序处理它们:
- 以概率 ,将 添加到 的 前面。
- 如果 没有被添加到 的前面,就将它添加到 的 后面。
-
找到最终获得的序列 的元素之和的期望值,对 取模。
解决给定的每个 个测试用例。
期望值的定义 可以证明要找到的期望值总是一个有理数。此外,这个问题的约束条件保证如果该值表示为一个不可约分数 ,那么 不能被 整除。在这里,存在一个唯一的整数 在 和 之间,包括 和 ,使得 。提供这个 作为答案。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given integers and .
There is a graph with vertices and edges, where each vertex is labeled to . The -th edge connects vertices and bidirectionally. Here, vertex refers to vertex .
Perform the following algorithm to obtain a sequence of length :
-
Set an integer sequence of length to . Also, set a sequence of number pairs to . Repeat the following process while is not empty:
-
Let be the first element of . Remove this element.
-
If , then set , and for each vertex adjacent to vertex such that , perform the following process. If there are multiple such that satisfy the condition, process them in ascending order of vertex number:
- With probability , add to the front of .
- If was not added to the front of , add it to the end of .
-
Find the expected value of the sum of the elements of the final sequence obtained, modulo .
Solve each of the test cases given.
Definition of expected value It can be proved that the expected value to be found is always a rational number. Furthermore, the constraints of this problem guarantee that if that value is expressed as an irreducible fraction , then is not divisible by . Here, there is a unique integer between and , inclusive, such that . Provide this as the answer.
Constraints
- All input numbers are integers.
Input
The input is given from Standard Input in the following format:
Each case is given in the following format:
Output
Print lines. The -th line should contain the answer for the -th test case.
Sample Input 1
3
3 50
4 1
1000000000000000000 70
Sample Output 1
499122179
595552585
760296751
In the first test case, the algorithm may operate as follows:
- Initially, and . Remove the first element from .
- , so set . The vertices adjacent to vertex such that are and .
- Add to the front of . Add to the end of . Now .
- Remove the first element from .
- , so set . The vertex adjacent to vertex such that is .
- Add to the front of . Now .
- Remove the first element from .
- , so set . There are no vertices adjacent to vertex such that , so do nothing.
- Remove the first element from .
- , so do nothing.
- is now empty, so the process ends.
In this case, the final sequence obtained is . The probability that the algorithm operates as described above is , and the expected sum of the elements of is .