Submission #754711

# Submission time Handle Problem Language Result Execution time Memory
754711 2023-06-08T12:21:29 Z Lucpp Festivals in JOI Kingdom 2 (JOI23_festival2) C++17
51 / 100
527 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
ll mod;

ll power(ll a, ll b){
    ll y = 1;
    while(b){
        if(b & 1) y = y * a % mod;
        b /= 2;
        a = (a*a)%mod;
    }
    return y;
}

ll inv(ll x){
    return power(x, mod-2);
}

struct State{
    ll a[2][2][2] = {};
};

void add(ll &a, ll x){a = (a+x)%mod;}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie();
    cin >> n >> mod;
    vector<ll> fact(2*n+1), inv_fact(2*n+1);
    fact[0] = 1;
    for(int i = 1; i <= 2*n; i++) fact[i] = (fact[i-1]*i) % mod;
    for(int i = 0; i <= 2*n; i++) inv_fact[i] = inv(fact[i]);

    vector<vector<State>> dp(2*n+1, vector<State>(n+1));

    dp[0][0].a[0][0][0] = 1;

    for(int i = 1; i <= 2*n; i++){
        for(int k = 0; k <= min(i, n); k++){
            for(int v = 0; v < 2; v++){
                for(int s = 0; s < 2; s++){
                    for(int g = 0; g <= s; g++){
                        ll &res = dp[i][k].a[v][s][g];
                        add(res, dp[i-1][k+1].a[v][1][g|(s==0)]);
                        if(s == 1 && g == 1 && v == 0 && k > 0){
                            ll factor = fact[i-1] * inv_fact[i-1-(k-1)] % mod;
                            add(res, dp[i-k][0].a[0][0][0] * factor);
                        }
                        if(s == 1 && g == 0){
                            add(res, dp[i-1][k].a[0][0][0]);
                        }
                        if(k > g && v == 0){
                            ll factor = (k-g) * fact[i-1] % mod * inv_fact[i-1-(k-g-1)] % mod;
                            add(res, dp[i-(k-g)][0].a[1][s][0] * factor);
                        }
                    }
                }
            }
        }
    }
    ll same = dp[2*n][0].a[0][0][0];
    ll total = 1;
    for(int i = 0; i < n; i++) total = total * (2*i+1) % mod;
    // cerr << same << "\n";
    cout << (total - same + mod) % mod << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 21 ms 11604 KB Output is correct
20 Correct 20 ms 11376 KB Output is correct
21 Correct 22 ms 11348 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 3 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 21 ms 11604 KB Output is correct
20 Correct 20 ms 11376 KB Output is correct
21 Correct 22 ms 11348 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 3 ms 1492 KB Output is correct
24 Runtime error 527 ms 1048576 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 21 ms 11604 KB Output is correct
20 Correct 20 ms 11376 KB Output is correct
21 Correct 22 ms 11348 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 3 ms 1492 KB Output is correct
24 Runtime error 527 ms 1048576 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -