Submission #62684

# Submission time Handle Problem Language Result Execution time Memory
62684 2018-07-29T18:38:59 Z SpaimaCarpatilor None (JOI16_solitaire) C++17
80 / 100
2000 ms 1572 KB
#include<bits/stdc++.h>

using namespace std;

int N, x[2018];
char sir[3][2018];
const int mod = 1e9 + 7;

int add (int x, int y) {int ans = x + y; if (ans >= mod) ans -= mod; return ans;}
int subtract (int x, int y) {if (x >= y) return x - y; return x - y + mod;}
int mul (int x, int y) {return 1LL * x * y % mod;}
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

int power (int a, int b)
{
    int p = 1;
    for (int i=0; (1<<i) <= b; i++)
    {
        if (b & (1 << i)) p = mul (p, a);
        a = mul (a, a);
    }
    return p;
}

int inv[6009], fac[6009];
void Prec (int lim){fac[0] = inv[0] = 1;for (int i=1; i<=lim; i++)fac[i] = mul (fac[i - 1], i);
inv[lim] = power (fac[lim], mod - 2);for (int i=lim - 1; i>=0; i--)inv[i] = mul (inv[i + 1], i + 1);}
int comb (int N, int K){if (K > N || N < 0 || K < 0) return 0;
int ans = mul (fac[N], inv[N - K]);ans = mul (ans, inv[K]);return ans;}

void finish (int ans)
{
    printf ("%d\n", ans);
    exit (0);
}

void combine (int &cnt, int &l, int nc, int nl)
{
    l += nl;
    cnt = mul (cnt, mul (nc, comb (l, nl)));
}

int old[6009];
void shiftDp (int dp[], int sz, int type, int x)
{
    if (x == 0)
    {
        if (type == 0)
        {
            for (int i=1; i<=sz + 2; i++)
                dp[i] = 0;
        }
        return ;
    }
    for (int i=1; i<=sz; i++)
        old[i] = dp[i], dp[i] = 0;
    dp[sz + 1] = dp[sz + 2] = 0;
    if (type == 0)
    {
        ///I want to have at least one after
        for (int i=1; i<=sz; i++)
        if (old[i] > 0)
        {
            if (x == 1)
                adto (dp[i], mul (old[i], sz - i + 1));
            else
            {
                adto (dp[i], mul (old[i], (sz - i + 2) * (sz - i + 1)));
                adto (dp[i + 1], mul (old[i], 2 * i * (sz - i + 1)));
            }
        }
        return ;
    }
    ///I want them both before
    for (int i=1; i<=sz; i++)
        if (old[i] > 0)
        {
            if (x == 1)
                adto (dp[i + 1], mul (old[i], i));
            else
                adto (dp[i + 2], mul (old[i], (i + 1) * i));
        }
}

