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>
#define INF 1000000005
#define INFL 1000000000000000005ll
#define EPS 1e-6
#define pb push_back
#define pause system("pause")
#define exit exit(0)
#define endl '\n'
using namespace std;
using ull = unsigned long long;
using ll = long long;
typedef pair<ll, ll> pii;
const int N = 2005, MOD = 1e9 + 7;
int n, m, f[N], c[N][N], dp[N][N];
inline int add(int a, int b) {
return a + b < MOD ? (a + b) : (a + b - MOD);
}
inline int mul(int a, int b) {
return ((ll)a * b) % MOD;
}
inline void upd(int &a, int b) {
a = add(a, b);
}
inline int bpow(int a, int b) {
int ans = 1;
while (b != 0) {
if (b & 1)
ans = mul(ans, a);
a = mul(a, a), b >>= 1;
}
return ans;
}
int fact(int n) {
int ans = 1;
for (int i = 2; i <= n; ++i) {
ans = mul(ans, i);
}
return ans;
}
int naive(int n, int m) {
vector<int> p(2 * n);
for (int i = 0; i < 2 * n; ++i) {
p[i] = i + 1;
}
int ans = 0;
do {
bool ok = 1;
for (int i = 0; ok && i < 2 * n; ++i) {
for (int j = i + 1; j < 2 * n; ++j) {
if (abs(p[i] - p[j]) == n) {
ok &= ((j - i) % m) != 0;
break;
}
}
}
ans += ok;
} while (next_permutation(p.begin(), p.end()));
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
//cout << naive(n, m) << endl;
for (int i = 0; i <= n; ++i) {
c[i][0] = 1;
for (int j = 1; j <= i; ++j) {
c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]);
}
}
for (int i = 0; i < 2 * n; ++i) {
++f[i % m];
}
int sm = f[0];
dp[0][f[0]] = mul(c[n][f[0]], bpow(2, f[0]));
for (int i = 0; i < m - 1; ++i) {
for (int j = 0; j <= sm; ++j) {
for (int k = 0; k <= j && k <= f[i + 1]; ++k) {
if (j - k + f[i + 1] - k > n || dp[i][j] == 0)
continue;
upd(dp[i + 1][j - k + f[i + 1] - k],
mul(
dp[i][j],
mul(
c[j][k],
mul(
c[n - (((sm - j) / 2) + j)][f[i + 1] - k],
bpow(2, f[i + 1] - k)
)
)
)
);
}
}
sm += f[i + 1];
}
int ans = dp[m - 1][0];
for (int i = 0; i < m; ++i) {
ans = mul(ans, fact(f[i]));
}
cout << ans << endl;
//pause;
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... |