Code Permutations
Time limit2sMemory limit128 MB
Count permutations of 1..N whose order (LCM of cycle lengths) equals K, modulo 2^31-1.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Number theory, Math
- Solved
- No attempts yet
Problem
You must break into a safe whose lock accepts the natural numbers from to entered in one fixed secret order. That order is a permutation of , and you know for certain that this permutation has order exactly .
The order of a permutation is the smallest positive integer such that applying the permutation times returns every element to its original position. Equivalently, it is the least common multiple of the lengths of the permutation's cycles. For example, the code has order , since , , and .
Knowing the order narrows down how many codes you might have to try, and you want that count exactly. Because you refuse to acknowledge any number larger than the prime , report the count modulo (so a count of , for instance, becomes ).
Given and , determine how many permutations of have order exactly , modulo .
Input
A single line with two integers and (, ).
Output
Print one integer: the number of permutations of elements whose order is exactly , taken modulo .