Submission #126570

#TimeUsernameProblemLanguageResultExecution timeMemory
126570choikiwonSolitaire (JOI16_solitaire)C++17
68 / 100
351 ms262148 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(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 (stderr)

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 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...