#P7154. [USACO20DEC] Sleeping Cows P
[USACO20DEC] Sleeping Cows P
Problem Description
Farmer John has cows of various sizes (). He originally custom-built a stall for each cow, but now some cows have grown, so the original stall sizes are no longer sufficient. Specifically, FJ originally built stalls with sizes , and the cows now have sizes ().
Every night, the cows find stalls to sleep in in some way. Cow can sleep in stall if and only if her size can fit into the stall (). Each stall can have at most one cow sleeping in it.
We say a matching between cows and stalls is maximal if and only if every assigned cow can fit into her assigned stall, and for every cow that is not assigned a stall, she cannot fit into any unassigned empty stall.
Compute the number of maximal matchings modulo .
Input Format
The first line contains .
The second line contains space-separated integers .
The third line contains space-separated integers .
Output Format
Output the number of maximal matchings modulo .
4
1 2 3 4
1 2 2 3
9
Hint
Below are all nine maximal matchings. The ordered pair means cow is assigned to stall .
(1, 1), (2, 2), (3, 4)
(1, 1), (2, 3), (3, 4)
(1, 1), (2, 4)
(1, 2), (2, 3), (3, 4)
(1, 2), (2, 4)
(1, 3), (2, 2), (3, 4)
(1, 3), (2, 4)
(1, 4), (2, 2)
(1, 4), (2, 3)
- In testdata 2-3, .
- In testdata 4-12, .
- In testdata 13-20, there are no additional constraints.
Problem provided by: Nick Wu.
Translated by ChatGPT 5