Sleeping Cows
Time limit1sMemory limit512 MB
Count the maximal matchings between cows and barns, where each cow fits in its assigned barn and no unmatched cow fits in any empty barn, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Combinatorics
- Solved
- No attempts yet
Problem
Farmer John has cows of various sizes. He originally built each cow a personalized barn, but now some of the cows have outgrown their barns. Specifically, FJ built barns of sizes , while the cows are now of sizes ().
Every night, the cows go through a ritual of finding a barn to sleep in. Cow can sleep in barn if and only if it fits inside the barn (). Each barn can house at most one cow.
A matching of cows to barns is maximal if every cow assigned to a barn fits in that barn, and every unassigned cow is too big for every empty barn left out of the matching.
Compute the number of maximal matchings modulo .
Input
The first line contains .
The second line contains space-separated integers .
The third line contains space-separated integers .
Output
Print the number of maximal matchings modulo .
Hint
Here is a list of all nine maximal matchings. An ordered pair means that cow is assigned to barn .
(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)