201322: [AtCoder]ARC132 C - Almost Sorted

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

Given is a sequence $a_1,\dots,a_n$ consisting of $1,\dots, n$ and $-1$, along with an integer $d$. How many sequences $p_1,\dots,p_n$ satisfy the conditions below? Print the count modulo $998244353$.

  • $p_1,\dots,p_n$ is a permutation of $1,\dots, n$.
  • For each $i=1,\dots,n$, $a_i=p_i$ if $a_i\neq -1$. (That is, $p_1,\dots,p_n$ can be obtained by replacing the $-1$s in $a_1,\dots,a_n$ in some way.)
  • For each $i=1,\dots,n$, $|p_i - i|\le d$.

Constraints

  • $1 \le d \le 5$
  • $d < n \le 500$
  • $1\le a_i \le n$ or $a_i=-1$.
  • $|a_i-i|\le d$ if $a_i\neq -1$.
  • $a_i\neq a_j$, if $i\neq j$ and $a_i, a_j \neq -1$.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$n$ $d$
$a_1$ $\dots$ $a_n$

Output

Print the number of sequences that satisfy the conditions, modulo $998244353$.


Sample Input 1

4 2
3 -1 1 -1

Sample Output 1

2

The conditions are satisfied by $(3,2,1,4)$ and $(3,4,1,2)$.


Sample Input 2

5 1
2 3 4 5 -1

Sample Output 2

0

The only permutation of $1,2,3,4,5$ that can be obtained by replacing the $-1$ is $(2,3,4,5,1)$, whose fifth term violates the condition, so the answer is $0$.


Sample Input 3

16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Sample Output 3

794673086

Print the count modulo $998244353$.

Input

题意翻译

给定一个长度为 $n$ 的数字序列 $A$,由 $1$ 到 $n$ 之间的整数和 $-1$ 组成。还有一个整数 $d$。 现在要对这个序列进行变换,将 $A$ 中所有为 $-1$ 的 $a_i$ 替换成一个数字,使得得到的序列 $P$,满足: - $\forall a_i \ne -1,p_i = a_i$。 - $P$ 是 $1$ 到 $n$ 的一个排列。 - $\forall |p_i-i| \leq d$ 试问有多少种这样的排列 $P$。答案对 $998244353$ 取膜。

加入题单

上一题 下一题 算法标签: