// nom_100_popovici.cpp
#include <iostream>
#include <fstream>
#include <cassert>
/*
Time O(M * N + N ^ 2)
Memory O(M * N + N ^ 2)
*/
constexpr int MOD = (int)1e9 + 7;
constexpr int MAXN = 3000;
constexpr int MAXM = 3000;
int comb[2 * MAXN + 1][2 * MAXN + 1];
int pow2[2 * MAXN + 1], fact[2 * MAXN + 1];
int counter[MAXM];
inline void add(int& x, int y) {
x += y;
if (x >= MOD)
x -= MOD;
}
void prec(int N, int M) {
for (int i = 0; i <= 2 * N; i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++) {
add(comb[i][j], comb[i - 1][j]);
add(comb[i][j], comb[i - 1][j - 1]);
}
}
for (int i = 0; i < 2 * N; i++) {
counter[i % M]++;
}
pow2[0] = fact[0] = 1;
for (int i = 1; i <= 2 * N; i++) {
pow2[i] = (2LL * pow2[i - 1]) % MOD;
fact[i] = (1LL * i * fact[i - 1]) % MOD;
}
}
int dp[MAXM + 1][MAXN + 1];
int ways[MAXN + 1];
int main() {
std::istream& fin = std::cin;
std::ostream& fout = std::cout;
int N, M;
fin >> N >> M;
prec(N, M);
dp[M][0] = 1;
for (int rem = M; rem > 0; rem--) {
for (int i = 0; i <= N; i++) {
if (dp[rem][i] == 0)
continue;
for (int j = 0; 2 * j <= counter[rem - 1]; j++) {
if (i + j > N)
assert(0);
int current = (1LL * comb[N - i][j] * comb[counter[rem - 1]][2 * j]) % MOD;
current = (1LL * current * fact[2 * j]) % MOD;
current = (1LL * current * dp[rem][i]) % MOD;
add(dp[rem - 1][i + j], current);
}
}
}
for (int i = N; i >= 0; i--) {
ways[i] = (1LL * dp[0][i] * fact[2 * N - 2 * i]) % MOD;
for (int j = i + 1; j <= N; j++) {
int current = MOD - (1LL * ways[j] * comb[j][i]) % MOD;
add(ways[i], current);
}
}
fout << ways[0] << "\n";
return 0;
}
/*
{1}
dp[rem][i] * coef -> dp[rem - 1][i + j], coef = C(N - i, j) * A(counter[rem - 1], 2 * j)
{1}
ways[i] = dp[0][i] * (2 * N - 2 * i)!
{1}
ways[i] = ways[i] - sum(ways[j] * C(j, i))
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1868 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
5 ms |
4044 KB |
Output is correct |
25 |
Correct |
4 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
2892 KB |
Output is correct |
27 |
Correct |
6 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1868 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
5 ms |
4044 KB |
Output is correct |
25 |
Correct |
4 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
2892 KB |
Output is correct |
27 |
Correct |
6 ms |
4812 KB |
Output is correct |
28 |
Correct |
9 ms |
8524 KB |
Output is correct |
29 |
Correct |
14 ms |
10752 KB |
Output is correct |
30 |
Correct |
15 ms |
11728 KB |
Output is correct |
31 |
Correct |
11 ms |
8660 KB |
Output is correct |
32 |
Correct |
13 ms |
11260 KB |
Output is correct |
33 |
Correct |
16 ms |
13392 KB |
Output is correct |
34 |
Correct |
32 ms |
19096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1868 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
5 ms |
4044 KB |
Output is correct |
25 |
Correct |
4 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
2892 KB |
Output is correct |
27 |
Correct |
6 ms |
4812 KB |
Output is correct |
28 |
Correct |
9 ms |
8524 KB |
Output is correct |
29 |
Correct |
14 ms |
10752 KB |
Output is correct |
30 |
Correct |
15 ms |
11728 KB |
Output is correct |
31 |
Correct |
11 ms |
8660 KB |
Output is correct |
32 |
Correct |
13 ms |
11260 KB |
Output is correct |
33 |
Correct |
16 ms |
13392 KB |
Output is correct |
34 |
Correct |
32 ms |
19096 KB |
Output is correct |
35 |
Correct |
57 ms |
34220 KB |
Output is correct |
36 |
Correct |
98 ms |
51768 KB |
Output is correct |
37 |
Correct |
50 ms |
33484 KB |
Output is correct |
38 |
Correct |
96 ms |
40868 KB |
Output is correct |
39 |
Correct |
89 ms |
42980 KB |
Output is correct |
40 |
Correct |
173 ms |
63524 KB |
Output is correct |