P29957 [洛谷] [JOISC 2023 Day1] Festivals in JOI Kingdom 2
省选
题目描述

Problem Description

In JOI Kingdom, a national festival is held once a year. During the period of the festival, there are $N$ events in total. The schedule for each event is already fixed. The schedule of the $N$ events are described by sequences $a, b$ of length $N$ satisfying the following conditions:

  • Every integer between $1$ and $2N$, inclusive, appears as an element of $a$ or $b$.
  • $a_i < b_i \ (1 \le i \le N)$.
  • $a_i < a_{i + 1} \ (1 \le i \le N - 1)$.

The $i$-th event will start at $a_i$ minutes after the beginning of the festival, and end at $b_i$ minutes after the beginning of the festival.

Participants of the festival may choose any events in which they will participate. However, it is not allowed to participate in two events whose schedules overlap. (Note that the starting times and the ending times of the events are different from each other.)

JOI-kun wants to participate in as many events as possible. Until last year, he chose the events in which he participated by the following procedures on a computer:

For $i = 1, 2, \dots, N$, the following are done in this order:

If the schedule of the $i$-th event does not overlap the schedules of the other events in which he already chose to participate, he will participate in the $i$-th event. Otherwise, he will not participate in the $i$-th event.

However, after studying computer science, JOI-kun noticed that the above algorithm does not necessarily maximize the number of events in which JOI-kun will participate. From this year, JOI-kun will use an improved algorithm. Using the improved algorithm, JOI-kun will be able to maximize the number of events in which JOI-kun will participate.

JOI-kun wants to know the number of cases where the improved algorithm produces a larger number of events.

Write a program which, given the integer $N$ and a large prime number $P$, calculates the number of pairs of sequences $a, b$ describing the schedules of the $N$ events for which the improved algorithm produces a larger number of events. Since the answer can be very large, your program should output the remainder of the answer when divided by $P$.

Input Format

Read the following data from the standard input:

$N \ P$

Output Format

Write one line to the standard output. The output should contain the remainder of the answer, the number of pairs of sequences $a, b$ describing the schedules of the $N$ events for which the improved algorithm produces a larger number of events, when divided by $P$.

Sample Input and Output

Sample Input #1

3 100000007

Sample Output #1

2

Sample Input #2

4 100000007

Sample Output #2

28

Sample Input #3

15 999999937

Sample Output #3

935834920

Notes/Hints

Sample Explanation #1

For example, consider the case where $a = (1, 2, 4)$ and $b = (6, 3, 5)$. If JOI-kun uses the algorithm used until last year, he will participate in the first event only. On the other hand, if he uses a correct algorithm to maximize the number of events, he will participate in the second event and the third event. Thus, he will participate in two events. In this case, the improved algorithm produces a larger number of events.

The following are the pair of sequences $a, b$ for which the improved algorithm produces a larger number of events:

  • $a = (1, 2, 4), b = (6, 3, 5)$
  • $a = (1, 2, 4), b = (5, 3, 6)$

Therefore, output $2$, which is the remainder of $2$ when divided by $100000007$.

This sample satisfies the constraints of all subtasks.

Sample Explanation #2

There are $28$ pairs of sequences $a, b$ satisfying the condition. Therefore, output $28$, which is the remainder of $28$ when divided by $100000007$.

This sample satisfies the constraints of all subtasks.

Sample Explanation #3

There are $5295044602247148$ pairs of sequences $a, b$ satisfying the condition. Therefore, output $935834920$, which is the remainder of $935834920$ when divided by $999999937$.

This sample satisfies the constraints of subtasks $3 \sim 6$.

Data Range

For all test data, $1 \le N \le 2 \times 10^4$, $10^8 < P < 10^9$, and $P$ is a prime number. All inputs are integers.

Subtask Number Score $N \le$
1 5 5
2 5 8
3 27 30
4 14 300
5 36 3000
6 13 $2 \times 10^4$
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 100000007
样例输出
2