#P9330. [JOISC 2023 Day1] Festivals in JOI Kingdom 2

[JOISC 2023 Day1] Festivals in JOI Kingdom 2

题目描述

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

  • Every integer between 11 and 2N2N, inclusive, appears as an element of aa or bb.
  • ai<bi (1iN)a_i < b_i \ (1 \le i \le N).
  • ai<ai+1 (1iN1)a_i < a_{i + 1} \ (1 \le i \le N - 1).

The ii-th event will start at aia_i minutes after the beginning of the festival, and end at bib_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,,Ni = 1, 2, \dots, N,the following are done in this order.

If the schedule of the ii-th event does not overlap the schedules of the other events in which he already chose to participate, he will participate in the ii-th event. Otherwise, he will not participate in the ii-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 NN and a large prime number PP, calculates the number of pairs of sequences a,ba, b describing the schedules of the NN 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 PP.

输入格式

Read the following data from the standard input.

N PN \ P

输出格式

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

题目大意

对于以下问题:

给定长度为 nn 的序列 aabb,满足以下条件:

  • 在序列 aa 与序列 bb 中,112n2n 的整数各出现恰好一次;
  • 对于 1in1\le i\le nai<bia_i<b_i
  • 对于 1i<n1\le i<nai<ai+1a_i<a_{i+1}

求:最多能在 [ai,bi][a_i,b_i] 中选出多少个两两不交的区间。

考虑以下算法:

11nn 枚举 ii,若 [ai,bi][a_i,b_i] 与所有已经选择的区间都不交,则选择该区间。最后输出选择的区间数。

给定 nn,求:有多少个满足条件的序列对 (a,b)(a,b),使得以上算法无法求出正确的结果。答案对 pp 取模。

3 100000007

2

4 100000007

28

15 999999937

935834920

提示

【样例解释 #1】

For example, consider the case where a=(1,2,4)a = (1, 2, 4) and b=(6,3,5)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,ba, 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 = (6, 3, 5)
  • a=(1,2,4),b=(5,3,6)a = (1, 2, 4), b = (5, 3, 6)

Therefore, output 22, which is the remainder of 22 when divided by 100000007100000007.

该样例满足所有子任务的限制。

【样例解释 #2】

There are 2828 pairs of sequences a,ba, b satisfying the condition. Therefore, output 2828, which is the remainder of 2828 when divided by 100000007100000007.

该样例满足所有子任务的限制。

【样例解释 #3】

There are 52950446022471485295044602247148 pairs of sequences a,ba, b satisfying the condition. Therefore, output 935834920935834920, which is the remainder of 935834920935834920 when divided by 999999937999999937.

该样例满足子任务 363 \sim 6 的限制。

【数据范围】

对于所有测试数据,满足 1N2×1041 \le N \le 2 \times 10 ^ 4108<P<10910 ^ 8 < P < 10 ^ 9,保证 PP 是质数,保证所有输入均为整数。

子任务编号 分值 NN \le
11 55 55
22 88
33 2727 3030
44 1414 300300
55 3636 30003000
66 1313 2×1042 \times 10 ^ 4