Festivals in JOI Kingdom 2
Time limit6sMemory limit1024 MB
Count the pairs of sequences a and b of N intervals using each value from 1 to 2N once, where greedy selection by start time is not optimal, modulo prime P.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Greedy
- Solved
- No attempts yet
Problem
In JOI Kingdom, a national festival is held once a year. During the festival, there are events in total, and the schedule of each event is already fixed. The schedules of the events are described by sequences and of length that satisfy the following conditions.
- Every integer from to , inclusive, appears as an element of or .
- ().
- ().
The -th event starts at minutes after the beginning of the festival and ends at minutes after the beginning of the festival.
Participants may choose any events they like. However, they may not take part in two events whose schedules overlap. (The starting times and ending times of all events are different from each other.)
JOI-kun wants to take part in as many events as possible. Until last year, he chose events on a computer with the following procedure.
- For , do the following in this order.
- If the schedule of the -th event does not overlap the schedules of the events he has already chosen, he takes part in the -th event. Otherwise, he does not.
After studying computer science, JOI-kun noticed that this procedure does not always maximize the number of events he takes part in. Starting this year, he will use an improved algorithm. With the improved algorithm, JOI-kun will be able to maximize the number of events he takes part in.
JOI-kun wants to know the number of cases in which the improved algorithm produces a larger number of events.
Given the integer and a large prime , write a program that counts the pairs of sequences and describing the schedules for which the improved algorithm produces a larger number of events. Output the count modulo .
Input
Read the following data from standard input.
Output
Write one line to standard output. The line contains the number of pairs of sequences and describing the schedules of the events for which the improved algorithm produces a larger number of events, modulo .
Constraints
- .
- .
- is a prime number.
- All given values are integers.