Submission #754712

# Submission time Handle Problem Language Result Execution time Memory
754712 2023-06-08T12:26:54 Z Lucpp Festivals in JOI Kingdom 2 (JOI23_festival2) C++17
87 / 100
1833 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{
    int a[2][2][2] = {};
};

void add(int &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++){
                        int &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 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 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 316 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 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 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 316 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 212 KB Output is correct
19 Correct 18 ms 5972 KB Output is correct
20 Correct 17 ms 5844 KB Output is correct
21 Correct 19 ms 5816 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 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 316 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 212 KB Output is correct
19 Correct 18 ms 5972 KB Output is correct
20 Correct 17 ms 5844 KB Output is correct
21 Correct 19 ms 5816 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 856 KB Output is correct
24 Correct 1821 ms 564616 KB Output is correct
25 Correct 1833 ms 561996 KB Output is correct
26 Correct 1804 ms 561996 KB Output is correct
27 Correct 23 ms 6996 KB Output is correct
28 Correct 304 ms 95796 KB Output is correct
29 Correct 257 ms 78280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 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 316 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 212 KB Output is correct
19 Correct 18 ms 5972 KB Output is correct
20 Correct 17 ms 5844 KB Output is correct
21 Correct 19 ms 5816 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 856 KB Output is correct
24 Correct 1821 ms 564616 KB Output is correct
25 Correct 1833 ms 561996 KB Output is correct
26 Correct 1804 ms 561996 KB Output is correct
27 Correct 23 ms 6996 KB Output is correct
28 Correct 304 ms 95796 KB Output is correct
29 Correct 257 ms 78280 KB Output is correct
30 Runtime error 381 ms 1048576 KB Execution killed with signal 9
31 Halted 0 ms 0 KB -