Submission #218251

# Submission time Handle Problem Language Result Execution time Memory
218251 2020-04-01T17:01:35 Z Akashi Ruins 3 (JOI20_ruins3) C++14
100 / 100
481 ms 8824 KB
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

int n;
int a[1205];
int dp[1205][1205], C[1205][1205];
int fact[1205];

void build_pascal(){
    fact[0] = 1;
    for(int i = 1; i <= 1200 ; ++i) fact[i] = (1LL * fact[i - 1] * i) % MOD;

    C[0][0] = 1;
    for(int i = 1; i <= 1200 ; ++i){
        C[i][0] = 1;
        for(int j = 1; j <= i ; ++j){
            C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
            if(C[i][j] >= MOD) C[i][j] -= MOD;
        }
    }
}

inline int lgput(int x, int p){
    int ans = 1, aux = x;
    for(int i = 1; i <= p ; i = i << 1){
        if(i & p) ans = (1LL * ans * aux) % MOD;
        aux = (1LL * aux * aux) % MOD;
    }
    return ans;
}

int main()
{
//    freopen("1.in", "r", stdin);

    scanf("%d", &n);
    for(int i = 1; i <= n ; ++i)
        scanf("%d", &a[i]);

    if(a[1] == 1 || a[n] != 2 * n){
        printf("0");
        return 0;
    }

    build_pascal();

    dp[0][n + 1] = 1;
    for(int l = 1, i = 0; i < n ; ++i, l = 1 - l){
        for(int j = n + 1; j >= 1 ; --j){
            if(dp[i][j] == 0) continue ;

            int libere = a[i + 1] - (i + 1) - (n - j + 1);
            for(int k = 0; k <= min(n, min(libere, j)) ; ++k){
                if(j - k > n - i) continue ;
                int pos = 1;
                if(k == 0) pos = (n - (j - k) + 1) * 2 - (a[i + 1] - (libere - k) - 1);

                int add = 1LL * dp[i][j] * C[2 * k][k] % MOD * C[libere][k] % MOD * fact[k] % MOD;
                add = 1LL * add * pos % MOD;
                dp[i + 1][j - k] = (dp[i + 1][j - k] + add);
                if(dp[i + 1][j - k] >= MOD) dp[i + 1][j - k] -= MOD;
            }
        }
    }

    int Sol = dp[n][1];
    for(int i = 1; i <= n ; ++i) Sol = (1LL * Sol * lgput(2, MOD - 2)) % MOD;

    printf("%d", Sol);

    return 0;
}












Compilation message

ruins3.cpp: In function 'int main()':
ruins3.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
ruins3.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a[i]);
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5888 KB Output is correct
2 Correct 10 ms 6016 KB Output is correct
3 Correct 10 ms 6016 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 10 ms 5888 KB Output is correct
6 Correct 10 ms 6016 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 10 ms 6016 KB Output is correct
10 Correct 10 ms 6016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5888 KB Output is correct
2 Correct 10 ms 6016 KB Output is correct
3 Correct 10 ms 6016 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 10 ms 5888 KB Output is correct
6 Correct 10 ms 6016 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 10 ms 6016 KB Output is correct
10 Correct 10 ms 6016 KB Output is correct
11 Correct 10 ms 6272 KB Output is correct
12 Correct 10 ms 6016 KB Output is correct
13 Correct 11 ms 6144 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 11 ms 6144 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 11 ms 6144 KB Output is correct
19 Correct 11 ms 6144 KB Output is correct
20 Correct 11 ms 6144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5888 KB Output is correct
2 Correct 10 ms 6016 KB Output is correct
3 Correct 10 ms 6016 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 10 ms 5888 KB Output is correct
6 Correct 10 ms 6016 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 10 ms 6016 KB Output is correct
10 Correct 10 ms 6016 KB Output is correct
11 Correct 10 ms 6272 KB Output is correct
12 Correct 10 ms 6016 KB Output is correct
13 Correct 11 ms 6144 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 11 ms 6144 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 11 ms 6144 KB Output is correct
19 Correct 11 ms 6144 KB Output is correct
20 Correct 11 ms 6144 KB Output is correct
21 Correct 14 ms 8448 KB Output is correct
22 Correct 13 ms 8320 KB Output is correct
23 Correct 15 ms 8448 KB Output is correct
24 Correct 16 ms 8448 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 11 ms 5888 KB Output is correct
27 Correct 4 ms 256 KB Output is correct
28 Correct 16 ms 8320 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 481 ms 8772 KB Output is correct
31 Correct 240 ms 8700 KB Output is correct
32 Correct 354 ms 8696 KB Output is correct
33 Correct 446 ms 8672 KB Output is correct
34 Correct 233 ms 8696 KB Output is correct
35 Correct 354 ms 8824 KB Output is correct
36 Correct 440 ms 8696 KB Output is correct