Submission #126575

#TimeUsernameProblemLanguageResultExecution timeMemory
126575choikiwonSolitaire (JOI16_solitaire)C++17
100 / 100
1468 ms1328 KiB
#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 (stderr)

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]);
             ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...