// nom_naive_fast_popovici.cpp
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
/*
Time O(M * N ^ 2)
Memory O(M * N + N * N)
*/
constexpr int MOD = (int)1e9 + 7;
constexpr int MAXN = 3000;
constexpr int MAXM = 3000;
int dp[2][MAXN + 1][2 * MAXN + 1];
short visited[2][MAXN + 1][2 * MAXN + 1];
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 main() {
std::istream& fin = std::cin;
std::ostream& fout = std::cout;
int N, M;
fin >> N >> M;
prec(N, M);
dp[M & 1][N][0] = 1;
visited[M & 1][N][0] = M + 1;
int used = 0;
for (int rem = M; rem > 0; rem--) {
used += counter[rem];
for (int pairs = 0; pairs <= N; pairs++) {
for (int others = 0; others + 2 * pairs <= 2 * N - used; others++) {
if (dp[rem & 1][pairs][others] == 0 || visited[rem & 1][pairs][others] != rem + 1)
continue;
int need = counter[rem - 1];
for (int p = std::max(0, need - others); p <= std::min(pairs, need); p++) {
// if (need > p + others || need < p)
// continue;
int current = (1LL * comb[pairs][p] * comb[others][need - p]) % MOD;
// current = (1LL * current * pow2[p]) % MOD;
current = (1LL * current * dp[rem & 1][pairs][others]) % MOD;
short& last_visited = visited[1 - rem & 1][pairs - p][others + p - (need - p)];
int& dp_value = dp[1 - rem & 1][pairs - p][others + p - (need - p)];
if (last_visited != rem) {
last_visited = rem;
dp_value = 0;
}
add(dp_value, current);
}
}
}
}
int answer = (1LL * dp[0][0][0] * pow2[N]) % MOD;
for (int rem = 0; rem < M; rem++) {
answer = (1LL * answer * fact[counter[rem]]) % MOD;
}
fout << answer << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:82:53: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
82 | short& last_visited = visited[1 - rem & 1][pairs - p][others + p - (need - p)];
| ~~^~~~~
Main.cpp:83:42: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
83 | int& dp_value = dp[1 - rem & 1][pairs - p][others + p - (need - p)];
| ~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
1952 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1868 KB |
Output is correct |
15 |
Correct |
2 ms |
1996 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
1 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1996 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
1952 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1868 KB |
Output is correct |
15 |
Correct |
2 ms |
1996 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
1 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1996 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
3 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
4300 KB |
Output is correct |
22 |
Correct |
12 ms |
7724 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
11 ms |
8664 KB |
Output is correct |
25 |
Correct |
15 ms |
7268 KB |
Output is correct |
26 |
Correct |
5 ms |
6604 KB |
Output is correct |
27 |
Correct |
16 ms |
8768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
1952 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1868 KB |
Output is correct |
15 |
Correct |
2 ms |
1996 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
1 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1996 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
3 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
4300 KB |
Output is correct |
22 |
Correct |
12 ms |
7724 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
11 ms |
8664 KB |
Output is correct |
25 |
Correct |
15 ms |
7268 KB |
Output is correct |
26 |
Correct |
5 ms |
6604 KB |
Output is correct |
27 |
Correct |
16 ms |
8768 KB |
Output is correct |
28 |
Correct |
26 ms |
18700 KB |
Output is correct |
29 |
Correct |
68 ms |
21984 KB |
Output is correct |
30 |
Correct |
119 ms |
21604 KB |
Output is correct |
31 |
Correct |
24 ms |
18372 KB |
Output is correct |
32 |
Correct |
108 ms |
23232 KB |
Output is correct |
33 |
Correct |
244 ms |
24460 KB |
Output is correct |
34 |
Correct |
270 ms |
33128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
1952 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1868 KB |
Output is correct |
15 |
Correct |
2 ms |
1996 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
1 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1996 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
3 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
4300 KB |
Output is correct |
22 |
Correct |
12 ms |
7724 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
11 ms |
8664 KB |
Output is correct |
25 |
Correct |
15 ms |
7268 KB |
Output is correct |
26 |
Correct |
5 ms |
6604 KB |
Output is correct |
27 |
Correct |
16 ms |
8768 KB |
Output is correct |
28 |
Correct |
26 ms |
18700 KB |
Output is correct |
29 |
Correct |
68 ms |
21984 KB |
Output is correct |
30 |
Correct |
119 ms |
21604 KB |
Output is correct |
31 |
Correct |
24 ms |
18372 KB |
Output is correct |
32 |
Correct |
108 ms |
23232 KB |
Output is correct |
33 |
Correct |
244 ms |
24460 KB |
Output is correct |
34 |
Correct |
270 ms |
33128 KB |
Output is correct |
35 |
Correct |
88 ms |
66016 KB |
Output is correct |
36 |
Execution timed out |
1078 ms |
55040 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |