Interesting Permutations
Time limit5sMemory limit512 MB
Count permutations of 1..n whose first i elements are pairwise coprime, for every i, modulo m, stopping at the first impossible i.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Number theory, Combinatorics, Bit manipulation
- Solved
- No attempts yet
Problem
Vasya studies permutations of length : sequences of integers such that every integer from to occurs in the sequence exactly once. Vasya says a permutation is -interesting if its first elements are pairwise coprime. By this definition, if a permutation is -interesting, it is also -interesting.
Now Vasya wants to find the number of -interesting permutations for all from to . If there is no -interesting permutation for a given , Vasya won't bother calculating for larger values of . For example, as numbers and are not coprime, there is no -interesting permutation of length .
Vasya does not like huge integers, so he calculates the number of permutations modulo a given integer . Help him do it.
Input
The first line contains two integers and (, ).
Output
Print lines, where is the maximum number such that there is at least one -interesting permutation of length . On the -th line, print the remainder modulo of the number of -interesting permutations of length .