Submission #1038431

# Submission time Handle Problem Language Result Execution time Memory
1038431 2024-07-29T19:21:10 Z andrei_c1 NoM (RMI21_nom) C++17
100 / 100
420 ms 72276 KB
#include <iostream>
#include <fstream>
#include <algorithm>
 
/*
     Time       O(M * N ^ 2)
     Memory     O(M * N)
*/
 
 
constexpr int MOD = (int)1e9 + 7;
constexpr int MAXN = 3000;
constexpr int MAXM = 3000;
 
int dp[MAXM + 1][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][N] = 1;
    int used = 0;
 
    for (int rem = M; rem > 0; rem--) {
        used += counter[rem];
        for (int pairs = 0; pairs <= N; pairs++) {
            if (dp[rem][pairs] == 0) 
                continue;
 
            int need = counter[rem - 1];
            for (int p = 0; p <= pairs; p++) {
                int others = 2 * N - 2 * pairs - used;
 
                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][pairs]) % MOD;
                add(dp[rem - 1][pairs - p], current);
            }
        }
    }
 
    int answer = (1LL * dp[0][0] * pow2[N]) % MOD;
    for (int rem = 0; rem < M; rem++) {
        answer = (1LL * answer * fact[counter[rem]]) % MOD;
    }
 
    fout << answer << "\n";
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4584 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4584 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 6716 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4584 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 6716 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 9048 KB Output is correct
21 Correct 1 ms 9308 KB Output is correct
22 Correct 5 ms 14172 KB Output is correct
23 Correct 1 ms 13148 KB Output is correct
24 Correct 3 ms 12124 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 4 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4584 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 6716 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 9048 KB Output is correct
21 Correct 1 ms 9308 KB Output is correct
22 Correct 5 ms 14172 KB Output is correct
23 Correct 1 ms 13148 KB Output is correct
24 Correct 3 ms 12124 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 4 ms 14428 KB Output is correct
28 Correct 5 ms 16476 KB Output is correct
29 Correct 13 ms 18548 KB Output is correct
30 Correct 19 ms 19548 KB Output is correct
31 Correct 5 ms 21080 KB Output is correct
32 Correct 14 ms 23640 KB Output is correct
33 Correct 24 ms 25484 KB Output is correct
34 Correct 46 ms 30804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4584 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 6716 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 9048 KB Output is correct
21 Correct 1 ms 9308 KB Output is correct
22 Correct 5 ms 14172 KB Output is correct
23 Correct 1 ms 13148 KB Output is correct
24 Correct 3 ms 12124 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 4 ms 14428 KB Output is correct
28 Correct 5 ms 16476 KB Output is correct
29 Correct 13 ms 18548 KB Output is correct
30 Correct 19 ms 19548 KB Output is correct
31 Correct 5 ms 21080 KB Output is correct
32 Correct 14 ms 23640 KB Output is correct
33 Correct 24 ms 25484 KB Output is correct
34 Correct 46 ms 30804 KB Output is correct
35 Correct 24 ms 46684 KB Output is correct
36 Correct 301 ms 61884 KB Output is correct
37 Correct 23 ms 45904 KB Output is correct
38 Correct 138 ms 52052 KB Output is correct
39 Correct 225 ms 53588 KB Output is correct
40 Correct 420 ms 72276 KB Output is correct