문제
Farmer John has come up with a new morning exercise routine for the cows (again)!
As before, Farmer John's
Given a permutation
A of lengthN , the cows change their order such that thei -th cow from the left before the change isA_i -th from the left after the change.
For example, if
0 steps:
(1,2,3,4,5) 1 step:
(3,1,2,5,4) 2 steps:
(2,3,1,4,5) 3 steps:
(1,2,3,5,4) 4 steps:
(3,1,2,4,5) 5 steps:
(2,3,1,5,4) 6 steps:
(1,2,3,4,5)
Compute the product of the numbers of steps needed over all
As this number may be very large, output the answer modulo
Contestants using C++ may find the following code from KACTL helpful. Known as the Barrett reduction, it allows you to compute
#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
}
SCORING:
Test case 2 satisfies
N=8 .Test cases 3-5 satisfy
N\le 50 .Test cases 6-8 satisfy
N\le 500 .Test cases 9-12 satisfy
N\le 3000 .Test cases 13-16 satisfy no additional constraints.
Problem credits: Benjamin Qi
입력
The first line contains
출력
A single integer.
예제1
5 1000000007
369329541
For each
Note: This problem has an expanded memory limit of 512 MB.