답안 #126575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
126575 2019-07-08T06:19:32 Z choikiwon Solitaire (JOI16_solitaire) C++17
100 / 100
1468 ms 1328 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int mod = 1e9 + 7;
const int MN = 6010;

int exp(int x, int n) {
    int ret = 1;
    while(n) {
        if(n & 1) ret = 1LL * ret * x % mod;
        x = 1LL * x * x % mod;
        n >>= 1;
    }
    return ret;
}
int inv(int x) {
    return exp(x, mod - 2);
}
int fact[MN], invf[MN];
int comb(int n, int k) {
    if(n < k) return 0;
    return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod;
}
int H(int n, int k) {
    if(k == 0) return 1;
    return comb(n + k - 1, k);
}

int N, L, R;
char G[3][MN];
int psum[MN];

int calc(int l, int r) {
    if(l > r) return 0;
    return psum[r] - (l? psum[l - 1] : 0);
}

int dp1[2][2][MN], dp2[3][3][2][MN], dp3[3][3][2][MN];

int proc(int l, int r) {
    L = l;
    R = r;

    int tot = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = L; j <= R; j++) {
            tot += G[i][j] == 'x';
        }
    }

    for(int i = 0; i < 2; i++) {
        dp1[i][1][0] = L? 1 : i == 0;
    }
    for(int r = 0; r <= R - L; r++) {
        int c = r & 1;

        int tr = L + r;
        int rem = calc(L, tr - 1);
        int cnt = (G[0][tr] == 'x') + (G[2][tr] == 'x');

        for(int a = 0; a < 3; a++) {
            for(int b = 0; b < 3; b++) {
                for(int pre = 0; pre <= rem; pre++) {
                    int &ret = dp2[a][b][c][pre];
                    ret = 0;
                    if(pre) {
                        ret += dp2[a][b][c][pre - 1];
                        ret %= mod;
                    }
                    ret += 1LL * dp1[0][c ^ 1][pre] * fact[a] % mod * fact[b] % mod * H(pre + 1, a) % mod * H(rem - pre + 1, b) % mod;
                    ret %= mod;
                }
                for(int pre = rem; pre >= 0; pre--) {
                    int &ret = dp3[a][b][c][pre];
                    ret = 0;
                    if(pre < rem) {
                        ret += dp3[a][b][c][pre + 1];
                        ret %= mod;
                    }
                    ret += 1LL * dp1[1][c ^ 1][pre] * fact[a] % mod * fact[b] % mod * H(pre + 1, a) % mod * H(rem - pre + 1, b) % mod;
                    ret %= mod;
                }
            }
        }
        for(int t = 0; t < 2; t++) {
            for(int pre = 0; pre <= calc(L, tr); pre++) {
                int &ret = dp1[t][c][pre];
                ret = 0;
                if(t) {
                    if(pre - 1 - cnt >= 0) {
                        ret += dp2[cnt][0][c][ min(rem, pre - 1 - cnt) ] % mod;
                        ret %= mod;
                    }
                }
                else {
                    ret += dp2[cnt][0][c][rem];
                    ret %= mod;
                    for(int i = 0; i < cnt; i++) {
                        if(pre - i <= rem) {
                            ret += 1LL * comb(cnt, i) * dp3[i][cnt - i][c][ max(0, pre - i) ] % mod;
                            ret %= mod;
                        }
                    }
                }
            }
        }
    }

    int ret;
    if(r == N - 1) ret = dp1[0][(R - L) & 1][tot];
    else ret = dp1[0][(R - L) & 1][0];

    //cout << l << ' ' << r << ' ' << ret << endl;

    return ret;
}

