Submission #365446

# Submission time Handle Problem Language Result Execution time Memory
365446 2021-02-11T17:08:54 Z arnold518 None (JOI16_solitaire) C++14
100 / 100
237 ms 69484 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 6000;
const ll MOD = 1e9+7;

int N, cnt;
char S[4][MAXN+10];
int A[4][MAXN+10];
int B[MAXN+10];
ll fac[MAXN+10], ifac[MAXN+10];

ll ans;
ll dp1[MAXN+10][MAXN+10], dp2[MAXN+10][MAXN+10];

ll mypow(ll x, ll y)
{
	if(y==0) return 1;
	if(y%2) return mypow(x, y-1)*x%MOD;
	ll t=mypow(x, y/2);
	return t*t%MOD;
}

ll inv(ll x)
{
	return mypow(x, MOD-2);
}

ll ncr(ll n, ll r)
{
	if(n<r) return 0;
	return fac[n]*ifac[r]%MOD*ifac[n-r]%MOD;
}

ll npr(ll n, ll r)
{
	if(n<r) return 0;
	return fac[n]*ifac[n-r]%MOD;
}

int main()
{
	fac[0]=1; ifac[0]=1;
	for(int i=1; i<=MAXN; i++) fac[i]=fac[i-1]*i%MOD;
	for(int i=1; i<=MAXN; i++) ifac[i]=inv(fac[i]);

	scanf("%d", &N);
	scanf(" %s", S[1]+1);
	scanf(" %s", S[2]+1);
	scanf(" %s", S[3]+1);
	for(int i=1; i<=N; i++)
	{
		A[1][i]=(S[1][i]=='x');
		A[2][i]=(S[2][i]=='x');
		A[3][i]=(S[3][i]=='x');
	}

	A[1][0]=1; A[1][N+1]=1;
	A[2][0]=1; A[2][N+1]=1;
	A[3][0]=1; A[3][N+1]=1;
	for(int i=1; i<=N; i++)
	{
		if(A[1][i])
		{
			if(A[1][i-1] || A[1][i+1])
			{
				return !printf("0\n");
			}
		}
	}
	for(int i=1; i<=N; i++)
	{
		if(A[3][i])
		{
			if(A[3][i-1] || A[3][i+1])
			{
				return !printf("0\n");
			}
		}
	}

	int cnt=0;
	for(int i=1; i<=N; i++)
	{
		if(A[2][i]==0)
		{
			B[i]=-1;
			cnt+=A[1][i]+A[3][i];
		}
		else
		{
			B[i]=A[1][i]+A[3][i];
		}
	}

	int sz=0;
	vector<ll> V;
	vector<int> VV;
	VV.push_back(0);
	for(int i=1; i<=N; i++) if(B[i]==-1) VV.push_back(i);
	VV.push_back(N+1);
	
	sz=cnt;
	ans=fac[cnt];
	for(int i=0; i+1<VV.size(); i++)
	{
		int l=VV[i]+1, r=VV[i+1]-1;
		if(l>r) continue;

		int val=0;
		for(int j=1; j<=B[l]; j++) dp1[l][j]=fac[B[l]];
		dp2[l][B[l]+1]=fac[B[l]];
		val+=B[l]+1;
		for(int k=2; k<=val; k++)
		{
			dp1[l][k]+=dp1[l][k-1];
			dp1[l][k]%=MOD;
			dp2[l][k]+=dp2[l][k-1];
			dp2[l][k]%=MOD;
		}

		if(l==1)
		{
			for(int j=1; j<=B[l]; j++) dp1[l][j]=0;
		}

		for(int j=l+1; j<=r; j++)
		{
			for(int k=1; k<=val+B[j]+1; k++)
			{
				for(int p=B[j]; p<=B[j]; p++)
				{
					ll t=ncr(B[j], p)*npr(k-1, p)%MOD*npr(val+B[j]+1-k, B[j]-p)%MOD;
					dp2[j][k]+=dp2[j-1][val]*t%MOD;
					dp2[j][k]%=MOD;
					ll x=dp1[j-1][val]-dp1[j-1][k-p-1];
					x%=MOD;
					if(x<0) x+=MOD;
					dp2[j][k]+=x*t%MOD;
					dp2[j][k]%=MOD;
				}
				for(int p=0; p<B[j]; p++)
				{
					ll t=ncr(B[j], p)*npr(k-1, p)%MOD*npr(val+B[j]+1-k, B[j]-p)%MOD;
					dp1[j][k]+=dp2[j-1][k-p-1]*t%MOD;
					dp1[j][k]%=MOD;
				}
			}

			val+=B[j]+1;
			for(int k=2; k<=val; k++)
			{
				dp1[j][k]+=dp1[j][k-1];
				dp1[j][k]%=MOD;
				dp2[j][k]+=dp2[j][k-1];
				dp2[j][k]%=MOD;
			}
		}

		ll t=0;
		if(r!=N)
		{
			t+=dp1[r][val];
			t%=MOD;
		}
		t+=dp2[r][val];
		t%=MOD;
		ans=ans*ncr(sz+val, sz)%MOD*t%MOD;
		sz+=val;
	}

	printf("%lld\n", ans);
}

