Submission #126570

# Submission time Handle Problem Language Result Execution time Memory
126570 2019-07-08T06:05:16 Z choikiwon None (JOI16_solitaire) C++17
68 / 100
351 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>(MN, -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>(MN, -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 5 ms 2780 KB Output is correct
2 Correct 5 ms 2296 KB Output is correct
3 Correct 4 ms 888 KB Output is correct
4 Correct 5 ms 1884 KB Output is correct
5 Correct 6 ms 2296 KB Output is correct
6 Correct 5 ms 2296 KB Output is correct
7 Correct 5 ms 1912 KB Output is correct
8 Correct 6 ms 3264 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 6 ms 3260 KB Output is correct
11 Correct 6 ms 3320 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 7080 KB Output is correct
2 Correct 71 ms 5224 KB Output is correct
3 Correct 26 ms 3708 KB Output is correct
4 Correct 137 ms 6720 KB Output is correct
5 Correct 89 ms 5720 KB Output is correct
6 Correct 85 ms 5768 KB Output is correct
7 Correct 66 ms 3220 KB Output is correct
8 Correct 138 ms 8756 KB Output is correct
9 Correct 150 ms 9492 KB Output is correct
10 Correct 60 ms 3796 KB Output is correct
11 Correct 87 ms 4728 KB Output is correct
12 Correct 123 ms 8652 KB Output is correct
13 Correct 335 ms 160468 KB Output is correct
14 Correct 351 ms 204128 KB Output is correct
15 Runtime error 348 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 3320 KB Output is correct
3 Correct 7 ms 2872 KB Output is correct
4 Correct 6 ms 1852 KB Output is correct
5 Correct 5 ms 1848 KB Output is correct
6 Correct 6 ms 2812 KB Output is correct
7 Correct 7 ms 3328 KB Output is correct
8 Correct 6 ms 2808 KB Output is correct
9 Correct 5 ms 1912 KB Output is correct
10 Correct 6 ms 2856 KB Output is correct
11 Correct 5 ms 2296 KB Output is correct
12 Correct 6 ms 3192 KB Output is correct
13 Correct 15 ms 14584 KB Output is correct
14 Correct 16 ms 14560 KB Output is correct
15 Correct 15 ms 14584 KB Output is correct
16 Correct 15 ms 14584 KB Output is correct
17 Correct 16 ms 14608 KB Output is correct
18 Correct 15 ms 14584 KB Output is correct
19 Correct 15 ms 14584 KB Output is correct
20 Correct 16 ms 14584 KB Output is correct
21 Correct 15 ms 14584 KB Output is correct
22 Correct 16 ms 14504 KB Output is correct
23 Correct 15 ms 14556 KB Output is correct
24 Correct 16 ms 14592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2780 KB Output is correct
2 Correct 5 ms 2296 KB Output is correct
3 Correct 4 ms 888 KB Output is correct
4 Correct 5 ms 1884 KB Output is correct
5 Correct 6 ms 2296 KB Output is correct
6 Correct 5 ms 2296 KB Output is correct
7 Correct 5 ms 1912 KB Output is correct
8 Correct 6 ms 3264 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 6 ms 3260 KB Output is correct
11 Correct 6 ms 3320 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 6 ms 3320 KB Output is correct
15 Correct 7 ms 2872 KB Output is correct
16 Correct 6 ms 1852 KB Output is correct
17 Correct 5 ms 1848 KB Output is correct
18 Correct 6 ms 2812 KB Output is correct
19 Correct 7 ms 3328 KB Output is correct
20 Correct 6 ms 2808 KB Output is correct
21 Correct 5 ms 1912 KB Output is correct
22 Correct 6 ms 2856 KB Output is correct
23 Correct 5 ms 2296 KB Output is correct
24 Correct 6 ms 3192 KB Output is correct
25 Correct 15 ms 14584 KB Output is correct
26 Correct 16 ms 14560 KB Output is correct
27 Correct 15 ms 14584 KB Output is correct
28 Correct 15 ms 14584 KB Output is correct
29 Correct 16 ms 14608 KB Output is correct
30 Correct 15 ms 14584 KB Output is correct
31 Correct 15 ms 14584 KB Output is correct
32 Correct 16 ms 14584 KB Output is correct
33 Correct 15 ms 14584 KB Output is correct
34 Correct 16 ms 14504 KB Output is correct
35 Correct 15 ms 14556 KB Output is correct
36 Correct 16 ms 14592 KB Output is correct
37 Correct 6 ms 1448 KB Output is correct
38 Correct 10 ms 2328 KB Output is correct
39 Correct 7 ms 3192 KB Output is correct
40 Correct 23 ms 7528 KB Output is correct
41 Correct 12 ms 2352 KB Output is correct
42 Correct 13 ms 2848 KB Output is correct
43 Correct 21 ms 4612 KB Output is correct
44 Correct 14 ms 2388 KB Output is correct
45 Correct 25 ms 6624 KB Output is correct
46 Correct 21 ms 5144 KB Output is correct
47 Correct 17 ms 4664 KB Output is correct
48 Correct 28 ms 4788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2780 KB Output is correct
2 Correct 5 ms 2296 KB Output is correct
3 Correct 4 ms 888 KB Output is correct
4 Correct 5 ms 1884 KB Output is correct
5 Correct 6 ms 2296 KB Output is correct
6 Correct 5 ms 2296 KB Output is correct
7 Correct 5 ms 1912 KB Output is correct
8 Correct 6 ms 3264 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 6 ms 3260 KB Output is correct
11 Correct 6 ms 3320 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
13 Correct 58 ms 7080 KB Output is correct
14 Correct 71 ms 5224 KB Output is correct
15 Correct 26 ms 3708 KB Output is correct
16 Correct 137 ms 6720 KB Output is correct
17 Correct 89 ms 5720 KB Output is correct
18 Correct 85 ms 5768 KB Output is correct
19 Correct 66 ms 3220 KB Output is correct
20 Correct 138 ms 8756 KB Output is correct
21 Correct 150 ms 9492 KB Output is correct
22 Correct 60 ms 3796 KB Output is correct
23 Correct 87 ms 4728 KB Output is correct
24 Correct 123 ms 8652 KB Output is correct
25 Correct 335 ms 160468 KB Output is correct
26 Correct 351 ms 204128 KB Output is correct
27 Runtime error 348 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
28 Halted 0 ms 0 KB -