Farmer John has come up with a new morning exercise routine for the cows (again)!
As before, Farmer John's NN cows (1≤N≤1041≤N≤104) are standing in a line. The ii-th cow from the left has label ii for each 1≤i≤N1≤i≤N. He tells them to repeat the following step until the cows are in the same order as when they started.
For example, if A=(1,2,3,4,5)A=(1,2,3,4,5) then the cows perform one step. If A=(2,3,1,5,4)A=(2,3,1,5,4), then the cows perform six steps. The order of the cows from left to right after each step is as follows:
Find the sum of all positive integers KK such that there exists a permutation of length NN that requires the cows to take exactly KK steps.
As this number may be very large, output the answer modulo MM (108≤M≤109+7108≤M≤109+7, MM is prime).
The first line contains NN and MM.
A single integer.
5 1000000007
21
There exist permutations that cause the cows to take 11, 22, 33, 44, 55, and 66 steps. Thus, the answer is 1+2+3+4+5+6=211+2+3+4+5+6=21.
Problem credits: Benjamin Qi
[/hide]
(Analysis by Benjamin Qi)
Call the number of steps that a permutation takes the period of the permutation. Every permutation can be partitioned into cycles of sizes c1,c2,c3,…,ckc1,c2,c3,…,ck such that c1+c2+…+ck=Nc1+c2+…+ck=N (see Swapity Swapity Swap from the last contest). Then the period is equal to lcm(c1,c2,…,ck)lcm(c1,c2,…,ck).
Suppose that we want find the minimum nn such that there exists a permutation of length nn with period K=∏peii,K=∏piei, where the right side denotes the prime factorization of KK. It turns out that n=∑peiin=∑piei because
Thus, we need to find the sum of all positive integers KK such that ∑peii≤N∑piei≤N.
Subtask N≤100N≤100:
Just searching for all possible KK suffices (there are only 1866318663 for N=100N=100). However, this number grows quite rapidly as NN increases.
Full Credit:
Maintain a DP table storing the sum of all possible KK for each prime power sum nn in [0,N][0,N]. Then we can add prime powers in increasing order of pp and update the table in O(N)O(N) for each of them.
Unfortunately, I was unaware that the sequence could be found on OEIS. I'll try to be more careful about this in the future ...
Mark Chen's code:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAXP = 1234; const int MAXN = 10005; LL res[MAXP][MAXN]; // result for permutations of length n restricted to using the first p primes int n; LL m; LL mul(LL x, LL y) { return (x * y) % m; } LL add(LL x, LL y) { x += y; if (x >= m) x -= m; return x; } LL sub(LL x, LL y) { x -= y; if (x < 0) x += m; return x; } int main() { freopen("exercise.in","r",stdin); freopen("exercise.out","w",stdout); cin >> n >> m; vector<int> composite(n+1); vector<int> primes; for (int i = 2; i <= n; i++) { if (!composite[i]) { primes.push_back(i); for (int j = 2*i; j <= n; j += i) { composite[j] = 1; } } } if (primes.size() == 0) { cout << "1\n"; return 0; } for (int j = 0; j <= n; j++) res[0][j] = 1; // identities for (int i = 1; i <= primes.size(); i++) { for (int j = 0; j <= n; j++) { res[i][j] = res[i-1][j]; int pp = primes[i-1]; while (pp <= j) { res[i][j] = add(res[i][j], mul(pp, res[i-1][j-pp])); pp *= primes[i-1]; } } } cout << res[primes.size()][n] << "\n"; }
[/hide]
© 2024. All Rights Reserved. 沪ICP备2023009024号-1