Compilation message

solitaire.cpp: In function 'int main()':
solitaire.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |  for(int i=0; i+1<VV.size(); i++)
      |               ~~~^~~~~~~~~~
solitaire.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
solitaire.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |  scanf(" %s", S[1]+1);
      |  ~~~~~^~~~~~~~~~~~~~~
solitaire.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |  scanf(" %s", S[2]+1);
      |  ~~~~~^~~~~~~~~~~~~~~
solitaire.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |  scanf(" %s", S[3]+1);
      |  ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4460 KB Output is correct
2 Correct 7 ms 7276 KB Output is correct
3 Correct 5 ms 2668 KB Output is correct
4 Correct 10 ms 10988 KB Output is correct
5 Correct 9 ms 9708 KB Output is correct
6 Correct 8 ms 8940 KB Output is correct
7 Correct 5 ms 5484 KB Output is correct
8 Correct 9 ms 10988 KB Output is correct
9 Correct 10 ms 11244 KB Output is correct
10 Correct 5 ms 5356 KB Output is correct
11 Correct 8 ms 7532 KB Output is correct
12 Correct 10 ms 11244 KB Output is correct
13 Correct 25 ms 20076 KB Output is correct
14 Correct 29 ms 21484 KB Output is correct
15 Correct 31 ms 21996 KB Output is correct
16 Correct 26 ms 20204 KB Output is correct
17 Correct 33 ms 22508 KB Output is correct
18 Correct 33 ms 21148 KB Output is correct
19 Correct 33 ms 21740 KB Output is correct
20 Correct 32 ms 22124 KB Output is correct
21 Correct 32 ms 22124 KB Output is correct
22 Correct 36 ms 23532 KB Output is correct
23 Correct 32 ms 22380 KB Output is correct
24 Correct 33 ms 22892 KB Output is correct
25 Correct 34 ms 22892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 620 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 3 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 748 KB Output is correct
14 Correct 2 ms 748 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
17 Correct 3 ms 748 KB Output is correct
18 Correct 3 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 3 ms 768 KB Output is correct
21 Correct 3 ms 748 KB Output is correct
22 Correct 3 ms 748 KB Output is correct
23 Correct 3 ms 748 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 2 ms 492 KB Output is correct
17 Correct 2 ms 492 KB Output is correct
18 Correct 2 ms 492 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 3 ms 492 KB Output is correct
23 Correct 3 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 2 ms 748 KB Output is correct
27 Correct 2 ms 748 KB Output is correct
28 Correct 3 ms 748 KB Output is correct
29 Correct 3 ms 748 KB Output is correct
30 Correct 3 ms 748 KB Output is correct
31 Correct 2 ms 748 KB Output is correct
32 Correct 3 ms 768 KB Output is correct
33 Correct 3 ms 748 KB Output is correct
34 Correct 3 ms 748 KB Output is correct
35 Correct 3 ms 748 KB Output is correct
36 Correct 3 ms 748 KB Output is correct
37 Correct 2 ms 620 KB Output is correct
38 Correct 3 ms 1004 KB Output is correct
39 Correct 2 ms 748 KB Output is correct
40 Correct 3 ms 1900 KB Output is correct
41 Correct 3 ms 1260 KB Output is correct
42 Correct 3 ms 1408 KB Output is correct
43 Correct 3 ms 1644 KB Output is correct
44 Correct 3 ms 1132 KB Output is correct
45 Correct 3 ms 1772 KB Output is correct
46 Correct 3 ms 1516 KB Output is correct
47 Correct 3 ms 1644 KB Output is correct
48 Correct 3 ms 2028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 5 ms 4460 KB Output is correct
14 Correct 7 ms 7276 KB Output is correct
15 Correct 5 ms 2668 KB Output is correct
16 Correct 10 ms 10988 KB Output is correct
17 Correct 9 ms 9708 KB Output is correct
18 Correct 8 ms 8940 KB Output is correct
19 Correct 5 ms 5484 KB Output is correct
20 Correct 9 ms 10988 KB Output is correct
21 Correct 10 ms 11244 KB Output is correct
22 Correct 5 ms 5356 KB Output is correct
23 Correct 8 ms 7532 KB Output is correct
24 Correct 10 ms 11244 KB Output is correct
25 Correct 25 ms 20076 KB Output is correct
26 Correct 29 ms 21484 KB Output is correct
27 Correct 31 ms 21996 KB Output is correct
28 Correct 26 ms 20204 KB Output is correct
29 Correct 33 ms 22508 KB Output is correct
30 Correct 33 ms 21148 KB Output is correct
31 Correct 33 ms 21740 KB Output is correct
32 Correct 32 ms 22124 KB Output is correct
33 Correct 32 ms 22124 KB Output is correct
34 Correct 36 ms 23532 KB Output is correct
35 Correct 32 ms 22380 KB Output is correct
36 Correct 33 ms 22892 KB Output is correct
37 Correct 34 ms 22892 KB Output is correct
38 Correct 2 ms 492 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 2 ms 492 KB Output is correct
43 Correct 2 ms 492 KB Output is correct
44 Correct 2 ms 620 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 2 ms 492 KB Output is correct
47 Correct 3 ms 492 KB Output is correct
48 Correct 3 ms 492 KB Output is correct
49 Correct 2 ms 492 KB Output is correct
50 Correct 2 ms 748 KB Output is correct
51 Correct 2 ms 748 KB Output is correct
52 Correct 2 ms 748 KB Output is correct
53 Correct 3 ms 748 KB Output is correct
54 Correct 3 ms 748 KB Output is correct
55 Correct 3 ms 748 KB Output is correct
56 Correct 2 ms 748 KB Output is correct
57 Correct 3 ms 768 KB Output is correct
58 Correct 3 ms 748 KB Output is correct
59 Correct 3 ms 748 KB Output is correct
60 Correct 3 ms 748 KB Output is correct
61 Correct 3 ms 748 KB Output is correct
62 Correct 2 ms 620 KB Output is correct
63 Correct 3 ms 1004 KB Output is correct
64 Correct 2 ms 748 KB Output is correct
65 Correct 3 ms 1900 KB Output is correct
66 Correct 3 ms 1260 KB Output is correct
67 Correct 3 ms 1408 KB Output is correct
68 Correct 3 ms 1644 KB Output is correct
69 Correct 3 ms 1132 KB Output is correct
70 Correct 3 ms 1772 KB Output is correct
71 Correct 3 ms 1516 KB Output is correct
72 Correct 3 ms 1644 KB Output is correct
73 Correct 3 ms 2028 KB Output is correct
74 Correct 7 ms 7532 KB Output is correct
75 Correct 5 ms 3948 KB Output is correct
76 Correct 5 ms 4332 KB Output is correct
77 Correct 10 ms 10368 KB Output is correct
78 Correct 9 ms 10604 KB Output is correct
79 Correct 6 ms 6508 KB Output is correct
80 Correct 9 ms 10092 KB Output is correct
81 Correct 8 ms 8684 KB Output is correct
82 Correct 9 ms 10220 KB Output is correct
83 Correct 6 ms 6636 KB Output is correct
84 Correct 6 ms 5740 KB Output is correct
85 Correct 8 ms 8940 KB Output is correct
86 Correct 229 ms 69228 KB Output is correct
87 Correct 233 ms 69100 KB Output is correct
88 Correct 226 ms 68588 KB Output is correct
89 Correct 228 ms 68972 KB Output is correct
90 Correct 237 ms 68460 KB Output is correct
91 Correct 236 ms 69484 KB Output is correct
92 Correct 230 ms 69228 KB Output is correct
93 Correct 234 ms 68948 KB Output is correct
94 Correct 230 ms 69228 KB Output is correct
95 Correct 227 ms 68844 KB Output is correct
96 Correct 229 ms 68844 KB Output is correct
97 Correct 226 ms 68460 KB Output is correct