#QHD0000084. Bài tập hoán vị (Exercise)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài tập hoán vị (Exercise)
Exercise
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
There are distinct cows initially standing in positions through . Farmer John chooses a fixed permutation of . In every step, the cow currently at position moves to position . The same permutation is used repeatedly.
Let be the smallest positive number of steps after which the cows return to their initial arrangement. In particular, the identity permutation has , not .
Consider all distinct positive integers such that at least one permutation on elements has . Compute the sum of these distinct values, not the number of permutations giving them. Print the result modulo the given prime .
Every permutation decomposes into disjoint cycles. A cycle of length returns to its original state after steps, and hence is the least common multiple of all cycle lengths.
Input
A single line contains two integers and .
Output
Print the sum of all distinct attainable permutation orders, modulo .
Subtasks
-
Subtask 1 (20 points): ; is prime and .
-
Subtask 2 (30 points): ; is prime and .
-
Subtask 3 (50 points): ; is prime and .
Examples
Example 1
Input
5 1000000007
Output
21
Explanation: The attainable distinct orders are 1, 2, 3, 4, 5 and 6, whose sum is 21.
Example 2
Input
3 998244353
Output
6
Explanation: The only possible orders for three elements are 1, 2 and 3, with sum 6.