순열의 걸음 수는 그 위수, 즉 순환 길이들의 최소공배수다. N!개의 모든 순열에 대해 이 위수의 곱을 소수 M으로 나눈 나머지를 N이 7500 이하일 때 구한다.
어려움9조합론정수론수학동적 계획법아직 제출이 없습니다시간 제한2초메모리 제한512 MBFarmer John has come up with a new morning exercise routine for the cows (again)!
As before, Farmer John's N cows (1≤N≤7500) are standing in a line. The i-th cow from the left has label i for each 1≤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) then the cows perform one step and immediately return to the same order. If A=(2,3,1,5,4), then the cows perform six steps before returning to the original order. The order of the cows from left to right after each step is as follows:
Compute the product of the numbers of steps needed over all N! possible permutations A of length N.
As this number may be very large, output the answer modulo M (108≤M≤109+7, M is prime).
Contestants using C++ may find the following code from KACTL helpful. Known as the Barrett reduction, it allows you to compute a several times faster than usual, where b>1 is constant but not known at compile time. (we are not aware of such an optimization for Java, unfortunately).
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod F(2);
int main() {
int M = 1000000007; F = FastMod(M);
ull x = 10ULL*M+3;
cout << x << " " << F.reduce(x) << "\n"; // 10000000073 3
}
The first line contains N and M.
A single integer.
For each 1≤i≤N, the i-th element of the following array is the number of permutations that cause the cows to take i steps: \[1,25,20,30,24,20]. The answer is 11⋅225⋅320⋅430⋅524⋅620≡369329541(mod109+7).