This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 4005;
const int MOD = 1e9 + 7;
int dp[2][N], fac[N], inv[N];
int add(int x, int y) {
return (x += y) >= MOD ? x - MOD : x;
}
int sub(int x, int y) {
return (x -= y) < 0 ? x + MOD : x;
}
int mul(int x, int y) {
return (long long)x * y % MOD;
}
int bin(int n, int k) {
if (n < k) return 0;
return mul(fac[n], mul(inv[k], inv[n - k]));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
fac[0] = inv[0] = inv[1] = 1;
for (int i = 2; i < N; i++)
inv[i] = MOD - mul(MOD / i, inv[MOD % i]);
for (int i = 1; i < N; i++) {
fac[i] = mul(fac[i - 1], i);
inv[i] = mul(inv[i - 1], inv[i]);
}
dp[0][0] = 1;
for (int i = 0; i < m; i++) {
int l = (2 * n - i - 1) / m + 1;
memset(dp[~i & 1], 0, sizeof dp[~i & 1]);
for (int j = 0; j <= n; j++)
for (int k = 0; k * 2 <= l && k <= j; k++)
dp[~i & 1][j] = add(dp[~i & 1][j],
mul(dp[i & 1][j - k], mul(bin(j, k),
mul(fac[l], inv[l - 2 * k]))));
}
int res = 0;
for (int i = 0; i <= n; i++) {
int cur = mul(dp[m & 1][i], mul
(bin(n, i), fac[2 * n - 2 * i]));
if (i & 1) res = sub(res, cur);
else res = add(res, cur);
}
cout << res;
}
# | 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... |