답안 #466367

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
466367 2021-08-19T02:18:35 Z Namnamseo Solitaire (JOI16_solitaire) C++17
100 / 100
373 ms 214868 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()

const int M = int(1e9) + 7;
typedef long long ll;

int n;
char arr[4][2010];

int comb[6001][6001];

typedef pair<int,int> pp;
vector<pp> dummy;

ll dp[2010][6010][2];
ll pdp[2010][6010][2];
int fact[6];

void workGugan(int l,int r){
    int cnt=0;

    // first column
    for(int i=1; i<=3; ++i) cnt+=!arr[i][l];
    if(cnt == 1){
        dp[l][1][0]=1;
    } else if(cnt == 2){
        dp[l][1][1]=1;
        dp[l][2][0]=1;
    } else {
        dp[l][1][1]=2;
        dp[l][2][1]=2;
        dp[l][3][0]=2;
    }

    for(int i=l, j=1; j<=cnt; ++j){
        pdp[i][j][0] = (pdp[i][j-1][0] + dp[i][j][0]) % M;
        pdp[i][j][1] = (pdp[i][j-1][1] + dp[i][j][1]) % M;
    }

    for(int i=l+1; i<=r; ++i){
        int cc=0;
        for(int j=1; j<=3; ++j) cc+=!arr[j][i];
        cnt += cc;

        for(int j=1; j<=cnt; ++j){
            if(j >= cc){
                dp[i][j][0] += comb[j-1][cc-1]*pdp[i-1][cnt-cc][0]%M*fact[cc-1]%M;
                dp[i][j][0] %= M;

                dp[i][j][0] += comb[j-1][cc-1]*(pdp[i-1][cnt-cc][1]-pdp[i-1][j-cc][1]+M)%M*fact[cc-1]%M;
                dp[i][j][0] %= M;
            }

            for(int f=0; f<cc-1; ++f){
                dp[i][j][1] +=
                    comb[j-1][f]
                    * comb[cnt-j][cc-f-1] % M
                    * pdp[i-1][j-1-f][0]  % M
                    * fact[cc-1] % M;
                dp[i][j][1] %= M;
            }

            pdp[i][j][0] = (pdp[i][j-1][0] + dp[i][j][0]) % M;
            pdp[i][j][1] = (pdp[i][j-1][1] + dp[i][j][1]) % M;
        }
    }

    ll ans = pdp[r][cnt][0];
    if(arr[2][r+1])
        ans += pdp[r][cnt][1], ans %= M;

    dummy.pb({ans, cnt});
}

