猎杀野兽AI (CI翻译)
In Zhuji, a city in central Zhejiang Province, local folklore tells of a wild beast known as the Modeiyon. Dwelling deep in the mountains, it is said to sneak into villages at night to devour livestock and prey on lone travelers. Though many elders claim to have seen it, no photograph of the creature has ever been taken.
在浙江省中部的诸暨,当地民间传说中有一种名为 Modeiyon 的野兽。传说它栖息在深山之中,夜晚会潜入村庄吞食牲畜,并捕食独自行走的旅人。尽管许多老人声称见过它,但至今从未有人拍到过这种生物的照片。AI (CI翻译)
A brave group of $m$ people decides to head up the mountain to hunt the beast. The mountain's locations and trails can be modeled as a functional graph $G$ with $n$ vertices (numbered $1$ to $n$). A functional graph is a directed graph with $n$ vertices and $n$ edges, where every vertex has an out-degree of exactly $1$. Additionally, it is known that the mountain's trails do not ever form self-loops.
一群勇敢的 $m$ 个人决定上山猎杀这头野兽。山中的地点和道路可以建模为一个有 $n$ 个顶点的函数图 $G$(顶点编号为 $1$ 到 $n$)。函数图是一种有向图,包含 $n$ 个顶点和 $n$ 条边,其中每个顶点的出度恰好为 $1$。此外,已知山中的道路不会形成自环。AI (CI翻译)
The group will choose exactly $m$ distinct vertices to form their initial starting set $S$. A starting set $S$ is defined as successful if every vertex $u$ in the graph is reachable from at least one vertex $v \in S$ (a vertex is always reachable from itself).
这群人将选择恰好 $m$ 个不同的顶点,组成初始出发点集合 $S$。如果图中的每个顶点 $u$ 都可以从 $S$ 中至少一个顶点 $v$ 到达,则称出发点集合 $S$ 是成功的(一个顶点总是可以从它自身到达)。AI (CI翻译)
There are $\binom{n}{m}$ possible ways to choose the starting set of size $m$. They define the value of a graph $G$ as the number of successful starting sets it has.
共有 $\binom{n}{m}$ 种选择大小为 $m$ 的出发点集合的方法。定义图 $G$ 的价值为其中成功的出发点集合数量。AI (CI翻译)
However, the exact layout of the mountain's trails is unknown. If the destination of the single outgoing edge from each vertex is chosen arbitrarily from the remaining $n-1$ vertices (excluding the vertex itself), there are exactly $(n-1)^n$ possible functional graphs. Given $n$ and $m$, your task is to calculate the sum of the values of all $(n-1)^n$ possible functional graphs. Since the answer can be very large, print it modulo $998\,244\,353$.
然而,山中道路的确切布局尚不明确。如果将每个顶点的唯一出边的终点从其余 $n-1$ 个顶点中任意选择(不包括该顶点自身),则恰好有 $(n-1)^n$ 个可能的函数图。给定 $n$ 和 $m$,请计算所有 $(n-1)^n$ 个可能的函数图的价值之和。由于答案可能很大,请将其对 $998\,244\,353$ 取模后输出。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来描述各个测试用例。AI (CI翻译)
The only line of each test case contains two integers $n,m$ ($1\le m \le n\le 10^6$) — the number of vertices in the graph and the number of starting vertices.
每个测试用例仅一行,包含两个整数 $n,m$($1\le m \le n\le 10^6$),分别表示图中的顶点数和出发点数。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
保证所有测试用例的 $n$ 之和不超过 $10^6$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer — the sum of the values of all $(n-1)^n$ possible functional graphs, modulo $998\,244\,353$.
对于每个测试用例,输出一个整数,即所有 $(n-1)^n$ 个可能的函数图的价值之和,对 $998\,244\,353$ 取模。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, there is exactly $(2-1)^2=1$ valid functional graph: $1 \to 2$ and $2 \to 1$. Both starting sets of size $1$ ($\{1\}$ and $\{2\}$) can reach all vertices. Thus, the total value is $2$.
在第一个测试用例中,恰好有 $(2-1)^2=1$ 个合法函数图:$1 \to 2$ 且 $2 \to 1$。大小为 $1$ 的两个出发点集合($\{1\}$ 和 $\{2\}$)都可以到达所有顶点。因此,价值总和为 $2$。AI (CI翻译)
In the second test case, there are $(3-1)^3=8$ valid functional graphs. They can be categorized as follows:
在第二个测试用例中,共有 $(3-1)^3=8$ 个合法函数图。它们可以分类如下:AI (CI翻译)
$2$graphs form a single cycle of length$3$(e.g.,$1 \to 2 \to 3 \to 1$). Starting at any of the$3$vertices can reach all vertices. This contributes$2 \cdot 3 = 6$to the total value.$6$graphs consist of a$2$-cycle and a single leaf pointing to it (e.g.,$1 \leftrightarrow 2$and$3 \to 1$). To reach all vertices, the starting set must be exactly the leaf vertex. This contributes$6 \cdot 1 = 6$to the total value.
$2$个图构成一个长度为$3$的单环(例如,$1 \to 2 \to 3 \to 1$)。从其中任意一个顶点出发都可以到达所有顶点。这部分对总和的贡献为$2 \cdot 3 = 6$。$6$个图由一个$2$-环和一个指向该环的叶子节点组成(例如,$1 \leftrightarrow 2$且$3 \to 1$)。要到达所有顶点,出发点集合必须恰好是叶子节点。这部分对总和的贡献为$6 \cdot 1 = 6$。
The total sum of values is $6 + 6 = 12$.
价值总和为 $6 + 6 = 12$。AI (CI翻译)
In the third test case, the $8$ possible graphs are the same, but we choose subsets of size $m=2$:
在第三个测试用例中,可能的 $8$ 个图与前一个测试用例相同,但我们选择大小为 $m=2$ 的子集:AI (CI翻译)
- For the
$2$cycle graphs, any subset of size$2$is successful. This contributes$2 \cdot \binom{3}{2} = 6$. - For the
$6$leaf graphs, the subset must contain the leaf vertex. There are exactly$\binom{2}{1}=2$such subsets for each graph. This contributes$6 \cdot 2 = 12$.
- 对于那
$2$个环图,任意大小为$2$的子集都成功。这部分的贡献为$2 \cdot \binom{3}{2} = 6$。 - 对于那
$6$个叶子图,子集必须包含叶子节点。每个图中恰好有$\binom{2}{1}=2$个这样的子集。这部分的贡献为$6 \cdot 2 = 12$。
The total sum of values is $6 + 12 = 18$.
价值总和为 $6 + 12 = 18$。AI (CI翻译)
样例
5
2 1
3 1
3 2
4 2
8 3
2
12
18
216
20415360
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。