#P9419. [POI 2021/2022 R1] Układanie kart
[POI 2021/2022 R1] Układanie kart
Background
Translated from XXIX Olimpiada Informatyczna – Stage I Układanie kart。
Problem Description
We sort a permutation into increasing order using the following method.
One operation: Let the first number be . Find in the permutation (if , take ). Move to the first position of the permutation, and shift the numbers in between one position to the right.
Cost of one operation: the position of (or ) in the original permutation (indexed starting from ).
Cost of a permutation: perform several operations until the permutation is sorted. The cost is the sum of the costs of all operations.
Given , find the sum of the costs over all permutations, modulo .
Input Format
One line with two positive integers, .
Output Format
One line with one integer, the answer modulo .
2 100
1
3 100
15
10 1000
100
500 100000
60000
100000 1000
0
Hint
For all testdata, ,。
| Subtask ID | Additional Constraints | Score |
|---|---|---|
| 1 | 10 | |
| 2 | 60 | |
| 3 | 30 |
Translated by ChatGPT 5