#P1446. [HNOI2008] Cards

    ID: 439 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp数学2008各省省选湖南置换逆元

[HNOI2008] Cards

Description

Xiaochun is quite idle. Facing nn cards on the desk, he decides to color each card. He currently has 33 colors: red, blue, and green. He asks Sun how many colorings there are, and Sun quickly gives the answer.

Furthermore, Xiaochun requires exactly SrS_r red, SbS_b blue, and SgS_g green cards. He asks again how many colorings there are; Sun thinks for a moment and gives the correct answer. Finally, Xiaochun invents mm different shuffles. He asks Sun how many essentially different colorings there are now. Two colorings are considered the same if and only if one can be transformed into the other by applying arbitrary shuffles (that is, you may use multiple shuffles, and each shuffle may be used multiple times).

Sun finds this problem somewhat challenging and decides to leave it to you. Since the answer may be large, you only need to output the result modulo PP. It is guaranteed that PP is a prime.

Description

Input Format

The first line contains 55 integers: Sr,Sb,Sg,m,PS_r, S_b, S_g, m, P (with m60,m+1<P<100m \le 60, m+1 < P < 100). Here, the nn mentioned in the statement is Sr+Sb+SgS_r + S_b + S_g, i.e., n=Sr+Sb+Sgn = S_r + S_b + S_g.

The next mm lines each describe a shuffle. Each line has nn space-separated integers X1,X2,,XnX_1, X_2, \dots, X_n, guaranteed to be a permutation of 11 to nn, meaning that when using this shuffle, position ii becomes the card that was originally at position XiX_i. The input guarantees that any number of shuffles can be replaced by one of these mm shuffles, and that for every shuffle, there exists a shuffle that brings you back to the original state.

Also, for 100%100\% of the testdata, max{Sr,Sb,Sg}20\max\{S_r, S_b, S_g\} \le 20.

Output Format

Output the number of distinct colorings modulo PP.

1 1 1 2 7
2 3 1
3 1 2

2

Hint

There are 22 essentially different colorings: RGB and RBG. Using shuffle 231 once gives GBR and BGR; using shuffle 312 once gives BRG and GRB.

Translated by ChatGPT 5