답안 #835145

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
835145 2023-08-23T09:09:32 Z RaulAndrei01 NoM (RMI21_nom) C++14
100 / 100
96 ms 109312 KB
#include <iostream>


using namespace std;
using ll = long long;
const int NMAX = 2002;
const int MOD = 1e9 + 7;


ll dp[NMAX][NMAX];
ll c[2 * NMAX][2 * NMAX];
ll fact[2 * NMAX], ifact[2 * NMAX];


ll imod(ll x)
{
    ll rez = 1;
    for (int bit = 0; (1 << bit) <= MOD - 2; bit++)
    {
        if ((1 << bit) & (MOD - 2))
        {
            rez = (rez * x) % MOD;
        }
        x = (x * x) % MOD;
    }
    return rez;
}

ll aranj(int n, int k)
{
    return (fact[n] * ifact[n - k]) % MOD;
}

int main()
{
    int n, m; cin >> n >> m;
    for (int i = 1; i <= 2 * n; i++)
    {
        c[i][i] = c[i][0] = 1;
        for (int j = 1; j < i; j++)
        {
            c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
        }
    }


    fact[0] = 1;
    for (int i = 1; i <= 2 * n; i++)
    {
        fact[i] = (fact[i - 1] * i) % MOD;
    }


    ifact[2 * n] = imod(fact[2 * n]);
    for (int i = 2 * n - 1; i >= 0; i--)
    {
        ifact[i] = (ifact[i + 1] * (1ll * i + 1)) % MOD;
    }

    dp[0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        dp[0][i] = 0;
    }

    for (int i = 1; i <= m; i++)
    {
        int ni = 2 * n / m + ((2 * n) % m >= i);

        for (int j = 0; j <= n; j++)
        {
            // calc dp[i][j]
            for (int k = 0; k <= min(ni / 2, j); k++)
            {
                dp[i][j] += ((dp[i - 1][j - k] * c[ni][k]) % MOD) * aranj(ni-k , k) % MOD;
                if (dp[i][j] >= MOD) dp[i][j] -= MOD;
            }
//            cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
        }
    }

    ll bad = 0;
    ll p2 = 1;
    for (int i = 1; i <= n; i++)
    {
        p2 <<= 1;
        if (p2 >= MOD) p2 -= MOD;
        if (i % 2 == 0)
        {
            bad -= ((c[n][i] * fact[2 * n - 2 * i] % MOD) * dp[m][i]) % MOD * (fact[i] % MOD) % MOD;
            if (bad < 0) bad += MOD;
        }
        else {
            bad += ((c[n][i] * fact[2 * n - 2 * i] % MOD) * dp[m][i]) % MOD * (fact[i] % MOD) % MOD;
            if (bad >= MOD) bad -= MOD;
        }
    }

    ll ans = fact[2 * n];
    
    ans -= bad;
    if (ans < 0) ans += MOD;

    cout << ans << '\n';
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 380 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 380 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 2132 KB Output is correct
21 Correct 1 ms 2260 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 4 ms 4920 KB Output is correct
25 Correct 4 ms 4820 KB Output is correct
26 Correct 2 ms 3412 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 380 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 2132 KB Output is correct
21 Correct 1 ms 2260 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 4 ms 4920 KB Output is correct
25 Correct 4 ms 4820 KB Output is correct
26 Correct 2 ms 3412 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 9 ms 11604 KB Output is correct
29 Correct 10 ms 15316 KB Output is correct
30 Correct 12 ms 17340 KB Output is correct
31 Correct 7 ms 11860 KB Output is correct
32 Correct 11 ms 16340 KB Output is correct
33 Correct 20 ms 20180 KB Output is correct
34 Correct 23 ms 30240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 380 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 2132 KB Output is correct
21 Correct 1 ms 2260 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 4 ms 4920 KB Output is correct
25 Correct 4 ms 4820 KB Output is correct
26 Correct 2 ms 3412 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 9 ms 11604 KB Output is correct
29 Correct 10 ms 15316 KB Output is correct
30 Correct 12 ms 17340 KB Output is correct
31 Correct 7 ms 11860 KB Output is correct
32 Correct 11 ms 16340 KB Output is correct
33 Correct 20 ms 20180 KB Output is correct
34 Correct 23 ms 30240 KB Output is correct
35 Correct 37 ms 54988 KB Output is correct
36 Correct 70 ms 90572 KB Output is correct
37 Correct 52 ms 53708 KB Output is correct
38 Correct 51 ms 69068 KB Output is correct
39 Correct 86 ms 75596 KB Output is correct
40 Correct 96 ms 109312 KB Output is correct