CF2226F.Inversion Invasion

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There is an array aa of length nn. Initially, ai=0a_i = 0 for all 1≤i≤n1 \le i \le n.

A permutation∗^{\text{∗}} pp is said to be valid if at least one of the following conditions is satisfied for every 1≤i≤n1 \le i \le n:

  • ai=0a_i = 0.
  • gcd⁡(pi,n)=ai\gcd(p_i, n) = a_i.

You have to process qq queries. In each query, you are given two integers ii and xx, and you must update the array by setting ai:=xa_i:= x persistently.

It is guaranteed that ai=0a_i = 0 at the time of each query, and it is guaranteed that xx divides nn.

After performing each query, output the sum of the number of inversions†^{\text{†}} across all valid permutations. As the answers can be very large, report them modulo 998 244 353998\,244\,353.

∗^{\text{∗}}A permutation of length mm is an array consisting of mm distinct integers from 11 to mm in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (m=3m=3 but there is a 44 in the array).

†^{\text{†}}An inversion in a permutation pp is a pair of indices (i,j)(i, j) such that i<ji \lt j and pi>pjp_i \gt p_j. For example, the permutation [4,1,3,2][4, 1, 3, 2] has 44 inversions: (1,2)(1, 2), (1,3)(1, 3), (1,4)(1, 4), and (3,4)(3, 4).

有一个长度为 nn 的数组 aa。初始时,对所有 1≤i≤n1 \le i \le n,均有 ai=0a_i = 0。

一个排列∗^{\text{∗}} pp 被称为合法的,当且仅当对每个 1≤i≤n1 \le i \le n,以下两个条件中至少有一个成立:

  • ai=0a_i = 0;
  • gcd⁡(pi,n)=ai\gcd(p_i, n) = a_i。

你需要处理 qq 个查询。在每个查询中,你将收到两个整数 ii 和 xx,并需持久化地更新数组:令 ai:=xa_i := x。

保证每次查询时均有 ai=0a_i = 0,且保证 xx 是 nn 的约数。

每次执行查询后,请输出所有合法排列的逆序对总数(即:对每个合法排列 pp,计算其逆序对数量,再将所有这些数量求和)。由于答案可能非常大,请对 998 244 353998\,244\,353 取模后输出。

∗^{\text{∗}} 长度为 mm 的排列是指由 11 到 mm 中互不相同的 mm 个整数组成的任意顺序的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 m=3m=3,但数组中出现了 44)。

†^{\text{†}} 排列 pp 中的一个逆序对是指一对下标 (i,j)(i, j),满足 i<ji \lt j 且 pi>pjp_i \gt p_j。例如,排列 [4,1,3,2][4, 1, 3, 2] 包含 44 个逆序对:(1,2)(1, 2)、(1,3)(1, 3)、(1,4)(1, 4) 和 (3,4)(3, 4)。

输入格式

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 first line of each testcase contains two integers nn and qq (1≤n≤2⋅1061 \le n \le 2 \cdot 10^6; 1≤q≤min⁡(n,106)1 \le q \le \min(n, 10^6)) — the length of the array aa and the number of queries.

Each of the next qq lines contains two integers ii and xx (1≤i≤n1 \le i \le n; 1≤x≤n1 \le x \le n).

It is guaranteed that ai=0a_i = 0 at the time of each query, and it is guaranteed that xx divides nn.

It is guaranteed that the sum of nn over all the test cases does not exceed 2⋅1062 \cdot 10^6, and the sum of qq over all the test cases does not exceed 10610^6.

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

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n≤2⋅1061 \le n \le 2 \cdot 10^6;1≤q≤min⁡(n,106)1 \le q \le \min(n, 10^6))—— 分别表示数组 aa 的长度和查询次数。

接下来的 qq 行中,每行包含两个整数 ii 和 xx(1≤i≤n1 \le i \le n;1≤x≤n1 \le x \le n)。

保证在每次查询时均有 ai=0a_i = 0,且保证 xx 整除 nn。

保证所有测试用例的 nn 之和不超过 2⋅1062 \cdot 10^6,且所有测试用例的 qq 之和不超过 10610^6。

输出格式

For each testcase, print qq integers — the total number of inversions across all valid permutations after processing each query.

As the answers can be large, print them modulo 998 244 353998\,244\,353.

对于每个测试用例,输出 qq 个整数——即在处理每个查询后,所有有效排列中逆序对的总数。

由于答案可能很大,请对 998 244 353998\,244\,353 取模后输出。

输入输出样例

  • 输入#1

    3
    3 2
    2 3
    3 3
    9 3
    6 3
    7 1
    3 3
    100 7
    67 4
    41 25
    69 1
    99 1
    50 100
    100 2
    9 10

    输出#1

    3
    0
    1461600
    1114560
    156960
    207622048
    432575995
    443345156
    499213668
    665624940
    770601684
    223944735

说明/提示

For the first testcase, initially, a=[0,0,0]a = [0, 0, 0].

After the first query, a=[0,3,0]a = [0, 3, 0]. The valid permutations are [1,3,2][1, 3, 2] and [2,3,1][2, 3, 1]. The total number of inversions is 1+2=31 + 2 = 3.

After the second query, a=[0,3,3]a = [0, 3, 3]. There are no valid permutations.

对于第一个测试用例,初始时 a=[0,0,0]a = [0, 0, 0]。

第一次查询后,a=[0,3,0]a = [0, 3, 0]。有效的排列为 [1,3,2][1, 3, 2] 和 [2,3,1][2, 3, 1]。逆序对总数为 1+2=31 + 2 = 3。

第二次查询后,a=[0,3,3]a = [0, 3, 3]。不存在有效的排列。

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

首页