int main() {
    fact[0] = 1;
    for(int i = 1; i < MN; i++) {
        fact[i] = 1LL * fact[i - 1] * i % mod;
    }
    for(int i = 0; i < MN; i++) {
        invf[i] = inv(fact[i]);
    }

    scanf("%d", &N);

    int sum = 0;
    for(int i = 0; i < 3; i++) {
        scanf("\n");
        for(int j = 0; j < N; j++) {
            scanf("%c", &G[i][j]);
            if(G[i][j] == 'x') sum++;
        }
    }
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < 3; j++) {
            psum[i] += G[j][i] == 'x';
        }
        if(i) psum[i] += psum[i - 1];
    }

    if(G[0][0] == 'x' || G[0][N - 1] == 'x' || G[2][0] == 'x' || G[2][N - 1] == 'x') {
        printf("0");
        return 0;
    }

    for(int i = 0; i < N - 1; i++) {
        if(G[0][i] == 'x' && G[0][i + 1] == 'x') {
            printf("0");
            return 0;
        }
        if(G[2][i] == 'x' && G[2][i + 1] == 'x') {
            printf("0");
            return 0;
        }
    }

    vector<int> ind;
    vector<pii> prob;
    int s = 0;
    for(int i = 0; i < N; i++) {
        if(G[1][i] == 'o') {
            if(s < i) {
                int cnt = 0;
                for(int j = s; j < i; j++) {
                    for(int k = 0; k < 3; k++) cnt += G[k][j] == 'x';
                }
                ind.push_back(cnt);
                prob.push_back(pii(s, i - 1));
            }
            s = i + 1;
        }
    }
    if(s < N) {
        int cnt = 0;
        for(int j = s; j < N; j++) {
            for(int k = 0; k < 3; k++) cnt += G[k][j] == 'x';
        }
        ind.push_back(cnt);
        prob.push_back(pii(s, N - 1));
    }

    int ans = fact[sum];
    for(int i = 0; i < ind.size(); i++) {
        ans = 1LL * ans * invf[ ind[i] ] % mod;
    }
    for(int i = 0; i < prob.size(); i++) {
        ans = 1LL * ans * proc(prob[i].first, prob[i].second) % mod;
    }
    printf("%d", ans);
}

Compilation message