int dp[2][2][6009];
void solve (int l, int r, int &nc, int &nl)
{
    memset (dp[l & 1], 0, sizeof (dp[l & 1]));
    dp[l & 1][0][1] = (l != 1);
    dp[l & 1][1][1] = 1;
    shiftDp (dp[l & 1][0], 1, 0, x[l]);
    shiftDp (dp[l & 1][1], 1, 1, x[l]);
    int currL = x[l] + 1;
    for (int i=l; i<r; i++)
    {
        int nxt = (i & 1) ^ 1;
        memset (dp[nxt], 0, sizeof (dp[nxt & 1]));
        for (int j=0; j<2; j++)
            for (int k=0; k<2; k++)
                if (j + k > 0)
                {
                    for (int p1=1; p1<=currL; p1++)
                        for (int p2=1; p2<=currL+1; p2++)
                        {
                            if (j == 0 && p1 < p2) continue;
                            if (k == 0 && p1 >= p2) continue;
                            adto (dp[nxt][k][p2], dp[i & 1][j][p1]);
                        }
                }
        shiftDp (dp[nxt][0], currL + 1, 0, x[i + 1]);
        shiftDp (dp[nxt][1], currL + 1, 1, x[i + 1]);
        currL += x[i + 1] + 1;
    }
    if (r == N)
        memset (dp[r & 1][0], 0, sizeof (dp[r & 1][0]));
    int ans = 0;
    for (int i=0; i<2; i++)
        for (int j=1; j<=currL; j++)
            adto (ans, dp[r & 1][i][j]);
    nc = ans, nl = currL;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d\n", &N), Prec (3 * N + 1);
for (int i=0; i<3; i++)
{
    scanf ("%s", sir[i] + 1);
    for (int j=1; j<=N; j++)
        sir[i][j] = (sir[i][j] == 'o');
}
if (sir[0][1] == 0 || sir[0][N] == 0 || sir[2][1] == 0 || sir[2][N] == 0)
    finish (0);
for (int i=0; i<3; i+=2)
    for (int j=1; j<N; j++)
        if (sir[i][j] == 0 && sir[i][j + 1] == 0)
            finish (0);
for (int i=1; i<=N; i++)
    x[i] = (2 - sir[0][i] - sir[2][i]);
int extra = 0, cnt = 1, l = 0;
for (int j=1; j<=N; j++)
    if (sir[1][j] == 0)
    {
        int k = j;
        while (sir[1][k + 1] == 0 && k < N)
            k ++;
        int nc, nl;
        solve (j, k, nc, nl);
        combine (cnt, l, nc, nl);
        j = k;
    }
    else extra += x[j];
combine (cnt, l, fac[extra], extra);
printf ("%d\n", cnt);
return 0;
}

Compilation message

solitaire.cpp: In function 'int main()':
solitaire.cpp:128:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d\n", &N), Prec (3 * N + 1);
 ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
solitaire.cpp:131:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%s", sir[i] + 1);
     ~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 3 ms 728 KB Output is correct
