QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100
統計

考虑两个从 $1$ 到 $n$ 的整数排列 $p$ 和 $q$。如果存在一个 $2 \times n$ 的矩阵 $a$,满足以下条件,我们就称一个长度为 $n$ 的二进制字符串 $s$ 是“满足条件”的:

  • 从 $1$ 到 $2n$ 的每个整数在矩阵中恰好出现一次。
  • 第一行的元素按照排列 $p$ 排序。更正式地说,$a_{1,i} < a_{1,j} \iff p_i < p_j$,对于 $1 \le i < j \le n$。
  • 第二行的元素按照排列 $q$ 排序。更正式地说,$a_{2,i} < a_{2,j} \iff q_i < q_j$,对于 $1 \le i < j \le n$。
  • 对于每个 $1 \le i \le n$,都有 $a_{1,i} < a_{2,i} \iff s_i = 0$。

对于两个大小为 $n$ 的排列 $p$ 和 $q$,定义 $f(p, q)$ 为满足条件的字符串 $s$ 的数量。 给定排列 $p$ 的所有元素,以及排列 $q$ 的部分元素(其余元素未知)。求所有符合已知元素的排列 $q$ 的 $f(p, q)$ 之和,结果对 $998\,244\,353$ 取模。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 100$)。 第二行包含 $n$ 个整数 $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$,所有 $p_i$ 互不相同),表示 $1$ 到 $n$ 的一个排列。 第三行包含 $n$ 个整数 $q_1, q_2, \dots, q_n$ ($0 \le q_i \le n$,当 $q_i \neq 0$ 且 $q_j \neq 0$ 时 $q_i \neq q_j$)。如果 $q_i \neq 0$,则给出了该元素的值;如果 $q_i = 0$,则该元素未知。所有给定的元素互不相同。

输出格式

输出所有合法的排列 $q$ 的 $f(p, q)$ 之和,结果对 $998\,244\,353$ 取模。

样例

样例输入 1

2
1 2
2 1

样例输出 1

3

样例输入 2

4
4 3 2 1
4 3 2 1

样例输出 2

16

样例输入 3

5
1 2 3 4 5
0 0 0 0 0

样例输出 3

1546

样例输入 4

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

样例输出 4

52

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#500EditorialOpen题解ezoilearner2026-04-19 21:08:30View
#841EditorialOpen题解alpha10222026-01-28 02:16:27View
#641EditorialOpen对其他题解的结论的补充证明liumonong2026-01-04 21:07:26View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.