#P6276. [USACO20OPEN] Exercise P
[USACO20OPEN] Exercise P
Description
Farmer John has (again) come up with a new morning exercise plan for the cows!
As before, Farmer John’s cows stand in a line. For each , the -th cow from left to right has ID . He tells them to repeat the following step until the cows are in the same order as they started.
Given a permutation of length , the cows change their order so that the cow that was the -th from left to right before the change becomes the -th from left to right after the change.
For example, if , then the cows return to the same order after a total of one step. If , then the cows return to the starting order after a total of six steps. After each step, the order of the cows from left to right is as follows:
Step 0:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Please compute the product, over all permutations of length , of the number of steps needed to return to the starting order.
Since this number can be very large, output the remainder modulo (, and is prime).
C++ users may use the following code from KACTL. This algorithm, called Barrett reduction, can compute several times faster than the usual method, where is a constant unknown at compile time. (Unfortunately, we were unable to find a similar optimization for Java.) (Translator’s note: Chinese users may refer to 几种取模优化方法(译自 min-25 的博客).)
#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
}
Input Format
The input contains one line with and .
Output Format
Output one integer.
5 1000000007
369329541
Hint
Sample Explanation:
For each , the -th element of the following sequence equals the number of permutations for which the cows need steps: . Therefore, the answer is $1^1\cdot 2^{25}\cdot 3^{20}\cdot 4^{30}\cdot 5^{24}\cdot 6^{20}\equiv 369329541\pmod{10^9+7}$.
Note: The memory limit for this problem is increased to 512 MB.
For of the testdata, .
There are test points. Test point is the sample, and the others have the following properties:
Test point satisfies .
Test points satisfy .
Test points satisfy .
Test points satisfy .
Test points have no additional constraints.
Problem setter: Benjamin Qi
Translated by ChatGPT 5
京公网安备 11011102002149号