#P3477. [POI 2008] PER-Permutation
[POI 2008] PER-Permutation
Problem Description
A multiset is like a set, but elements may appear more than once.
A permutation of a multiset is any ordering of its elements. For example, among the permutations of the multiset , there are and .
We use lexicographic order to compare two permutations: at the first position where they differ, the permutation with the smaller element is considered smaller. All permutations of a given multiset can be sorted increasingly and numbered starting from (this number is called the rank).
Task: Given a permutation of a multiset of size and a positive integer , compute the remainder modulo of the rank of this permutation in lexicographic order.
Input Format
- The first line contains two integers and (): the size of the multiset and the modulus .
- The second line contains positive integers (), the given permutation of the multiset.
Output Format
Output a single integer: the rank of the given permutation in lexicographic order, taken modulo .
4 1000
2 1 10 2
5
Hint
Thanks to @远航之曲 for the translation.
Translated by ChatGPT 5.