QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 1024 MB Total points: 10

#10251. 平滑排列 [A]

Statistics

我們稱數列 $p_1, p_2, \dots, p_k$ 為: 遞增的,若 $p_1 < p_2 < \dots < p_k$; 遞減的,若 $p_1 > p_2 > \dots > p_k$; * 凸的,若存在某個 $1 \le l \le k$,使得數列 $p_1, p_2, \dots, p_l$ 為遞增,且數列 $p_l, p_{l+1}, \dots, p_k$ 為遞減。

特別地,我們將單元素數列視為同時是遞增、遞減且凸的。

若一個排列同時滿足以下三個條件,我們稱其為 $(a, b, c)$-平滑的: 其最長遞增子序列長度為 $a$; 其最長遞減子序列長度為 $b$; * 其最長凸子序列長度為 $c$。

例如,排列 $4, 5, 2, 3, 1$ 是 $(2, 3, 4)$-平滑的,因為: 其最長遞增子序列例如為 $4, 5$; 其最長遞減子序列例如為 $4, 2, 1$; * 其最長凸子序列例如為 $4, 5, 3, 1$。

給定三個整數 $a, b, c$ 滿足 $1 \le a \le b \le c < a + b$,以及一個質數 $p$。可以證明,對於這樣的 $(a, b, c)$ 三元組,所有 $(a, b, c)$-平滑排列的集合是非空且有限的。

請撰寫一個程式,計算: 最長的 $(a, b, c)$-平滑排列之長度(記為 $n$); 長度為 $n$ 的 $(a, b, c)$-平滑排列數量除以 $p$ 的餘數。

輸入格式

輸入僅一行,包含四個整數 $a, b, c, p$ ($1 \le a \le 20, a \le b \le 50\,000, b \le c < a + b, 10^7 \le p \le 10^9$),分別代表所考慮排列中遞增、遞減、凸子序列的最大長度,以及質數 $p$。

輸出格式

輸出僅一行,包含兩個整數:最長的 $(a, b, c)$-平滑排列之長度,以及該長度下 $(a, b, c)$-平滑排列的數量對 $p$ 取模的結果。

範例

範例 1

輸入:

2 2 3 10000019

輸出:

4 4

範例 2

輸入:

2 3 3 999999937

輸出:

5 10

範例 3

輸入:

8 9 11 15872567

輸出:

57 57

說明

範例說明:所有 $(2, 2, 3)$-平滑排列的集合如下: $1, 3, 2 \quad 2, 3, 1 \quad 2, 1, 4, 3 \quad 2, 4, 1, 3 \quad 3, 1, 4, 2 \quad 3, 4, 1, 2$ 其中最長的 4 個排列長度為 4。

在第二個範例測試中,我們考慮長度為 5 的 $(2, 3, 3)$-平滑排列: $3, 2, 1, 5, 4 \quad 3, 2, 5, 1, 4 \quad 4, 2, 1, 5, 3 \quad 4, 2, 5, 1, 3 \quad 4, 3, 1, 5, 2$ $4, 3, 5, 1, 2 \quad 5, 2, 1, 4, 3 \quad 5, 2, 4, 1, 3 \quad 5, 3, 1, 4, 2 \quad 5, 3, 4, 1, 2$

子任務

在部分分數的測試中,滿足條件 $c = a + b - 1$。

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.