Codeforces Round 1105 (Div. 1)
D
猎捕野兽
Hunting the Beast
Codeforces
2500
原题
← CE →

猎捕野兽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 \in S$ 到达(一个顶点总是可以从它自身到达),则称出发集合 $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$)。从 $3$ 个顶点中的任意一个顶点出发都可以到达所有顶点。这部分对总和的贡献为 $2 \cdot 3 = 6$。
  • $6$ 个图由一个 $2$-环和一片指向该环的叶子组成(例如 $1 \leftrightarrow 2$ 且 $3 \to 1$)。要到达所有顶点,出发集合必须恰好是叶子顶点。这部分对总和的贡献为 $6 \cdot 1 = 6$。
AI (CI翻译)

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$。
AI (CI翻译)

The total sum of values is $6 + 12 = 18$.

价值总和为 $6 + 12 = 18$。AI (CI翻译)

样例

Input #1
5
2 1
3 1
3 2
4 2
8 3
Output #1
2
12
18
216
20415360
InputCopy OutputCopy
题目信息
题号
2239D
时间限制
2 s
内存限制
256 MB
标签
组合数学
动态规划
图论
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。