5 Correct 3 ms 728 KB Output is correct
6 Correct 3 ms 728 KB Output is correct
7 Correct 3 ms 772 KB Output is correct
8 Correct 3 ms 772 KB Output is correct
9 Correct 2 ms 772 KB Output is correct
10 Correct 3 ms 772 KB Output is correct
11 Correct 3 ms 772 KB Output is correct
12 Correct 3 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 832 KB Output is correct
2 Correct 5 ms 836 KB Output is correct
3 Correct 3 ms 844 KB Output is correct
4 Correct 8 ms 992 KB Output is correct
5 Correct 8 ms 992 KB Output is correct
6 Correct 7 ms 996 KB Output is correct
7 Correct 5 ms 996 KB Output is correct
8 Correct 8 ms 996 KB Output is correct
9 Correct 6 ms 1012 KB Output is correct
10 Correct 5 ms 1020 KB Output is correct
11 Correct 5 ms 1028 KB Output is correct
12 Correct 7 ms 1140 KB Output is correct
13 Correct 197 ms 1164 KB Output is correct
14 Correct 354 ms 1164 KB Output is correct
15 Correct 632 ms 1164 KB Output is correct
16 Correct 296 ms 1212 KB Output is correct
17 Correct 682 ms 1212 KB Output is correct
18 Correct 366 ms 1228 KB Output is correct
19 Correct 510 ms 1228 KB Output is correct
20 Correct 625 ms 1228 KB Output is correct
21 Correct 549 ms 1252 KB Output is correct
22 Correct 824 ms 1252 KB Output is correct
23 Correct 563 ms 1268 KB Output is correct
24 Correct 810 ms 1276 KB Output is correct
25 Correct 703 ms 1276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1276 KB Output is correct
2 Correct 3 ms 1276 KB Output is correct
3 Correct 3 ms 1276 KB Output is correct
4 Correct 3 ms 1276 KB Output is correct
5 Correct 3 ms 1276 KB Output is correct
6 Correct 2 ms 1276 KB Output is correct
7 Correct 3 ms 1276 KB Output is correct
8 Correct 3 ms 1276 KB Output is correct
9 Correct 3 ms 1276 KB Output is correct
10 Correct 3 ms 1276 KB Output is correct
11 Correct 3 ms 1276 KB Output is correct
12 Correct 3 ms 1276 KB Output is correct
13 Correct 3 ms 1276 KB Output is correct
14 Correct 4 ms 1276 KB Output is correct
15 Correct 4 ms 1276 KB Output is correct
16 Correct 3 ms 1276 KB Output is correct
17 Correct 3 ms 1276 KB Output is correct
18 Correct 3 ms 1276 KB Output is correct
19 Correct 4 ms 1276 KB Output is correct
20 Correct 3 ms 1276 KB Output is correct
21 Correct 3 ms 1276 KB Output is correct
22 Correct 2 ms 1276 KB Output is correct
23 Correct 4 ms 1276 KB Output is correct
24 Correct 3 ms 1276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 3 ms 728 KB Output is correct
5 Correct 3 ms 728 KB Output is correct
6 Correct 3 ms 728 KB Output is correct
7 Correct 3 ms 772 KB Output is correct
8 Correct 3 ms 772 KB Output is correct
9 Correct 2 ms 772 KB Output is correct
10 Correct 3 ms 772 KB Output is correct
11 Correct 3 ms 772 KB Output is correct
12 Correct 3 ms 772 KB Output is correct
13 Correct 4 ms 1276 KB Output is correct
14 Correct 3 ms 1276 KB Output is correct
15 Correct 3 ms 1276 KB Output is correct
16 Correct 3 ms 1276 KB Output is correct
17 Correct 3 ms 1276 KB Output is correct
18 Correct 2 ms 1276 KB Output is correct
19 Correct 3 ms 1276 KB Output is correct
20 Correct 3 ms 1276 KB Output is correct
21 Correct 3 ms 1276 KB Output is correct
22 Correct 3 ms 1276 KB Output is correct
23 Correct 3 ms 1276 KB Output is correct
24 Correct 3 ms 1276 KB Output is correct
25 Correct 3 ms 1276 KB Output is correct
26 Correct 4 ms 1276 KB Output is correct
27 Correct 4 ms 1276 KB Output is correct
28 Correct 3 ms 1276 KB Output is correct
29 Correct 3 ms 1276 KB Output is correct
30 Correct 3 ms 1276 KB Output is correct
31 Correct 4 ms 1276 KB Output is correct
32 Correct 3 ms 1276 KB Output is correct
33 Correct 3 ms 1276 KB Output is correct
34 Correct 2 ms 1276 KB Output is correct
35 Correct 4 ms 1276 KB Output is correct
36 Correct 3 ms 1276 KB Output is correct
37 Correct 4 ms 1276 KB Output is correct
38 Correct 4 ms 1276 KB Output is correct
39 Correct 3 ms 1276 KB Output is correct
40 Correct 3 ms 1276 KB Output is correct
41 Correct 4 ms 1276 KB Output is correct
42 Correct 4 ms 1284 KB Output is correct
43 Correct 3 ms 1284 KB Output is correct
44 Correct 3 ms 1284 KB Output is correct
45 Correct 4 ms 1284 KB Output is correct
46 Correct 3 ms 1284 KB Output is correct
47 Correct 4 ms 1284 KB Output is correct
48 Correct 5 ms 1284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 3 ms 728 KB Output is correct
5 Correct 3 ms 728 KB Output is correct
6 Correct 3 ms 728 KB Output is correct
7 Correct 3 ms 772 KB Output is correct
8 Correct 3 ms 772 KB Output is correct
9 Correct 2 ms 772 KB Output is correct
10 Correct 3 ms 772 KB Output is correct
11 Correct 3 ms 772 KB Output is correct
12 Correct 3 ms 772 KB Output is correct
13 Correct 5 ms 832 KB Output is correct
14 Correct 5 ms 836 KB Output is correct
15 Correct 3 ms 844 KB Output is correct
16 Correct 8 ms 992 KB Output is correct
17 Correct 8 ms 992 KB Output is correct
18 Correct 7 ms 996 KB Output is correct
19 Correct 5 ms 996 KB Output is correct
20 Correct 8 ms 996 KB Output is correct
21 Correct 6 ms 1012 KB Output is correct
22 Correct 5 ms 1020 KB Output is correct
23 Correct 5 ms 1028 KB Output is correct
24 Correct 7 ms 1140 KB Output is correct
25 Correct 197 ms 1164 KB Output is correct
26 Correct 354 ms 1164 KB Output is correct
27 Correct 632 ms 1164 KB Output is correct
28 Correct 296 ms 1212 KB Output is correct
29 Correct 682 ms 1212 KB Output is correct
30 Correct 366 ms 1228 KB Output is correct
31 Correct 510 ms 1228 KB Output is correct
32 Correct 625 ms 1228 KB Output is correct
33 Correct 549 ms 1252 KB Output is correct
34 Correct 824 ms 1252 KB Output is correct
35 Correct 563 ms 1268 KB Output is correct
36 Correct 810 ms 1276 KB Output is correct
37 Correct 703 ms 1276 KB Output is correct
38 Correct 4 ms 1276 KB Output is correct
39 Correct 3 ms 1276 KB Output is correct
40 Correct 3 ms 1276 KB Output is correct
41 Correct 3 ms 1276 KB Output is correct
42 Correct 3 ms 1276 KB Output is correct
43 Correct 2 ms 1276 KB Output is correct
44 Correct 3 ms 1276 KB Output is correct
45 Correct 3 ms 1276 KB Output is correct
46 Correct 3 ms 1276 KB Output is correct
47 Correct 3 ms 1276 KB Output is correct
48 Correct 3 ms 1276 KB Output is correct
49 Correct 3 ms 1276 KB Output is correct
50 Correct 3 ms 1276 KB Output is correct
51 Correct 4 ms 1276 KB Output is correct
52 Correct 4 ms 1276 KB Output is correct
53 Correct 3 ms 1276 KB Output is correct
54 Correct 3 ms 1276 KB Output is correct
55 Correct 3 ms 1276 KB Output is correct
56 Correct 4 ms 1276 KB Output is correct
57 Correct 3 ms 1276 KB Output is correct
58 Correct 3 ms 1276 KB Output is correct
59 Correct 2 ms 1276 KB Output is correct
60 Correct 4 ms 1276 KB Output is correct
61 Correct 3 ms 1276 KB Output is correct
62 Correct 4 ms 1276 KB Output is correct
63 Correct 4 ms 1276 KB Output is correct
64 Correct 3 ms 1276 KB Output is correct
65 Correct 3 ms 1276 KB Output is correct
66 Correct 4 ms 1276 KB Output is correct
67 Correct 4 ms 1284 KB Output is correct
68 Correct 3 ms 1284 KB Output is correct
69 Correct 3 ms 1284 KB Output is correct
70 Correct 4 ms 1284 KB Output is correct
71 Correct 3 ms 1284 KB Output is correct
72 Correct 4 ms 1284 KB Output is correct
73 Correct 5 ms 1284 KB Output is correct
74 Correct 6 ms 1344 KB Output is correct
75 Correct 4 ms 1352 KB Output is correct
76 Correct 5 ms 1356 KB Output is correct
77 Correct 8 ms 1360 KB Output is correct
78 Correct 8 ms 1368 KB Output is correct
79 Correct 6 ms 1376 KB Output is correct
80 Correct 7 ms 1384 KB Output is correct
81 Correct 7 ms 1384 KB Output is correct
82 Correct 8 ms 1400 KB Output is correct
83 Correct 6 ms 1412 KB Output is correct
84 Correct 6 ms 1420 KB Output is correct
85 Correct 7 ms 1428 KB Output is correct
86 Execution timed out 2069 ms 1572 KB Time limit exceeded
87 Halted 0 ms 0 KB -