Interval-Free Permutations
Time limit2sMemory limit512 MB
Count permutations of length n with no contiguous block of length 2 to n-1 whose values form consecutive integers, modulo a prime p.
- Level
Hard9 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Consider a permutation of the integers from to . A sub-segment is called an interval if it is a reordering of some set of consecutive integers. For example, the permutation has the intervals , , , and others.
Every permutation has trivial intervals: the full permutation and every single element. A permutation is interval-free if it has no non-trivial intervals, meaning no intervals of length between and inclusive.
Count the interval-free permutations of length and print the result modulo the prime .
Input
The first line contains two integers () and (), the number of test cases and the prime modulo. Each of the next lines contains one integer (), the length of the permutation.
Output
For each test case, print a single integer: the number of interval-free permutations of length modulo .
Hint
For , the only permutation is interval-free. For , the interval-free permutations are and . For , they are , , , , , and . For there are of them, so the full list is omitted. Examples include , , , and .