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 mod = 1e9 + 7;
const int kN = 2e3;
int f[1 + 2 * kN], invf[1 + 2 * kN], cnt[1 + kN], dp[2][1 + kN];
void multSelf(int &x, int y) {
x = (int64_t)x * y % mod;
}
int mult(int x, int y) {
multSelf(x, y);
return x;
}
void addSelf(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
int invers(int x) {
int ans = 1, n = mod - 2;
while (n) {
if (n % 2) {
multSelf(ans, x);
}
multSelf(x, x);
n /= 2;
}
return ans;
}
void computeFactorials(int n) {
f[0] = 1;
for (int i = 1; i <= 2 * n; ++i) {
f[i] = mult(f[i - 1], i);
}
invf[2 * n] = invers(f[2 * n]);
for (int i = 2 * n - 1; i >= 0; --i) {
invf[i] = mult(invf[i + 1], i + 1);
}
}
int arr(int n, int k) {
return mult(f[n], invf[n - k]);
}
int nck(int n, int k) {
return mult(f[n], mult(invf[k], invf[n - k]));
}
void testCase() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= 2 * n; ++i) {
++cnt[1 + i % m];
}
computeFactorials(n);
int sum = 0;
dp[0][0] = dp[1][0] = 1;
for (int i = 1, t = 1; i <= m; ++i, t ^= 1) {
sum += cnt[i];
for (int j = 1; j * 2 <= sum; ++j) {
dp[t][j] = 0;
for (int k = 0; k <= j && 2 * k <= cnt[i]; ++k) {
addSelf(dp[t][j], mult(dp[t ^ 1][j - k], mult(arr(cnt[i], 2 * k), nck(j, k))));
}
}
}
int ans = f[2 * n];
for (int i = 1; i <= n; ++i) {
int contribution = mult(nck(n, i), mult(f[2 * n - 2 * i], dp[m % 2][i]));
if (i % 2) {
contribution = mod - contribution;
}
addSelf(ans, contribution);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# | 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... |