#P5664. [CSP-S 2019] Emiya 家今天的饭
[CSP-S 2019] Emiya 家今天的饭
Problem Description
Emiya is a high school student who is good at cooking. He has mastered kinds of cooking methods, and uses kinds of main ingredients. For convenience, we number cooking methods from to , and main ingredients from to .
Every dish Emiya makes will use exactly one cooking method and exactly one main ingredient. More specifically, Emiya can make different dishes that use cooking method and main ingredient (, ). This also means Emiya can make a total of $\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} a_{i,j}$ different dishes.
Today, Emiya is going to prepare a meal to treat his good friends Yazid and Rin. However, the three of them have different requirements for how the dishes are combined. More specifically, for a plan (a selection) that contains dishes:
- Emiya will not let everyone go hungry, so he will make at least one dish, i.e. .
- Rin wants to taste dishes made with different cooking methods, so she requires that the cooking methods of all dishes are pairwise different.
- Yazid does not want to taste too many dishes made from the same ingredient, so he requires that each main ingredient is used in at most half of the dishes (i.e. at most dishes).
Here, is the floor function, meaning the greatest integer not exceeding .
These requirements are not hard for Emiya, but he wants to know how many different plans satisfy them. Two plans are different if and only if there exists at least one dish that appears in one plan but not in the other.
Emiya asks you for help. Please compute the number of valid plans, and output the result modulo the prime .
Input Format
The first line contains two integers separated by a single space.
Lines to each contain integers separated by single spaces. On line , the numbers are in order.
Output Format
Output a single integer: the number of valid plans modulo .
2 3
1 0 1
0 1 1
3
3 3
1 2 3
4 5 0
6 0 0
190
5 5
1 0 0 1 1
0 1 0 1 0
1 1 1 1 0
1 0 1 0 1
0 1 1 0 1
742
Hint
[Sample 1 Explanation]
In this sample, for every pair , Emiya can make at most one dish, so we directly describe a dish by giving the indices of its cooking method and main ingredient.
Valid plans include:
- Make one dish using cooking method and main ingredient , and one dish using cooking method and main ingredient .
- Make one dish using cooking method and main ingredient , and one dish using cooking method and main ingredient .
- Make one dish using cooking method and main ingredient , and one dish using cooking method and main ingredient .
Therefore the output is . Note that all plans that contain only one dish are invalid, because the only main ingredient appears in more than half of the dishes, which does not satisfy Yazid’s requirement.
[Sample 2 Explanation]
Emiya must make at least dishes.
The number of valid plans with dishes is .
The number of valid plans with dishes is .
Therefore the total number of valid plans is .
[Constraints]
::cute-table{tuack}
| Test Point ID | |||
|---|---|---|---|
| ^ | ^ | ||
| ^ | |||
| ^ | |||
| ^ | ^ | ||
| ^ | |||
For all test points, it is guaranteed that , , .
Translated by ChatGPT 5