This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, MOD;
const int MAXN = 20005;
int dp[MAXN][2];
int fac[3*MAXN];
int facinv[3*MAXN];
int expo(ll a, int b) {
ll cur = 1;
while (b) {
if (b & 1) {
cur = (cur*a)%MOD;
}
a = (a*a)%MOD;
b /= 2;
}
return cur;
}
int inv(int a) {
return expo(a, MOD-2);
}
int choose(int a, int b) {
ll cur = (ll)fac[a]*facinv[b];
cur %= MOD;
return (cur*facinv[a-b])%MOD;
}
void fastmod(int &a) {
if (a > MOD) a -= MOD;
}
int main() {
cin >> n >> MOD;
fac[0] = facinv[0] = 1;
for (int i = 1; i <= 3*n; i++) {
fac[i] = ((ll)i*fac[i-1])%MOD;
}
facinv[3*n] = inv(fac[3*n]);
for (int i = 3*n-1; i > 0; i--) {
facinv[i] = ((ll)(i+1)*facinv[i+1])%MOD;
}
dp[0][0] = dp[0][1] = 1;
// for (int i = 0; i <= n; i++) {
// dp[0][0] = fac[i];
// dp[0][1] = fac[i+1];
// }
for (int i = 1; i <= n; i++) {
for (int a = 0; a <= i-1; a++) {
for (int t2 = 0; t2 < 2; t2++) {
if (t2 && a == i-1) continue; // factorial of
ll cur = (ll)dp[i-a-1-t2][t2]*fac[a+t2];;
cur %= MOD;
cur *= choose(2*i-a-t2-2, a); cur %= MOD;
// if (i == 2 && t1 == 0) cerr << cur << '\n';
dp[i][0] += cur;
fastmod(dp[i][0]);
dp[i][1] += (cur*(a+1+t2))%MOD;
fastmod(dp[i][1]);
}
}
}
ll oth = 1;
for (int i = 2*n-1; i > 0; i -= 2) {
oth = (oth*i)%MOD;
}
cout << (oth+MOD-dp[n][0])%MOD << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |