Submission #126572

# Submission time Handle Problem Language Result Execution time Memory
126572 2019-07-08T06:07:09 Z choikiwon None (JOI16_solitaire) C++17
80 / 100
239 ms 262148 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(int t, int r, int pre);

vector<vector<int> > cc3[3][3];
int dp3(int a, int b, int r, int pre) {
    int &ret = cc3[a][b][r][pre];
    if(ret != -1) return ret;

    int tr = L + r;
    int rem = calc(L, tr - 1);

    if(rem < pre) return ret = 0;

    ret = 0;
    if(pre < rem) {
        ret += dp3(a, b, r, pre + 1);
        ret %= mod;
    }
    ret += 1LL * dp1(1, r - 1, pre) * fact[a] % mod * fact[b] % mod * H(pre + 1, a) % mod * H(rem - pre + 1, b) % mod;
    ret %= mod;
    return ret;
}

vector<vector<int> > cc2[3][3];
int dp2(int a, int b, int r, int pre) {
    if(pre < 0) return 0;
    int &ret = cc2[a][b][r][pre];
    if(ret != -1) return ret;

    int tr = L + r;
    int rem = calc(L, tr - 1);

    ret = 0;
    if(pre) {
        ret += dp2(a, b, r, pre - 1);
        ret %= mod;
    }
    ret += 1LL * dp1(0, r - 1, pre) * fact[a] % mod * fact[b] % mod * H(pre + 1, a) % mod * H(rem - pre + 1, b) % mod;
    ret %= mod;

    return ret;
}

