#P7752. [COCI 2013/2014 #2] PALETA
[COCI 2013/2014 #2] PALETA
Description
Mirko has colors and pictures that need to be colored. The pictures are numbered from to , and he decided to fill each picture using one of the colors.
To do this, he chose numbers , and decided to color the pictures in the following way:
- If , then the color of picture must not be the same as the color of picture .
- If , then as long as all other conditions are satisfied, he may color picture with any color.
You need to find the number of possible ways to color the pictures.
Since the output may be very large, you only need to output the answer modulo .
Input Format
The first line contains two integers , representing the number of pictures and the number of colors.
The next lines each contain .
Output Format
Output a single integer: the number of possible ways to color the pictures modulo .
2 3
2 1
6
3 4
2 3 1
24
3 4
2 1 1
36
3 4
1 1 2
36
Hint
Explanation for Sample 1
There are colors, and the color of picture must not be the same as the color of picture . The possible colorings are , where the two numbers in parentheses represent the colors of the two pictures.
Explanation for Sample 4
There are colors.
- For picture , it can be colored with any color.
- For picture , its color must not be the same as the color of picture .
- For picture , its color must not be the same as the color of picture .
That is, picture can be colored in ways excluding the color of picture , and picture can be colored in ways excluding the color of picture .
The answer is .
Constraints
- For of the testdata, .
- For of the testdata, .
For all valid , .
Source
This problem is translated from COCI2013-2014 CONTEST 2 T5 PALETA.
According to the original testdata settings, the full score of this problem is points.
Translated by ChatGPT 5
京公网安备 11011102002149号