solitaire.cpp: In function 'int main()':
solitaire.cpp:188:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ind.size(); i++) {
                    ~~^~~~~~~~~~~~
solitaire.cpp:191:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < prob.size(); i++) {
                    ~~^~~~~~~~~~~~~
solitaire.cpp:129:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
solitaire.cpp:133:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n");
         ~~~~~^~~~~~
solitaire.cpp:135:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%c", &G[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 5 ms 632 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 5 ms 632 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 6 ms 632 KB Output is correct
9 Correct 6 ms 632 KB Output is correct
10 Correct 4 ms 760 KB Output is correct
11 Correct 4 ms 632 KB Output is correct
12 Correct 6 ms 604 KB Output is correct
13 Correct 93 ms 680 KB Output is correct
14 Correct 136 ms 760 KB Output is correct
15 Correct 148 ms 632 KB Output is correct
16 Correct 97 ms 668 KB Output is correct
17 Correct 156 ms 732 KB Output is correct
18 Correct 121 ms 632 KB Output is correct
19 Correct 138 ms 816 KB Output is correct
20 Correct 148 ms 888 KB Output is correct
21 Correct 148 ms 700 KB Output is correct
22 Correct 183 ms 632 KB Output is correct
23 Correct 155 ms 760 KB Output is correct
24 Correct 167 ms 696 KB Output is correct
25 Correct 169 ms 796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 508 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 4 ms 504 KB Output is correct
12 Correct 4 ms 504 KB Output is correct
13 Correct 4 ms 632 KB Output is correct
14 Correct 4 ms 632 KB Output is correct
15 Correct 4 ms 632 KB Output is correct
16 Correct 4 ms 504 KB Output is correct
17 Correct 4 ms 632 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 4 ms 632 KB Output is correct
20 Correct 4 ms 604 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 4 ms 504 KB Output is correct
23 Correct 4 ms 504 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 508 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 4 ms 504 KB Output is correct
24 Correct 4 ms 504 KB Output is correct
25 Correct 4 ms 632 KB Output is correct
26 Correct 4 ms 632 KB Output is correct
27 Correct 4 ms 632 KB Output is correct
28 Correct 4 ms 504 KB Output is correct
29 Correct 4 ms 632 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 4 ms 632 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
34 Correct 4 ms 504 KB Output is correct
35 Correct 4 ms 504 KB Output is correct
36 Correct 4 ms 632 KB Output is correct
37 Correct 3 ms 536 KB Output is correct
38 Correct 3 ms 504 KB Output is correct
39 Correct 3 ms 504 KB Output is correct
40 Correct 4 ms 540 KB Output is correct
41 Correct 3 ms 504 KB Output is correct
42 Correct 3 ms 504 KB Output is correct
43 Correct 3 ms 504 KB Output is correct
44 Correct 3 ms 504 KB Output is correct
45 Correct 4 ms 504 KB Output is correct
46 Correct 3 ms 504 KB Output is correct
47 Correct 4 ms 632 KB Output is correct
48 Correct 5 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 5 ms 632 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 5 ms 504 KB Output is correct
17 Correct 6 ms 632 KB Output is correct
18 Correct 5 ms 632 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 6 ms 632 KB Output is correct
21 Correct 6 ms 632 KB Output is correct
22 Correct 4 ms 760 KB Output is correct
23 Correct 4 ms 632 KB Output is correct
24 Correct 6 ms 604 KB Output is correct
25 Correct 93 ms 680 KB Output is correct
26 Correct 136 ms 760 KB Output is correct
27 Correct 148 ms 632 KB Output is correct
28 Correct 97 ms 668 KB Output is correct
29 Correct 156 ms 732 KB Output is correct
30 Correct 121 ms 632 KB Output is correct
31 Correct 138 ms 816 KB Output is correct
32 Correct 148 ms 888 KB Output is correct
33 Correct 148 ms 700 KB Output is correct
34 Correct 183 ms 632 KB Output is correct
35 Correct 155 ms 760 KB Output is correct
36 Correct 167 ms 696 KB Output is correct
37 Correct 169 ms 796 KB Output is correct
38 Correct 3 ms 504 KB Output is correct
39 Correct 3 ms 504 KB Output is correct
40 Correct 3 ms 508 KB Output is correct
41 Correct 3 ms 504 KB Output is correct
42 Correct 3 ms 504 KB Output is correct
43 Correct 3 ms 504 KB Output is correct
44 Correct 3 ms 504 KB Output is correct
45 Correct 3 ms 504 KB Output is correct
46 Correct 3 ms 504 KB Output is correct
47 Correct 3 ms 504 KB Output is correct
48 Correct 4 ms 504 KB Output is correct
49 Correct 4 ms 504 KB Output is correct
50 Correct 4 ms 632 KB Output is correct
51 Correct 4 ms 632 KB Output is correct
52 Correct 4 ms 632 KB Output is correct
53 Correct 4 ms 504 KB Output is correct
54 Correct 4 ms 632 KB Output is correct
55 Correct 3 ms 504 KB Output is correct
56 Correct 4 ms 632 KB Output is correct
57 Correct 4 ms 604 KB Output is correct
58 Correct 3 ms 604 KB Output is correct
59 Correct 4 ms 504 KB Output is correct
60 Correct 4 ms 504 KB Output is correct
61 Correct 4 ms 632 KB Output is correct
62 Correct 3 ms 536 KB Output is correct
63 Correct 3 ms 504 KB Output is correct
64 Correct 3 ms 504 KB Output is correct
65 Correct 4 ms 540 KB Output is correct
66 Correct 3 ms 504 KB Output is correct
67 Correct 3 ms 504 KB Output is correct
68 Correct 3 ms 504 KB Output is correct
69 Correct 3 ms 504 KB Output is correct
70 Correct 4 ms 504 KB Output is correct
71 Correct 3 ms 504 KB Output is correct
72 Correct 4 ms 632 KB Output is correct
73 Correct 5 ms 604 KB Output is correct
74 Correct 5 ms 504 KB Output is correct
75 Correct 4 ms 504 KB Output is correct
76 Correct 4 ms 508 KB Output is correct
77 Correct 6 ms 504 KB Output is correct
78 Correct 6 ms 632 KB Output is correct
79 Correct 4 ms 504 KB Output is correct
80 Correct 6 ms 504 KB Output is correct
81 Correct 5 ms 632 KB Output is correct
82 Correct 6 ms 504 KB Output is correct
83 Correct 4 ms 504 KB Output is correct
84 Correct 4 ms 504 KB Output is correct
85 Correct 5 ms 504 KB Output is correct
86 Correct 1419 ms 1264 KB Output is correct
87 Correct 1416 ms 1232 KB Output is correct
88 Correct 1402 ms 1272 KB Output is correct
89 Correct 1404 ms 1244 KB Output is correct
90 Correct 1449 ms 1176 KB Output is correct
91 Correct 1426 ms 1204 KB Output is correct
92 Correct 1468 ms 1276 KB Output is correct
93 Correct 1417 ms 1188 KB Output is correct
94 Correct 1418 ms 1128 KB Output is correct
95 Correct 1403 ms 1292 KB Output is correct
96 Correct 1412 ms 1328 KB Output is correct
97 Correct 1401 ms 1148 KB Output is correct