AT_abc457_f.Second Gap

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given an integer NN and an integer sequence D=(D1,D2,…,DN−1)D = (D_1, D_2, \ldots, D_{N-1}) of length N−1N - 1.

Find the number, modulo 998244353998244353, of permutations P=(P1,P2,…,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,…,N)(1, 2, \ldots, N) that satisfy the following condition.

  • For each 1≤i≤N−11 \le i \le N - 1, let PaP_a and PbP_b be the elements with the largest and the second largest values, respectively, among (Pi,Pi+1,…,PN)(P_i, P_{i+1}, \ldots, P_N); then ∣a−b∣=Di|a - b| = D_i.

给定一个整数 NN 和一个长度为 N−1N - 1 的整数序列 D=(D1,D2,…,DN−1)D = (D_1, D_2, \ldots, D_{N-1})。

求满足以下条件的排列 P=(P1,P2,…,PN)P = (P_1, P_2, \ldots, P_N)(即 (1,2,…,N)(1, 2, \ldots, N) 的一个排列)的个数,答案对 998244353998244353 取模。

  • 对每个 1≤i≤N−11 \le i \le N - 1,设 PaP_a 和 PbP_b 分别为子序列 (Pi,Pi+1,…,PN)(P_i, P_{i+1}, \ldots, P_N) 中最大值和次大值所对应的元素;则需满足 ∣a−b∣=Di|a - b| = D_i。

输入格式

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

NN
D1D_1 D2D_2 …\ldots DN−1D_{N-1}

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

NN
D1D_1 D2D_2 …\ldots DN−1D_{N-1}

输出格式

Output the number, modulo 998244353998244353, of permutations satisfying the condition.

输出满足该条件的排列数量,对 998244353998244353 取模。

输入输出样例

  • 输入#1

    3
    1 1

    输出#1

    4
  • 输入#2

    5
    1 2 2 1

    输出#2

    0
  • 输入#3

    15
    4 4 4 4 4 4 3 2 2 2 2 2 1 1

    输出#3

    70270200

说明/提示

Sample 1 Explanation:
For example, we can verify that (2,3,1)(2, 3, 1) satisfies the condition as follows.

  • We have (P1,P2,P3)=(2,3,1)(P_1, P_2, P_3) = (2, 3, 1). The largest value is P2P_2 and the second largest value is P1P_1, and ∣2−1∣=1=D1|2 - 1| = 1 = D_1.

  • We have (P2,P3)=(3,1)(P_2, P_3) = (3, 1). The largest value is P2P_2 and the second largest value is P3P_3, and ∣2−3∣=1=D2|2 - 3| = 1 = D_2.

Four permutations satisfy the condition: (1,2,3),(1,3,2),(2,3,1),(3,2,1)(1, 2, 3), (1, 3, 2), (2, 3, 1), (3, 2, 1). Thus, output 44.

Constraints

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 1≤Di≤N−i1 \le D_i \le N - i
  • All input values are integers.

样例 1 解释:
例如,我们可以验证排列 (2,3,1)(2, 3, 1) 满足条件,如下所示。

  • 此时 (P1,P2,P3)=(2,3,1)(P_1, P_2, P_3) = (2, 3, 1)。其中最大值为 P2P_2,次大值为 P1P_1,且 ∣2−1∣=1=D1|2 - 1| = 1 = D_1。
  • 此时 (P2,P3)=(3,1)(P_2, P_3) = (3, 1)。其中最大值为 P2P_2,次大值为 P3P_3,且 ∣2−3∣=1=D2|2 - 3| = 1 = D_2。

共有四个排列满足条件:(1,2,3), (1,3,2), (2,3,1), (3,2,1)(1, 2, 3),\ (1, 3, 2),\ (2, 3, 1),\ (3, 2, 1)。因此输出 44。

约束条件

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 1≤Di≤N−i1 \le D_i \le N - i
  • 所有输入值均为整数。

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

首页