Submission #781508

# Submission time Handle Problem Language Result Execution time Memory
781508 2023-07-13T07:12:45 Z 이동현(#10013) None (JOI16_solitaire) C++17
100 / 100
307 ms 165008 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

const int mod = (int)1e9 + 7;
int dp[6004][2], pre[6004][2], ndp[6004][2];
int comb[6004][6004];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    for(int i = 0; i < 6004; ++i){
        comb[i][0] = 1;
        for(int j = 1; j <= i; ++j){
            comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod;
        }
    }

    int n;
    cin >> n;
    vector<string> a(3);
    for(int i = 0; i < 3; ++i){
        cin >> a[i];
    }
    if(a[0][0] == 'x' || a[0][n - 1] == 'x' || a[2][0] == 'x' || a[2][n - 1] == 'x'){
        cout << "0\n";
        return 0;
    }
    for(int i = 0; i + 1 < n; ++i){
        if((a[0][i] == 'x' && a[0][i + 1] == 'x') || (a[2][i] == 'x' && a[2][i + 1] == 'x')){
            cout << "0\n";
            return 0;
        }
    }
    
    vector<int> cnt(n);
    for(int i = 0; i < n; ++i){
        cnt[i] = (a[0][i] == 'x') + (a[2][i] == 'x');
    }

    auto merge = [&](int x, int y){
        return comb[x + y][x];
    };

    vector<vector<int>> vc;
    int fact[4] = {1, 1, 2, 6};
    int gop[3] = {1, 1, 2};
    for(int i = 0; i < n; ++i){
        if(a[1][i] == 'o'){
            if(a[0][i] == 'x'){
                vc.push_back({1, 1});
            }
            if(a[2][i] == 'x'){
                vc.push_back({1, 1});
            }
            continue;
        }

        memset(dp, 0, sizeof(dp));
        memset(ndp, 0, sizeof(ndp));
        if(i){
            dp[0][0] = fact[cnt[i]];
            if(cnt[i] >= 1) dp[1][0] = cnt[i];
            if(cnt[i] >= 2) dp[2][0] = 2;
            dp[cnt[i]][1] = max(1ll, cnt[i]);
        }
        else{
            dp[cnt[i]][0] = dp[cnt[i]][1] = max(1ll, cnt[i]);
        }

        auto get = [&](int p, int l, int r){
            int rv = pre[r][p];
            if(l){
                rv = (rv - pre[l - 1][p] + mod) % mod;
            }
            return rv;
        };

        int j = i + 1;
        int sum = cnt[i] + 1;
        while(j < n && a[1][j] == 'x'){
            for(int x = 0; x < 6004; ++x){
                pre[x][0] = (dp[x][0] + (x ? pre[x - 1][0] : 0)) % mod;
                pre[x][1] = (dp[x][1] + (x ? pre[x - 1][1] : 0)) % mod;
            }

            int psum = sum;
            sum += cnt[j] + 1;
            for(int x = cnt[j]; x < sum; ++x){
                (ndp[x][0] += get(0, x - cnt[j], psum - 1) * merge(cnt[j], x - cnt[j]) % mod * gop[cnt[j]] % mod) %= mod;
            }
            for(int x = cnt[j] + 1; x < sum; ++x){
                (ndp[x][0] += get(1, 0, x - cnt[j] - 1) * merge(cnt[j], x - cnt[j]) % mod * gop[cnt[j]] % mod) %= mod;
            }
            for(int x = 0; x < sum; ++x){
                ndp[x][1] = ndp[x][0];
            }
            if(cnt[j]){
                for(int x = cnt[j]; x < sum; ++x){
                    (ndp[x][0] += get(1, 0, x - cnt[j]) * merge(cnt[j] - 1, x - cnt[j] + 1) % mod * merge(1, psum - (x - cnt[j] + 1)) % mod * gop[cnt[j]] % mod) %= mod;
                }
            }
            if(cnt[j] > 1){
                for(int x = cnt[j] - 1; x < sum; ++x){
                    (ndp[x][0] += get(1, 0, x - cnt[j] + 1) * merge(2, psum - (x - cnt[j] + 2)) % mod * gop[cnt[j]] % mod) %= mod;
                }
            }
            for(int x = 0; x < 6004; ++x){
                dp[x][0] = ndp[x][0];
                dp[x][1] = ndp[x][1];
                ndp[x][0] = ndp[x][1] = 0;
            }
            ++j;
        }

        --j;
        int nans = 0;
        if(j == n - 1){
            for(int k = 0; k < sum; ++k){
                (nans += dp[k][1]) %= mod;
            }
        }
        else{
            for(int k = 0; k < sum; ++k){
                (nans += dp[k][0]) %= mod;
            }
        }
        vc.push_back({nans, sum});
        i = j;
    }

    int ans = 1, acnt = 0;
    for(auto&i:vc){
        // cout << i[0] << ' ' << i[1] << endl;
        ans = ans * i[0] % mod * merge(acnt, i[1]) % mod;
        acnt += i[1];
    }

    cout << ans << '\n';
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 71 ms 164792 KB Output is correct
2 Correct 69 ms 164820 KB Output is correct
3 Correct 85 ms 164680 KB Output is correct
4 Correct 78 ms 164788 KB Output is correct
5 Correct 72 ms 164832 KB Output is correct
6 Correct 70 ms 164764 KB Output is correct
7 Correct 70 ms 164768 KB Output is correct
8 Correct 82 ms 164748 KB Output is correct
9 Correct 73 ms 164844 KB Output is correct
10 Correct 71 ms 164860 KB Output is correct
11 Correct 70 ms 164760 KB Output is correct
12 Correct 70 ms 164772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 164860 KB Output is correct
2 Correct 87 ms 164816 KB Output is correct
3 Correct 75 ms 164812 KB Output is correct
4 Correct 119 ms 164900 KB Output is correct
5 Correct 101 ms 164920 KB Output is correct
6 Correct 95 ms 164832 KB Output is correct
7 Correct 83 ms 164912 KB Output is correct
8 Correct 109 ms 164928 KB Output is correct
9 Correct 108 ms 164836 KB Output is correct
10 Correct 96 ms 164880 KB Output is correct
11 Correct 88 ms 164860 KB Output is correct
12 Correct 107 ms 164816 KB Output is correct
13 Correct 159 ms 164888 KB Output is correct
14 Correct 151 ms 164832 KB Output is correct
15 Correct 152 ms 164768 KB Output is correct
16 Correct 152 ms 164888 KB Output is correct
17 Correct 158 ms 164884 KB Output is correct
18 Correct 167 ms 164848 KB Output is correct
19 Correct 155 ms 164888 KB Output is correct
20 Correct 156 ms 164884 KB Output is correct
21 Correct 156 ms 164884 KB Output is correct
22 Correct 154 ms 164784 KB Output is correct
23 Correct 161 ms 164836 KB Output is correct
24 Correct 155 ms 164860 KB Output is correct
25 Correct 158 ms 164856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 164764 KB Output is correct
2 Correct 71 ms 164728 KB Output is correct
3 Correct 72 ms 164760 KB Output is correct
4 Correct 70 ms 164816 KB Output is correct
5 Correct 70 ms 164796 KB Output is correct
6 Correct 78 ms 164892 KB Output is correct
7 Correct 71 ms 164780 KB Output is correct
8 Correct 79 ms 164796 KB Output is correct
9 Correct 80 ms 164852 KB Output is correct
10 Correct 71 ms 164808 KB Output is correct
11 Correct 76 ms 164848 KB Output is correct
12 Correct 71 ms 164804 KB Output is correct
13 Correct 71 ms 164748 KB Output is correct
14 Correct 72 ms 164788 KB Output is correct
15 Correct 76 ms 164784 KB Output is correct
16 Correct 71 ms 164800 KB Output is correct
17 Correct 71 ms 164744 KB Output is correct
18 Correct 71 ms 164752 KB Output is correct
19 Correct 71 ms 164760 KB Output is correct
20 Correct 73 ms 164812 KB Output is correct
21 Correct 71 ms 164800 KB Output is correct
22 Correct 72 ms 164756 KB Output is correct
23 Correct 82 ms 164784 KB Output is correct
24 Correct 82 ms 164860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 164792 KB Output is correct
2 Correct 69 ms 164820 KB Output is correct
3 Correct 85 ms 164680 KB Output is correct
4 Correct 78 ms 164788 KB Output is correct
5 Correct 72 ms 164832 KB Output is correct
6 Correct 70 ms 164764 KB Output is correct
7 Correct 70 ms 164768 KB Output is correct
8 Correct 82 ms 164748 KB Output is correct
9 Correct 73 ms 164844 KB Output is correct
10 Correct 71 ms 164860 KB Output is correct
11 Correct 70 ms 164760 KB Output is correct
12 Correct 70 ms 164772 KB Output is correct
13 Correct 71 ms 164764 KB Output is correct
14 Correct 71 ms 164728 KB Output is correct
15 Correct 72 ms 164760 KB Output is correct
16 Correct 70 ms 164816 KB Output is correct
17 Correct 70 ms 164796 KB Output is correct
18 Correct 78 ms 164892 KB Output is correct
19 Correct 71 ms 164780 KB Output is correct
20 Correct 79 ms 164796 KB Output is correct
21 Correct 80 ms 164852 KB Output is correct
22 Correct 71 ms 164808 KB Output is correct
23 Correct 76 ms 164848 KB Output is correct
24 Correct 71 ms 164804 KB Output is correct
25 Correct 71 ms 164748 KB Output is correct
26 Correct 72 ms 164788 KB Output is correct
27 Correct 76 ms 164784 KB Output is correct
28 Correct 71 ms 164800 KB Output is correct
29 Correct 71 ms 164744 KB Output is correct
30 Correct 71 ms 164752 KB Output is correct
31 Correct 71 ms 164760 KB Output is correct
32 Correct 73 ms 164812 KB Output is correct
33 Correct 71 ms 164800 KB Output is correct
34 Correct 72 ms 164756 KB Output is correct
35 Correct 82 ms 164784 KB Output is correct
36 Correct 82 ms 164860 KB Output is correct
37 Correct 72 ms 164848 KB Output is correct
38 Correct 72 ms 164820 KB Output is correct
39 Correct 72 ms 164744 KB Output is correct
40 Correct 75 ms 164844 KB Output is correct
41 Correct 72 ms 164868 KB Output is correct
42 Correct 74 ms 164764 KB Output is correct
43 Correct 74 ms 164820 KB Output is correct
44 Correct 69 ms 164804 KB Output is correct
45 Correct 75 ms 164788 KB Output is correct
46 Correct 76 ms 164804 KB Output is correct
47 Correct 74 ms 164840 KB Output is correct
48 Correct 77 ms 164760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 164792 KB Output is correct
2 Correct 69 ms 164820 KB Output is correct
3 Correct 85 ms 164680 KB Output is correct
4 Correct 78 ms 164788 KB Output is correct
5 Correct 72 ms 164832 KB Output is correct
6 Correct 70 ms 164764 KB Output is correct
7 Correct 70 ms 164768 KB Output is correct
8 Correct 82 ms 164748 KB Output is correct
9 Correct 73 ms 164844 KB Output is correct
10 Correct 71 ms 164860 KB Output is correct
11 Correct 70 ms 164760 KB Output is correct
12 Correct 70 ms 164772 KB Output is correct
13 Correct 87 ms 164860 KB Output is correct
14 Correct 87 ms 164816 KB Output is correct
15 Correct 75 ms 164812 KB Output is correct
16 Correct 119 ms 164900 KB Output is correct
17 Correct 101 ms 164920 KB Output is correct
18 Correct 95 ms 164832 KB Output is correct
19 Correct 83 ms 164912 KB Output is correct
20 Correct 109 ms 164928 KB Output is correct
21 Correct 108 ms 164836 KB Output is correct
22 Correct 96 ms 164880 KB Output is correct
23 Correct 88 ms 164860 KB Output is correct
24 Correct 107 ms 164816 KB Output is correct
25 Correct 159 ms 164888 KB Output is correct
26 Correct 151 ms 164832 KB Output is correct
27 Correct 152 ms 164768 KB Output is correct
28 Correct 152 ms 164888 KB Output is correct
29 Correct 158 ms 164884 KB Output is correct
30 Correct 167 ms 164848 KB Output is correct
31 Correct 155 ms 164888 KB Output is correct
32 Correct 156 ms 164884 KB Output is correct
33 Correct 156 ms 164884 KB Output is correct
34 Correct 154 ms 164784 KB Output is correct
35 Correct 161 ms 164836 KB Output is correct
36 Correct 155 ms 164860 KB Output is correct
37 Correct 158 ms 164856 KB Output is correct
38 Correct 71 ms 164764 KB Output is correct
39 Correct 71 ms 164728 KB Output is correct
40 Correct 72 ms 164760 KB Output is correct
41 Correct 70 ms 164816 KB Output is correct
42 Correct 70 ms 164796 KB Output is correct
43 Correct 78 ms 164892 KB Output is correct
44 Correct 71 ms 164780 KB Output is correct
45 Correct 79 ms 164796 KB Output is correct
46 Correct 80 ms 164852 KB Output is correct
47 Correct 71 ms 164808 KB Output is correct
48 Correct 76 ms 164848 KB Output is correct
49 Correct 71 ms 164804 KB Output is correct
50 Correct 71 ms 164748 KB Output is correct
51 Correct 72 ms 164788 KB Output is correct
52 Correct 76 ms 164784 KB Output is correct
53 Correct 71 ms 164800 KB Output is correct
54 Correct 71 ms 164744 KB Output is correct
55 Correct 71 ms 164752 KB Output is correct
56 Correct 71 ms 164760 KB Output is correct
57 Correct 73 ms 164812 KB Output is correct
58 Correct 71 ms 164800 KB Output is correct
59 Correct 72 ms 164756 KB Output is correct
60 Correct 82 ms 164784 KB Output is correct
61 Correct 82 ms 164860 KB Output is correct
62 Correct 72 ms 164848 KB Output is correct
63 Correct 72 ms 164820 KB Output is correct
64 Correct 72 ms 164744 KB Output is correct
65 Correct 75 ms 164844 KB Output is correct
66 Correct 72 ms 164868 KB Output is correct
67 Correct 74 ms 164764 KB Output is correct
68 Correct 74 ms 164820 KB Output is correct
69 Correct 69 ms 164804 KB Output is correct
70 Correct 75 ms 164788 KB Output is correct
71 Correct 76 ms 164804 KB Output is correct
72 Correct 74 ms 164840 KB Output is correct
73 Correct 77 ms 164760 KB Output is correct
74 Correct 92 ms 164832 KB Output is correct
75 Correct 84 ms 164812 KB Output is correct
76 Correct 85 ms 164816 KB Output is correct
77 Correct 109 ms 164936 KB Output is correct
78 Correct 106 ms 164852 KB Output is correct
79 Correct 86 ms 164872 KB Output is correct
80 Correct 103 ms 164880 KB Output is correct
81 Correct 104 ms 164940 KB Output is correct
82 Correct 103 ms 164932 KB Output is correct
83 Correct 89 ms 164940 KB Output is correct
84 Correct 82 ms 164856 KB Output is correct
85 Correct 97 ms 164912 KB Output is correct
86 Correct 277 ms 164812 KB Output is correct
87 Correct 305 ms 164880 KB Output is correct
88 Correct 271 ms 164784 KB Output is correct
89 Correct 275 ms 164888 KB Output is correct
90 Correct 307 ms 164768 KB Output is correct
91 Correct 277 ms 164884 KB Output is correct
92 Correct 295 ms 165008 KB Output is correct
93 Correct 281 ms 164888 KB Output is correct
94 Correct 299 ms 164848 KB Output is correct
95 Correct 274 ms 164888 KB Output is correct
96 Correct 272 ms 164812 KB Output is correct
97 Correct 306 ms 164884 KB Output is correct