AT_arc219_b.Reverse Permutation

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given an integer NN and a permutation P=(P1,P2,…,PN)P=(P_1,P_2,\ldots,P_N) of (1,2,…,N)(1,2,\ldots,N).

For a permutation Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,…,N)(1,2,\ldots,N), let Q′=(Q1′,Q2′,…,QN′)Q'=(Q_1',Q_2',\ldots,Q_N') be the lexicographically smallest permutation obtainable by performing the following operation exactly once:

  • Choose a pair of integers (l,r)(l,r) satisfying 1≤l≤r≤N1\le l\le r\le N, and reverse Ql,Ql+1,…,QrQ_l,Q_{l+1},\ldots,Q_r. More precisely, replace QQ with (Q1,Q2,…,Ql−1,Qr,Qr−1,…,Ql,Qr+1,Qr+2,…,QN)(Q_1,Q_2,\ldots,Q_{l-1},Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N).

Find the number, modulo 998244353998244353, of permutations QQ of (1,2,…,N)(1,2,\ldots,N) such that Q′=PQ'=P.

You are given TT test cases; solve each of them.

给你一个整数 NN 和 (1,2,…,N)(1,2,\ldots,N) 的一个排列 P=(P1,P2,…,PN)P=(P_1,P_2,\ldots,P_N)。

对于 (1,2,…,N)(1,2,\ldots,N) 的任意一个排列 Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\ldots,Q_N),定义 Q′=(Q1′,Q2′,…,QN′)Q'=(Q_1',Q_2',\ldots,Q_N') 为对 QQ 恰好执行一次如下操作后所能得到的字典序最小的排列:

  • 选择一对满足 1≤l≤r≤N1\le l\le r\le N 的整数 (l,r)(l,r),并将子段 Ql,Ql+1,…,QrQ_l,Q_{l+1},\ldots,Q_r 翻转。更准确地说,将 QQ 替换为
    (Q1,Q2,…,Ql−1,Qr,Qr−1,…,Ql,Qr+1,Qr+2,…,QN)(Q_1,Q_2,\ldots,Q_{l-1},Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N)。

求满足 Q′=PQ'=P 的排列 QQ(即 (1,2,…,N)(1,2,\ldots,N) 的排列)的个数,结果对 998244353998244353 取模。

你将收到 TT 组测试数据;请分别求解每组。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each test case is given in the following format:

NN
P1P_1 P2P_2 …\ldots PNP_N

输入从标准输入中按以下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例按以下格式给出:

NN
P1P_1 P2P_2 …\ldots PNP_N

输出格式

Output the answers for the test cases in order, separated by newlines.

按顺序输出测试用例的答案,答案之间用换行符分隔。

输入输出样例

  • 输入#1

    4
    3
    1 3 2
    1
    1
    4
    4 3 2 1
    6
    1 2 6 4 5 3

    输出#1

    2
    1
    0
    9

说明/提示

Sample 1 Explanation:
Consider the first test case.

For example, when Q=(2,3,1)Q=(2,3,1), choosing (l,r)=(1,3)(l,r)=(1,3) gives (1,3,2)(1,3,2). No permutation lexicographically smaller than (1,3,2)(1,3,2) can be obtained, so we have Q′=(1,3,2)Q'=(1,3,2). Thus, Q=(2,3,1)Q=(2,3,1) satisfies Q′=PQ'=P.

There are two permutations QQ such that Q′=PQ'=P: Q=(2,3,1),(3,1,2)Q=(2,3,1),(3,1,2).

Constraints

  • 1≤T1\le T
  • 1≤N≤5×1051\le N\le 5\times 10^5
  • PP is a permutation of (1,2,…,N)(1,2,\ldots,N).
  • The sum of NN over all test cases is at most 5×1055\times 10^5.
  • All input values are integers.

样例 1 解释:
考虑第一个测试用例。

例如,当 Q=(2,3,1)Q=(2,3,1) 时,选择 (l,r)=(1,3)(l,r)=(1,3) 可得到 (1,3,2)(1,3,2)。无法通过任何操作得到字典序比 (1,3,2)(1,3,2) 更小的排列,因此有 Q′=(1,3,2)Q'=(1,3,2)。于是,Q=(2,3,1)Q=(2,3,1) 满足 Q′=PQ'=P。

满足 Q′=PQ'=P 的排列 QQ 共有两个:Q=(2,3,1), (3,1,2)Q=(2,3,1),\ (3,1,2)。

约束条件

  • 1≤T1\le T
  • 1≤N≤5×1051\le N\le 5\times 10^5
  • PP 是 (1,2,…,N)(1,2,\ldots,N) 的一个排列。
  • 所有测试用例的 NN 之和不超过 5×1055\times 10^5。
  • 所有输入值均为整数。

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

首页