Submission #203708

# Submission time Handle Problem Language Result Execution time Memory
203708 2020-02-21T23:38:14 Z dsabolic Boat (APIO16_boat) C++17
100 / 100
558 ms 4856 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
#define per(i, n, a) for(int (i) = (n) - 1; (i) >= (a); --(i))
#define trav(i, x) for(auto& (i) : (x))
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
mt19937 mrand(random_device{}());

const int INF = 0x3f3f3f3f;
const int OFF = 1 << 20;
const int MOD = (int)1e9 + 7;
const int N = 550;

int n;
pii q[N];
vector<int> comp;
ll dist[2 * N];
ll dp[N][2 * N];
ll inv [N];

ll add(ll a, ll b) {
    return (a + b) % MOD;
}

ll binpow(ll base, int exp) {
        ll ret = 1;
        while(exp) {
                if(exp & 1) {
                        ret = (ret * base) % MOD;
                        if(!ret) return ret;
                }

                exp >>= 1;
                base = (base * base) % MOD;
        }

        return ret;
}
int main() {
    scanf("%d", &n);
    rep(i, 1, n + 1) {
        scanf("%d%d", &q[i].X, &q[i].Y);
        comp.pb(q[i].X); comp.pb(q[i].Y + 1);
    }
    sort(all(comp));

    comp.erase(unique(all(comp)), comp.end());

        rep(i, 2, n + 1) inv[i] = binpow(i, MOD - 2);
    rep(i, 1, n + 1) {
        q[i].X = lower_bound(all(comp), q[i].X) - comp.begin() + 1;
        q[i].Y = lower_bound(all(comp), q[i].Y + 1) - comp.begin();
    }

    dist[0] = comp[0];
    rep(i, 1, SZ(comp)) dist[i] = comp[i] - comp[i - 1];

    //printf("compsz %d\n", SZ(comp));
    rep(j, 0, SZ(comp)) dp[0][j] = 1;
    rep(i, 1, n + 1) { // fiksiramo prefix
        dp[i][0] = 1;
        //printf("for %d is [%d,%d]\n", i, q[i].X, q[i].Y);
        rep(j, q[i].X, q[i].Y + 1) { // fiksiramo kraj
            dp[i][j] = add(dp[i][j], (dp[i - 1][j - 1] * dist[j]) % MOD); // ako samo sebe uzimamo
            //printf("dp[%d][%d] -> %lld\n", i, j, dp[i][j]);

            ll binom = dist[j] - 1; int cnt = 1;
            per(k, i, 1) {
                if(j >= q[k].X && j <= q[k].Y) {
                                        binom = (binom * (dist[j] - 1 + cnt)) % MOD;
                                        if(!binom) break;
                                        binom = (binom * inv[++cnt]) % MOD;

                    dp[i][j] = add(dp[i][j], (dp[k - 1][j - 1] * binom) % MOD);
                }
            }
        }

        rep(j, 1, SZ(comp))
            dp[i][j] = (dp[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
    }

    printf("%lld\n", ((ll)dp[n][SZ(comp) - 1] - 1 + MOD) % MOD);

    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
boat.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &q[i].X, &q[i].Y);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 4600 KB Output is correct
2 Correct 12 ms 4728 KB Output is correct
3 Correct 13 ms 4728 KB Output is correct
4 Correct 12 ms 4600 KB Output is correct
5 Correct 12 ms 4728 KB Output is correct
6 Correct 15 ms 4728 KB Output is correct
7 Correct 12 ms 4728 KB Output is correct
8 Correct 12 ms 4600 KB Output is correct
9 Correct 12 ms 4600 KB Output is correct
10 Correct 12 ms 4600 KB Output is correct
11 Correct 13 ms 4728 KB Output is correct
12 Correct 12 ms 4728 KB Output is correct
13 Correct 12 ms 4600 KB Output is correct
14 Correct 12 ms 4600 KB Output is correct
15 Correct 12 ms 4600 KB Output is correct
16 Correct 7 ms 3064 KB Output is correct
17 Correct 8 ms 3064 KB Output is correct
18 Correct 7 ms 3064 KB Output is correct
19 Correct 8 ms 3064 KB Output is correct
20 Correct 7 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 4600 KB Output is correct
2 Correct 12 ms 4728 KB Output is correct
3 Correct 13 ms 4728 KB Output is correct
4 Correct 12 ms 4600 KB Output is correct
5 Correct 12 ms 4728 KB Output is correct
6 Correct 15 ms 4728 KB Output is correct
7 Correct 12 ms 4728 KB Output is correct
8 Correct 12 ms 4600 KB Output is correct
9 Correct 12 ms 4600 KB Output is correct
10 Correct 12 ms 4600 KB Output is correct
11 Correct 13 ms 4728 KB Output is correct
12 Correct 12 ms 4728 KB Output is correct
13 Correct 12 ms 4600 KB Output is correct
14 Correct 12 ms 4600 KB Output is correct
15 Correct 12 ms 4600 KB Output is correct
16 Correct 7 ms 3064 KB Output is correct
17 Correct 8 ms 3064 KB Output is correct
18 Correct 7 ms 3064 KB Output is correct
19 Correct 8 ms 3064 KB Output is correct
20 Correct 7 ms 3064 KB Output is correct
21 Correct 204 ms 4728 KB Output is correct
22 Correct 184 ms 4600 KB Output is correct
23 Correct 164 ms 4600 KB Output is correct
24 Correct 199 ms 4732 KB Output is correct
25 Correct 196 ms 4728 KB Output is correct
26 Correct 376 ms 4600 KB Output is correct
27 Correct 388 ms 4728 KB Output is correct
28 Correct 388 ms 4728 KB Output is correct
29 Correct 404 ms 4728 KB Output is correct
30 Correct 13 ms 4604 KB Output is correct
31 Correct 12 ms 4728 KB Output is correct
32 Correct 12 ms 4728 KB Output is correct
33 Correct 12 ms 4728 KB Output is correct
34 Correct 14 ms 4728 KB Output is correct
35 Correct 13 ms 4728 KB Output is correct
36 Correct 13 ms 4728 KB Output is correct
37 Correct 13 ms 4600 KB Output is correct
38 Correct 13 ms 4600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 888 KB Output is correct
2 Correct 7 ms 884 KB Output is correct
3 Correct 8 ms 888 KB Output is correct
4 Correct 7 ms 760 KB Output is correct
5 Correct 7 ms 888 KB Output is correct
6 Correct 9 ms 888 KB Output is correct
7 Correct 10 ms 888 KB Output is correct
8 Correct 10 ms 888 KB Output is correct
9 Correct 10 ms 888 KB Output is correct
10 Correct 9 ms 888 KB Output is correct
11 Correct 7 ms 888 KB Output is correct
12 Correct 7 ms 888 KB Output is correct
13 Correct 8 ms 888 KB Output is correct
14 Correct 7 ms 888 KB Output is correct
15 Correct 7 ms 888 KB Output is correct
16 Correct 6 ms 888 KB Output is correct
17 Correct 6 ms 888 KB Output is correct
18 Correct 6 ms 888 KB Output is correct
19 Correct 6 ms 764 KB Output is correct
20 Correct 6 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 4600 KB Output is correct
2 Correct 12 ms 4728 KB Output is correct
3 Correct 13 ms 4728 KB Output is correct
4 Correct 12 ms 4600 KB Output is correct
5 Correct 12 ms 4728 KB Output is correct
6 Correct 15 ms 4728 KB Output is correct
7 Correct 12 ms 4728 KB Output is correct
8 Correct 12 ms 4600 KB Output is correct
9 Correct 12 ms 4600 KB Output is correct
10 Correct 12 ms 4600 KB Output is correct
11 Correct 13 ms 4728 KB Output is correct
12 Correct 12 ms 4728 KB Output is correct
13 Correct 12 ms 4600 KB Output is correct
14 Correct 12 ms 4600 KB Output is correct
15 Correct 12 ms 4600 KB Output is correct
16 Correct 7 ms 3064 KB Output is correct
17 Correct 8 ms 3064 KB Output is correct
18 Correct 7 ms 3064 KB Output is correct
19 Correct 8 ms 3064 KB Output is correct
20 Correct 7 ms 3064 KB Output is correct
21 Correct 204 ms 4728 KB Output is correct
22 Correct 184 ms 4600 KB Output is correct
23 Correct 164 ms 4600 KB Output is correct
24 Correct 199 ms 4732 KB Output is correct
25 Correct 196 ms 4728 KB Output is correct
26 Correct 376 ms 4600 KB Output is correct
27 Correct 388 ms 4728 KB Output is correct
28 Correct 388 ms 4728 KB Output is correct
29 Correct 404 ms 4728 KB Output is correct
30 Correct 13 ms 4604 KB Output is correct
31 Correct 12 ms 4728 KB Output is correct
32 Correct 12 ms 4728 KB Output is correct
33 Correct 12 ms 4728 KB Output is correct
34 Correct 14 ms 4728 KB Output is correct
35 Correct 13 ms 4728 KB Output is correct
36 Correct 13 ms 4728 KB Output is correct
37 Correct 13 ms 4600 KB Output is correct
38 Correct 13 ms 4600 KB Output is correct
39 Correct 7 ms 888 KB Output is correct
40 Correct 7 ms 884 KB Output is correct
41 Correct 8 ms 888 KB Output is correct
42 Correct 7 ms 760 KB Output is correct
43 Correct 7 ms 888 KB Output is correct
44 Correct 9 ms 888 KB Output is correct
45 Correct 10 ms 888 KB Output is correct
46 Correct 10 ms 888 KB Output is correct
47 Correct 10 ms 888 KB Output is correct
48 Correct 9 ms 888 KB Output is correct
49 Correct 7 ms 888 KB Output is correct
50 Correct 7 ms 888 KB Output is correct
51 Correct 8 ms 888 KB Output is correct
52 Correct 7 ms 888 KB Output is correct
53 Correct 7 ms 888 KB Output is correct
54 Correct 6 ms 888 KB Output is correct
55 Correct 6 ms 888 KB Output is correct
56 Correct 6 ms 888 KB Output is correct
57 Correct 6 ms 764 KB Output is correct
58 Correct 6 ms 888 KB Output is correct
59 Correct 252 ms 4600 KB Output is correct
60 Correct 229 ms 4600 KB Output is correct
61 Correct 218 ms 4728 KB Output is correct
62 Correct 251 ms 4728 KB Output is correct
63 Correct 243 ms 4600 KB Output is correct
64 Correct 556 ms 4856 KB Output is correct
65 Correct 553 ms 4728 KB Output is correct
66 Correct 556 ms 4728 KB Output is correct
67 Correct 558 ms 4848 KB Output is correct
68 Correct 553 ms 4728 KB Output is correct
69 Correct 217 ms 4600 KB Output is correct
70 Correct 220 ms 4728 KB Output is correct
71 Correct 230 ms 4640 KB Output is correct
72 Correct 233 ms 4604 KB Output is correct
73 Correct 239 ms 4728 KB Output is correct
74 Correct 39 ms 3064 KB Output is correct
75 Correct 32 ms 3064 KB Output is correct
76 Correct 34 ms 3064 KB Output is correct
77 Correct 33 ms 3064 KB Output is correct
78 Correct 34 ms 3064 KB Output is correct