CF2146F.Bubble Sort
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have just learned the algorithm bubble sort, which is able to sort an array in non-descending order. Let's define the function sort(a) as in the following pseudocode:
function sort(array a): rounds := 0 n := length of a while a is not non-decreasing: rounds := rounds + 1 for i from 1 to n - 1: if a[i] > a[i + 1]: swap(a[i], a[i + 1]) return rounds
As it is shown, the return value of sort(a) represents the number of rounds needed to make array a sorted in non-descending order by using bubble sort.
You are given an integer n, as well as m integer tuples (ki,li,ri) (1≤i≤m). Count the number of permutations∗ p of length n, modulo 998244353, so that the following restrictions are satisfied:
- For each 1≤i≤n, let bi=sort([p1,p2,…,pi]), then
- For each 1≤j≤m, let x be the number of indices y (1≤y≤n) such that by≤kj, then lj≤x≤rj holds.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
你刚刚学习了冒泡排序算法,该算法可以将一个数组按非降序排列。我们定义函数 sort(a) 如下伪代码所示:
function sort(array a): rounds := 0 n := length of a while a is not non-decreasing: rounds := rounds + 1 for i from 1 to n - 1: if a[i] > a[i + 1]: swap(a[i], a[i + 1]) return rounds
如上所示,sort(a) 的返回值表示使用冒泡排序将数组 a 变为非降序所需进行的轮数(round)。
给定一个整数 n,以及 m 个整数三元组 (ki,li,ri)(其中 1≤i≤m)。请计算满足以下约束条件的长度为 n 的排列∗ p 的个数,结果对 998244353 取模:
- 对每个 1≤i≤n,令 bi=sort([p1,p2,…,pi]);
- 对每个 1≤j≤m,令 x 表示满足 by≤kj 的下标 y(其中 1≤y≤n)的个数,则需满足 lj≤x≤rj。
∗ 长度为 n 的排列是指由 1 到 n 这 n 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 不是排列(数字 2 在数组中出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (2≤n≤106, 0≤m≤1000).
Then m lines follow, the i-th line containing three integers ki, li, and ri (0≤ki≤n−1, 1≤li≤ri≤n) — the restrictions.
It is guaranteed that the sum of m2 over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n≤106,0≤m≤1000)。
接下来是 m 行,其中第 i 行包含三个整数 ki、li 和 ri(0≤ki≤n−1,1≤li≤ri≤n)—— 表示限制条件。
保证所有测试用例中 m2 的总和不超过 106。
输出格式
For each test case, output a single integer — the number of possible permutations, modulo 998244353.
对于每个测试用例,输出一个整数——可能的排列数对 998244353 取模的结果。
输入输出样例
输入#1
6 4 3 0 1 1 1 3 3 2 4 4 3 2 0 3 3 1 2 3 4 3 1 2 2 2 3 4 0 1 2 5 3 1 1 4 3 5 5 4 5 5 10 5 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 1000000 0
输出#1
2 1 8 80 192600 373341033
说明/提示
In the first test case, only permutations [3,1,4,2] and [4,1,3,2] satisfy the restrictions. Take [3,1,4,2] as an example. Let's calculate the array b:
- sort([3])=0;
- sort([3,1])=1;
- sort([3,1,4])=1;
- sort([3,1,4,2])=2.
Thus, b=[0,1,1,2]. It is easy to show that it satisfies all the restrictions.
In the second test case, only permutation [1,2,3] satisfies the restrictions.
In the third test case, the following 8 permutations satisfy the restrictions:
- [2,3,1,4];
- [2,4,1,3];
- [3,2,1,4];
- [3,4,1,2];
- [3,4,2,1];
- [4,2,1,3];
- [4,3,1,2];
- [4,3,2,1].
在第一个测试用例中,仅有排列 [3,1,4,2] 和 [4,1,3,2] 满足限制条件。以 [3,1,4,2] 为例,我们来计算数组 b:
- sort([3])=0;
- sort([3,1])=1;
- sort([3,1,4])=1;
- sort([3,1,4,2])=2。
因此,b=[0,1,1,2]。容易验证其满足所有限制条件。
在第二个测试用例中,仅有排列 [1,2,3] 满足限制条件。
在第三个测试用例中,以下 8 个排列满足限制条件:
- [2,3,1,4];
- [2,4,1,3];
- [3,2,1,4];
- [3,4,1,2];
- [3,4,2,1];
- [4,2,1,3];
- [4,3,1,2];
- [4,3,2,1]。
输入解题思路,AI测评打分。不知道怎么写?