luogu#P8062. [BalkanOI 2012] handsome
[BalkanOI 2012] handsome
Background
You are the most handsome among OIers, so you have to solve a problem related to being handsome.
Problem Description
An -digit number is handsome if and only if every digit is one of . Also, the ordered pair formed by any two adjacent digits is not a “dangerous pair”.
-
An -digit number is lexicographically smaller than an -digit number under the permutation if and only if there exists a such that $X_{p_1}=Y_{p_1},X_{p_2}=Y_{p_2},\dots,X_{p_{k}}<Y_{p_{k}}$.
-
An -digit number is lexicographically equal to an -digit number under the permutation if and only if $X_{p_1}=Y_{p_1},X_{p_2}=Y_{p_2},\dots,X_{p_{N}}=Y_{p_{N}}$.
-
An -digit number is lexicographically greater than an -digit number under the permutation if and only if there exists a such that $X_{p_1}=Y_{p_1},X_{p_2}=Y_{p_2},\dots,X_{p_{k}}>Y_{p_{k}}$.
Please output the number of handsome numbers that are under the permutation , modulo .
Input Format
The first line contains an integer , the number of digits.
The second line contains space-separated integers, representing the permutation . The -th integer on this line is .
The third line contains an integer , the number of dangerous pairs.
The fourth line contains space-separated two-digit integers. The -th two-digit integer indicates that is a dangerous pair.
The fifth line contains a positive integer , the given handsome number.
Output Format
Output one line: the number of handsome numbers that are under the permutation , modulo .
3
2 1 3
2
22 13
321
9
Hint
Constraints
Subtask#0 is the sample.
, , .
It is guaranteed that is a handsome number.
Sample Explanation
are not handsome numbers, because the dangerous pair or appears.
So, all handsome -digit numbers are: $111,112,121,123, 211,212,231,232,233, 311,312,321,323,331,332,333$.
Under the permutation , the handsome numbers that are are: .
For example, compare and under the permutation (let be and be ):
- First compare the -nd digit: .
- Then compare the -st digit: .
- Finally compare the -rd digit: .
So is greater than under the permutation , and it is not a valid handsome number.
Translated by ChatGPT 5