vector<vector<int> > cc1[2];
int dp1(int t, int r, int pre) {
    if(r == -1) {
        if(L) return pre == 0;
        else return pre == 0 && t == 0;
    }
    int &ret = cc1[t][r][pre];
    if(ret != -1) return ret;

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

    ret = 0;
    if(t) {
        ret += dp2(cnt, 0, r, min(rem, pre - 1 - cnt)) % mod;
        ret %= mod;
        return ret;
    }
    else {
        ret += dp2(cnt, 0, r, rem);
        ret %= mod;
        for(int i = 0; i < cnt; i++) {
            ret += 1LL * comb(cnt, i) * dp3(i, cnt - i, r, max(0, pre - i)) % mod;
            ret %= mod;
        }
        return ret;
    }
}

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';
        }
    }

    cc1[0] = cc1[1] = vector<vector<int> >(R - L + 1, vector<int>(tot + 1, -1));

    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            cc2[i][j] = cc3[i][j] = vector<vector<int> >(R - L + 1, vector<int>(tot + 1, -1));
        }
    }

    int ret;
    if(r == N - 1) ret = dp1(0, R - L, tot);
    else ret = dp1(0, R - L, 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:208:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ind.size(); i++) {
                    ~~^~~~~~~~~~~~
solitaire.cpp:211:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < prob.size(); i++) {
                    ~~^~~~~~~~~~~~~
solitaire.cpp:149:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
solitaire.cpp:153:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n");
         ~~~~~^~~~~~
solitaire.cpp:155:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%c", &G[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 4 ms 376 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 5 ms 380 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 33 ms 13404 KB Output is correct
14 Correct 47 ms 20600 KB Output is correct
15 Correct 67 ms 52452 KB Output is correct
16 Correct 36 ms 18296 KB Output is correct
17 Correct 66 ms 38532 KB Output is correct
18 Correct 48 ms 25708 KB Output is correct
19 Correct 61 ms 42488 KB Output is correct
20 Correct 68 ms 49468 KB Output is correct
21 Correct 64 ms 39848 KB Output is correct
22 Correct 83 ms 59100 KB Output is correct
23 Correct 69 ms 30116 KB Output is correct
24 Correct 68 ms 38904 KB Output is correct
25 Correct 65 ms 38520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 424 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 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 504 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 4 ms 508 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 424 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 504 KB Output is correct
27 Correct 3 ms 504 KB Output is correct
28 Correct 3 ms 504 KB Output is correct
29 Correct 3 ms 632 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 4 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 4 ms 632 KB Output is correct
34 Correct 4 ms 508 KB Output is correct
35 Correct 3 ms 504 KB Output is correct
36 Correct 3 ms 508 KB Output is correct
37 Correct 3 ms 376 KB Output is correct
38 Correct 3 ms 376 KB Output is correct
39 Correct 4 ms 376 KB Output is correct
40 Correct 4 ms 504 KB Output is correct
41 Correct 3 ms 376 KB Output is correct
42 Correct 3 ms 376 KB Output is correct
43 Correct 3 ms 376 KB Output is correct
44 Correct 3 ms 376 KB Output is correct
45 Correct 3 ms 504 KB Output is correct
46 Correct 3 ms 376 KB Output is correct
47 Correct 3 ms 380 KB Output is correct
48 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 4 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 4 ms 376 KB Output is correct
20 Correct 5 ms 504 KB Output is correct
21 Correct 5 ms 504 KB Output is correct
22 Correct 4 ms 460 KB Output is correct
23 Correct 5 ms 380 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 33 ms 13404 KB Output is correct
26 Correct 47 ms 20600 KB Output is correct
27 Correct 67 ms 52452 KB Output is correct
28 Correct 36 ms 18296 KB Output is correct
29 Correct 66 ms 38532 KB Output is correct
30 Correct 48 ms 25708 KB Output is correct
31 Correct 61 ms 42488 KB Output is correct
32 Correct 68 ms 49468 KB Output is correct
33 Correct 64 ms 39848 KB Output is correct
34 Correct 83 ms 59100 KB Output is correct
35 Correct 69 ms 30116 KB Output is correct
36 Correct 68 ms 38904 KB Output is correct
37 Correct 65 ms 38520 KB Output is correct
38 Correct 3 ms 380 KB Output is correct
39 Correct 3 ms 376 KB Output is correct
40 Correct 3 ms 424 KB Output is correct
41 Correct 3 ms 376 KB Output is correct
42 Correct 3 ms 376 KB Output is correct
43 Correct 3 ms 376 KB Output is correct
44 Correct 3 ms 376 KB Output is correct
45 Correct 3 ms 376 KB Output is correct
46 Correct 3 ms 376 KB Output is correct
47 Correct 3 ms 376 KB Output is correct
48 Correct 3 ms 376 KB Output is correct
49 Correct 3 ms 376 KB Output is correct
50 Correct 3 ms 504 KB Output is correct
51 Correct 3 ms 504 KB Output is correct
52 Correct 3 ms 504 KB Output is correct
53 Correct 3 ms 504 KB Output is correct
54 Correct 3 ms 632 KB Output is correct
55 Correct 3 ms 504 KB Output is correct
56 Correct 4 ms 504 KB Output is correct
57 Correct 3 ms 504 KB Output is correct
58 Correct 4 ms 632 KB Output is correct
59 Correct 4 ms 508 KB Output is correct
60 Correct 3 ms 504 KB Output is correct
61 Correct 3 ms 508 KB Output is correct
62 Correct 3 ms 376 KB Output is correct
63 Correct 3 ms 376 KB Output is correct
64 Correct 4 ms 376 KB Output is correct
65 Correct 4 ms 504 KB Output is correct
66 Correct 3 ms 376 KB Output is correct
67 Correct 3 ms 376 KB Output is correct
68 Correct 3 ms 376 KB Output is correct
69 Correct 3 ms 376 KB Output is correct
70 Correct 3 ms 504 KB Output is correct
71 Correct 3 ms 376 KB Output is correct
72 Correct 3 ms 380 KB Output is correct
73 Correct 3 ms 376 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 504 KB Output is correct
77 Correct 6 ms 504 KB Output is correct
78 Correct 6 ms 632 KB Output is correct
79 Correct 5 ms 504 KB Output is correct
80 Correct 5 ms 504 KB Output is correct
81 Correct 5 ms 508 KB Output is correct
82 Correct 6 ms 504 KB Output is correct
83 Correct 5 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 Runtime error 239 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
87 Halted 0 ms 0 KB -