CF2239D.Hunting the Beast

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

A brave group of mm people decides to head up the mountain to hunt the beast. The mountain's locations and trails can be modeled as a functional graph GG with nn vertices (numbered 11 to nn). A functional graph is a directed graph with nn vertices and nn edges, where every vertex has an out-degree of exactly 11. Additionally, it is known that the mountain's trails do not ever form self-loops.

The group will choose exactly mm distinct vertices to form their initial starting set SS. A starting set SS is defined as successful if every vertex uu in the graph is reachable from at least one vertex v∈Sv \in S (a vertex is always reachable from itself).

There are (nm)\binom{n}{m} possible ways to choose the starting set of size mm. They define the value of a graph GG as the number of successful starting sets it has.

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−1n-1 vertices (excluding the vertex itself), there are exactly (n−1)n(n-1)^n possible functional graphs. Given nn and mm, your task is to calculate the sum of the values of all (n−1)n(n-1)^n possible functional graphs. Since the answer can be very large, print it modulo 998 244 353998\,244\,353.

在浙江省中部的诸暨市,当地民间传说中有一种名为“魔德勇”的野兽。它栖息于深山之中,据说会在夜间潜入村庄,吞食牲畜,并袭击独行的旅人。尽管许多老人声称曾亲眼见过它,却从未有人成功拍摄到它的照片。

一支由 mm 人组成的勇敢队伍决定进山围猎这头野兽。整座山的地貌与路径可建模为一个具有 nn 个顶点(编号为 11 至 nn)的函数图(functional graph)GG。所谓函数图,是指一个含 nn 个顶点和 nn 条有向边的有向图,其中每个顶点的出度恰好为 11。此外,已知山中的路径绝不会形成自环(即不存在从某顶点指向其自身的边)。

这支队伍将从中精确选择 mm 个互不相同的顶点,构成其初始出发集合 SS。若图 GG 中任意顶点 uu 均可从某个 v∈Sv \in S 出发沿有向边路径到达(规定每个顶点自身总是可达的),则称该出发集合 SS 是成功的。

共有 (nm)\binom{n}{m} 种方式选择大小为 mm 的出发集合。他们将图 GG 的价值定义为其中成功的出发集合的数量。

然而,山中路径的确切布局尚属未知。若对每个顶点,其唯一一条出边的终点在其余 n−1n-1 个顶点中(即排除自身)任意选取,则总共存在 (n−1)n(n-1)^n 种可能的函数图。给定 nn 和 mm,你的任务是计算所有这 (n−1)n(n-1)^n 个可能的函数图的价值之和。由于答案可能极大,请输出其对 998 244 353998\,244\,353 取模的结果。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The only line of each test case contains two integers n,mn,m (1≤m≤n≤1061\le m \le n\le 10^6) — the number of vertices in the graph and the number of starting vertices.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例仅有一行,包含两个整数 n,mn,m(1≤m≤n≤1061\le m \le n\le 10^6)—— 分别表示图中的顶点数和起始顶点数。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output a single integer — the sum of the values of all (n−1)n(n-1)^n possible functional graphs, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——所有 (n−1)n(n-1)^n 个可能的函数图的值之和,对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    5
    2 1
    3 1
    3 2
    4 2
    8 3

    输出#1

    2
    12
    18
    216
    20415360

说明/提示

In the first test case, there is exactly (2−1)2=1(2-1)^2=1 valid functional graph: 1→21 \to 2 and 2→12 \to 1. Both starting sets of size 11 (1{1} and 2{2}) can reach all vertices. Thus, the total value is 22.

In the second test case, there are (3−1)3=8(3-1)^3=8 valid functional graphs. They can be categorized as follows:

  • 22 graphs form a single cycle of length 33 (e.g., 1→2→3→11 \to 2 \to 3 \to 1). Starting at any of the 33 vertices can reach all vertices. This contributes 2⋅3=62 \cdot 3 = 6 to the total value.
  • 66 graphs consist of a 22-cycle and a single leaf pointing to it (e.g., 1↔21 \leftrightarrow 2 and 3→13 \to 1). To reach all vertices, the starting set must be exactly the leaf vertex. This contributes 6⋅1=66 \cdot 1 = 6 to the total value.

The total sum of values is 6+6=126 + 6 = 12.

In the third test case, the 88 possible graphs are the same, but we choose subsets of size m=2m=2:

  • For the 22 cycle graphs, any subset of size 22 is successful. This contributes 2⋅(32)=62 \cdot \binom{3}{2} = 6.
  • For the 66 leaf graphs, the subset must contain the leaf vertex. There are exactly (21)=2\binom{2}{1}=2 such subsets for each graph. This contributes 6⋅2=126 \cdot 2 = 12.

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

在第一个测试用例中,恰好存在 (2−1)2=1(2-1)^2=1 个有效的函数图:1→21 \to 2 且 2→12 \to 1。所有大小为 11 的起始集合(即 {1}\{1\} 和 {2}\{2\})均能到达全部顶点。因此,总值为 22。

在第二个测试用例中,共有 (3−1)3=8(3-1)^3=8 个有效的函数图。它们可分类如下:

  • 22 个图构成一个长度为 33 的单环(例如 1→2→3→11 \to 2 \to 3 \to 1)。从任意一个 33 个顶点出发均能到达全部顶点。这部分对总值的贡献为 2⋅3=62 \cdot 3 = 6。
  • 66 个图由一个 22-环及一个指向该环的孤立叶节点组成(例如 1↔21 \leftrightarrow 2 且 3→13 \to 1)。为到达全部顶点,起始集合必须恰好为该叶节点。这部分对总值的贡献为 6⋅1=66 \cdot 1 = 6。

总值之和为 6+6=126 + 6 = 12。

在第三个测试用例中,可能的 88 个图与前述相同,但此时我们选择大小为 m=2m=2 的子集:

  • 对于 22 个环图,任意大小为 22 的子集均成功。这部分贡献为 2⋅(32)=62 \cdot \binom{3}{2} = 6。
  • 对于 66 个含叶节点的图,子集必须包含该叶节点。对每个图,恰有 (21)=2\binom{2}{1}=2 个满足条件的子集。这部分贡献为 6⋅2=126 \cdot 2 = 12。

总值之和为 6+12=186 + 12 = 18。

输入解题思路,AI测评打分。不知道怎么写?

首页