int main()
{
    // input
    cin >> n;
    for(int i=1; i<=3; ++i){
        cin >> (arr[i]+1);
        for(int j=1; j<=n; ++j){
            arr[i][j] = (arr[i][j] == 'o');
        }
    }

    // build comb & fact
    fact[0]=1;
    for(int i=1; i<=5; ++i) fact[i]=i*fact[i-1];
    for(int i=0; i<=6000; ++i){
        comb[i][0] = comb[i][i]=1;
        for(int j=1; j<i; ++j) comb[i][j] = (comb[i-1][j-1]+comb[i-1][j])%M;
    }

    // check unable
    if(!arr[1][1] || !arr[1][n] || !arr[3][1] || !arr[3][n]){
        cout << 0;
        return 0;
    }
    for(int i=1; i<=3; i+=2) for(int j=1; j<n; ++j)
    if(!arr[i][j] && !arr[i][j+1]){
        cout << 0;
        return 0;
    }

    // fill in non-2 columns
    for(int i=1; i<=n; ++i) if(arr[2][i]){
        if(!arr[1][i]) dummy.pb({1, 1});
        if(!arr[3][i]) dummy.pb({1, 1});
    }

    // for each group:
    int last_off = -1;
    for(int i=1; i<=n; ++i){
        if(!arr[2][i]){
            if(last_off == -1) last_off = i;
        } else {
            if(last_off != -1) workGugan(last_off, i-1);
            last_off = -1;
        }
    }
    if(last_off != -1){
        workGugan(last_off, n);
    }

    ll ans=1;
    int sz=0;
    for(pp a:dummy){
        ans=(ans*a.first)%M;
        sz+=a.second;
    }

    for(pp a:dummy){
        ans=(ans*comb[sz][a.second])%M;
        sz-=a.second;
    }

    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 92844 KB Output is correct
2 Correct 66 ms 92820 KB Output is correct
3 Correct 69 ms 92912 KB Output is correct
4 Correct 73 ms 92924 KB Output is correct
5 Correct 67 ms 92912 KB Output is correct
6 Correct 88 ms 92880 KB Output is correct
7 Correct 68 ms 92876 KB Output is correct
8 Correct 88 ms 92888 KB Output is correct
9 Correct 69 ms 92868 KB Output is correct
10 Correct 66 ms 92876 KB Output is correct
11 Correct 81 ms 92860 KB Output is correct
12 Correct 73 ms 92836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 97304 KB Output is correct
2 Correct 71 ms 100548 KB Output is correct
3 Correct 69 ms 95624 KB Output is correct
4 Correct 82 ms 104260 KB Output is correct
5 Correct 72 ms 103112 KB Output is correct
6 Correct 89 ms 102368 KB Output is correct
7 Correct 69 ms 98816 KB Output is correct
8 Correct 73 ms 104388 KB Output is correct
9 Correct 74 ms 104544 KB Output is correct
10 Correct 71 ms 98564 KB Output is correct
11 Correct 71 ms 100872 KB Output is correct
12 Correct 73 ms 104596 KB Output is correct
13 Correct 102 ms 116104 KB Output is correct
14 Correct 89 ms 118968 KB Output is correct
15 Correct 88 ms 120160 KB Output is correct
16 Correct 84 ms 116400 KB Output is correct
17 Correct 111 ms 121032 KB Output is correct
18 Correct 85 ms 118420 KB Output is correct
19 Correct 90 ms 119572 KB Output is correct
20 Correct 89 ms 120388 KB Output is correct
21 Correct 88 ms 120312 KB Output is correct
22 Correct 95 ms 123032 KB Output is correct
23 Correct 94 ms 121052 KB Output is correct
24 Correct 108 ms 121768 KB Output is correct
25 Correct 109 ms 121796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 92964 KB Output is correct
2 Correct 67 ms 92944 KB Output is correct
3 Correct 93 ms 92868 KB Output is correct
4 Correct 67 ms 92868 KB Output is correct
5 Correct 69 ms 92924 KB Output is correct
6 Correct 66 ms 92940 KB Output is correct
7 Correct 67 ms 92880 KB Output is correct
8 Correct 66 ms 92860 KB Output is correct
9 Correct 66 ms 92868 KB Output is correct
10 Correct 65 ms 92984 KB Output is correct
11 Correct 65 ms 92904 KB Output is correct
12 Correct 66 ms 92924 KB Output is correct
13 Correct 67 ms 93072 KB Output is correct
14 Correct 71 ms 93056 KB Output is correct
15 Correct 78 ms 93052 KB Output is correct
16 Correct 68 ms 93040 KB Output is correct
17 Correct 66 ms 93124 KB Output is correct
18 Correct 66 ms 93052 KB Output is correct
19 Correct 70 ms 93124 KB Output is correct
20 Correct 83 ms 93124 KB Output is correct
21 Correct 66 ms 93132 KB Output is correct
22 Correct 66 ms 93108 KB Output is correct
23 Correct 68 ms 93060 KB Output is correct
24 Correct 67 ms 93080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 92844 KB Output is correct
2 Correct 66 ms 92820 KB Output is correct
3 Correct 69 ms 92912 KB Output is correct
4 Correct 73 ms 92924 KB Output is correct
5 Correct 67 ms 92912 KB Output is correct
6 Correct 88 ms 92880 KB Output is correct
7 Correct 68 ms 92876 KB Output is correct
8 Correct 88 ms 92888 KB Output is correct
9 Correct 69 ms 92868 KB Output is correct
10 Correct 66 ms 92876 KB Output is correct
11 Correct 81 ms 92860 KB Output is correct
12 Correct 73 ms 92836 KB Output is correct
13 Correct 74 ms 92964 KB Output is correct
14 Correct 67 ms 92944 KB Output is correct
15 Correct 93 ms 92868 KB Output is correct
16 Correct 67 ms 92868 KB Output is correct
17 Correct 69 ms 92924 KB Output is correct
18 Correct 66 ms 92940 KB Output is correct
19 Correct 67 ms 92880 KB Output is correct
20 Correct 66 ms 92860 KB Output is correct
21 Correct 66 ms 92868 KB Output is correct
22 Correct 65 ms 92984 KB Output is correct
23 Correct 65 ms 92904 KB Output is correct
24 Correct 66 ms 92924 KB Output is correct
25 Correct 67 ms 93072 KB Output is correct
26 Correct 71 ms 93056 KB Output is correct
27 Correct 78 ms 93052 KB Output is correct
28 Correct 68 ms 93040 KB Output is correct
29 Correct 66 ms 93124 KB Output is correct
30 Correct 66 ms 93052 KB Output is correct
31 Correct 70 ms 93124 KB Output is correct
32 Correct 83 ms 93124 KB Output is correct
33 Correct 66 ms 93132 KB Output is correct
34 Correct 66 ms 93108 KB Output is correct
35 Correct 68 ms 93060 KB Output is correct
36 Correct 67 ms 93080 KB Output is correct
37 Correct 66 ms 93004 KB Output is correct
38 Correct 68 ms 93452 KB Output is correct
39 Correct 67 ms 93036 KB Output is correct
40 Correct 83 ms 94476 KB Output is correct
41 Correct 70 ms 93720 KB Output is correct
42 Correct 68 ms 93836 KB Output is correct
43 Correct 67 ms 94280 KB Output is correct
44 Correct 66 ms 93764 KB Output is correct
45 Correct 81 ms 94352 KB Output is correct
46 Correct 85 ms 94020 KB Output is correct
47 Correct 78 ms 94176 KB Output is correct
48 Correct 69 ms 94532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 92844 KB Output is correct
2 Correct 66 ms 92820 KB Output is correct
3 Correct 69 ms 92912 KB Output is correct
4 Correct 73 ms 92924 KB Output is correct
5 Correct 67 ms 92912 KB Output is correct
6 Correct 88 ms 92880 KB Output is correct
7 Correct 68 ms 92876 KB Output is correct
8 Correct 88 ms 92888 KB Output is correct
9 Correct 69 ms 92868 KB Output is correct
10 Correct 66 ms 92876 KB Output is correct
11 Correct 81 ms 92860 KB Output is correct
12 Correct 73 ms 92836 KB Output is correct
13 Correct 69 ms 97304 KB Output is correct
14 Correct 71 ms 100548 KB Output is correct
15 Correct 69 ms 95624 KB Output is correct
16 Correct 82 ms 104260 KB Output is correct
17 Correct 72 ms 103112 KB Output is correct
18 Correct 89 ms 102368 KB Output is correct
19 Correct 69 ms 98816 KB Output is correct
20 Correct 73 ms 104388 KB Output is correct
21 Correct 74 ms 104544 KB Output is correct
22 Correct 71 ms 98564 KB Output is correct
23 Correct 71 ms 100872 KB Output is correct
24 Correct 73 ms 104596 KB Output is correct
25 Correct 102 ms 116104 KB Output is correct
26 Correct 89 ms 118968 KB Output is correct
27 Correct 88 ms 120160 KB Output is correct
28 Correct 84 ms 116400 KB Output is correct
29 Correct 111 ms 121032 KB Output is correct
30 Correct 85 ms 118420 KB Output is correct
31 Correct 90 ms 119572 KB Output is correct
32 Correct 89 ms 120388 KB Output is correct
33 Correct 88 ms 120312 KB Output is correct
34 Correct 95 ms 123032 KB Output is correct
35 Correct 94 ms 121052 KB Output is correct
36 Correct 108 ms 121768 KB Output is correct
37 Correct 109 ms 121796 KB Output is correct
38 Correct 74 ms 92964 KB Output is correct
39 Correct 67 ms 92944 KB Output is correct
40 Correct 93 ms 92868 KB Output is correct
41 Correct 67 ms 92868 KB Output is correct
42 Correct 69 ms 92924 KB Output is correct
43 Correct 66 ms 92940 KB Output is correct
44 Correct 67 ms 92880 KB Output is correct
45 Correct 66 ms 92860 KB Output is correct
46 Correct 66 ms 92868 KB Output is correct
47 Correct 65 ms 92984 KB Output is correct
48 Correct 65 ms 92904 KB Output is correct
49 Correct 66 ms 92924 KB Output is correct
50 Correct 67 ms 93072 KB Output is correct
51 Correct 71 ms 93056 KB Output is correct
52 Correct 78 ms 93052 KB Output is correct
53 Correct 68 ms 93040 KB Output is correct
54 Correct 66 ms 93124 KB Output is correct
55 Correct 66 ms 93052 KB Output is correct
56 Correct 70 ms 93124 KB Output is correct
57 Correct 83 ms 93124 KB Output is correct
58 Correct 66 ms 93132 KB Output is correct
59 Correct 66 ms 93108 KB Output is correct
60 Correct 68 ms 93060 KB Output is correct
61 Correct 67 ms 93080 KB Output is correct
62 Correct 66 ms 93004 KB Output is correct
63 Correct 68 ms 93452 KB Output is correct
64 Correct 67 ms 93036 KB Output is correct
65 Correct 83 ms 94476 KB Output is correct
66 Correct 70 ms 93720 KB Output is correct
67 Correct 68 ms 93836 KB Output is correct
68 Correct 67 ms 94280 KB Output is correct
69 Correct 66 ms 93764 KB Output is correct
70 Correct 81 ms 94352 KB Output is correct
71 Correct 85 ms 94020 KB Output is correct
72 Correct 78 ms 94176 KB Output is correct
73 Correct 69 ms 94532 KB Output is correct
74 Correct 86 ms 100744 KB Output is correct
75 Correct 71 ms 96552 KB Output is correct
76 Correct 82 ms 97540 KB Output is correct
77 Correct 72 ms 103688 KB Output is correct
78 Correct 78 ms 104044 KB Output is correct
79 Correct 86 ms 100044 KB Output is correct
80 Correct 75 ms 103608 KB Output is correct
81 Correct 84 ms 102312 KB Output is correct
82 Correct 96 ms 103692 KB Output is correct
83 Correct 70 ms 100292 KB Output is correct
84 Correct 70 ms 99464 KB Output is correct
85 Correct 86 ms 102560 KB Output is correct
86 Correct 332 ms 214332 KB Output is correct
87 Correct 349 ms 214048 KB Output is correct
88 Correct 335 ms 212872 KB Output is correct
89 Correct 373 ms 213708 KB Output is correct
90 Correct 367 ms 212868 KB Output is correct
91 Correct 359 ms 214868 KB Output is correct
92 Correct 357 ms 214504 KB Output is correct
93 Correct 353 ms 213552 KB Output is correct
94 Correct 361 ms 214276 KB Output is correct
95 Correct 357 ms 213568 KB Output is correct
96 Correct 360 ms 213468 KB Output is correct
97 Correct 345 ms 212812 KB Output is correct