Submission #342029

# Submission time Handle Problem Language Result Execution time Memory
342029 2021-01-01T06:40:44 Z thecodingwizard Boat (APIO16_boat) C++11
100 / 100
659 ms 6636 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define inf 1000000010

const int mod = 1e9+7;

ll binpow(ll x, ll n, ll m) {
    assert(n >= 0);
    x %= m; //note: m*m must be less than 2^63 to avoid ll overflow
    ll res = 1;
    while (n > 0) {
        if (n % 2 == 1) //if n is odd
            res = res * x % m;
        x = x * x % m;
        n /= 2; //divide by two
    }
    return res;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    ii A[n];
    map<int, int> compress;
    set<int> setValues; setValues.insert(0); setValues.insert(1);
    F0R(i, n) {
        cin >> A[i].f >> A[i].s;
        A[i].s++;
        setValues.insert(A[i].f);
        setValues.insert(A[i].s);
    }
    int idx = 0;
    vector<int> values;
    for (auto x : setValues) {
        compress[x] = idx;
        values.pb(x);
        idx++;
    }
    F0R(i, n) {
        A[i].f = compress[A[i].f];
        A[i].s = compress[A[i].s];
    }

    // ct[i][j] = nCr(len + j - 1, j)
    int ct[values.size()][n+1];
    F0R(i, values.size()-1) {
        F0R(j, n+1) {
            if (j == 0) ct[i][j] = 0;
            else {
                int len = values[i+1]-values[i];
                if (j == 1) ct[i][j] = len;
                else {
                    ct[i][j] = ((ll)ct[i][j-1]*(len+j-1)%mod*binpow(j, mod-2, mod))%mod;
                }
            }
        }
        ct[i][0] = 0;
    }

    int dp[n][values.size()];
    int dpSum[n][values.size()];
    F0R(i, n) {
        F0R(j, values.size()-1) {
            if (j == 0) { dp[i][j] = 1; continue; }

            dp[i][j] = 0;
            int num = 0;
            for (int k = i-1; k >= -1; k--) {
                if (A[k+1].f <= j && A[k+1].s > j) {
                    num++;
                    dp[i][j] = (dp[i][j] + (ll)(k>=0?dpSum[k][j-1]:1)*ct[j][num])%mod;
                }
            }
        }
        F0R(j, values.size()-1) {
            dpSum[i][j] = ((j?dpSum[i][j-1]:0)+dp[i][j])%mod;
        }
    }

    ll ans = 0;
    FOR(j, 1, values.size()-1) {
        ans = (ans + dp[n-1][j]) % mod;
    }
    cout << ans << endl;

    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:13:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 | #define F0R(i, n) for (int i = 0; i < n; i++)
......
   59 |     F0R(i, values.size()-1) {
      |         ~~~~~~~~~~~~~~~~~~           
boat.cpp:59:5: note: in expansion of macro 'F0R'
   59 |     F0R(i, values.size()-1) {
      |     ^~~
boat.cpp:13:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 | #define F0R(i, n) for (int i = 0; i < n; i++)
......
   76 |         F0R(j, values.size()-1) {
      |             ~~~~~~~~~~~~~~~~~~       
boat.cpp:76:9: note: in expansion of macro 'F0R'
   76 |         F0R(j, values.size()-1) {
      |         ^~~
boat.cpp:13:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 | #define F0R(i, n) for (int i = 0; i < n; i++)
......
   88 |         F0R(j, values.size()-1) {
      |             ~~~~~~~~~~~~~~~~~~       
boat.cpp:88:9: note: in expansion of macro 'F0R'
   88 |         F0R(j, values.size()-1) {
      |         ^~~
boat.cpp:14:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define FOR(i, a, b) for (int i = a; i < b; i++)
......
   94 |     FOR(j, 1, values.size()-1) {
      |         ~~~~~~~~~~~~~~~~~~~~~           
boat.cpp:94:5: note: in expansion of macro 'FOR'
   94 |     FOR(j, 1, values.size()-1) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 357 ms 6440 KB Output is correct
2 Correct 350 ms 6380 KB Output is correct
3 Correct 350 ms 6292 KB Output is correct
4 Correct 343 ms 6380 KB Output is correct
5 Correct 345 ms 6380 KB Output is correct
6 Correct 239 ms 6380 KB Output is correct
7 Correct 232 ms 6488 KB Output is correct
8 Correct 240 ms 6380 KB Output is correct
9 Correct 241 ms 6380 KB Output is correct
10 Correct 237 ms 6400 KB Output is correct
11 Correct 236 ms 6400 KB Output is correct
12 Correct 240 ms 6380 KB Output is correct
13 Correct 235 ms 6528 KB Output is correct
14 Correct 230 ms 6380 KB Output is correct
15 Correct 240 ms 6380 KB Output is correct
16 Correct 68 ms 1388 KB Output is correct
17 Correct 71 ms 1516 KB Output is correct
18 Correct 71 ms 1388 KB Output is correct
19 Correct 72 ms 1516 KB Output is correct
20 Correct 72 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 357 ms 6440 KB Output is correct
2 Correct 350 ms 6380 KB Output is correct
3 Correct 350 ms 6292 KB Output is correct
4 Correct 343 ms 6380 KB Output is correct
5 Correct 345 ms 6380 KB Output is correct
6 Correct 239 ms 6380 KB Output is correct
7 Correct 232 ms 6488 KB Output is correct
8 Correct 240 ms 6380 KB Output is correct
9 Correct 241 ms 6380 KB Output is correct
10 Correct 237 ms 6400 KB Output is correct
11 Correct 236 ms 6400 KB Output is correct
12 Correct 240 ms 6380 KB Output is correct
13 Correct 235 ms 6528 KB Output is correct
14 Correct 230 ms 6380 KB Output is correct
15 Correct 240 ms 6380 KB Output is correct
16 Correct 68 ms 1388 KB Output is correct
17 Correct 71 ms 1516 KB Output is correct
18 Correct 71 ms 1388 KB Output is correct
19 Correct 72 ms 1516 KB Output is correct
20 Correct 72 ms 1388 KB Output is correct
21 Correct 481 ms 6044 KB Output is correct
22 Correct 479 ms 5996 KB Output is correct
23 Correct 466 ms 5796 KB Output is correct
24 Correct 469 ms 5996 KB Output is correct
25 Correct 487 ms 5740 KB Output is correct
26 Correct 587 ms 5656 KB Output is correct
27 Correct 569 ms 5888 KB Output is correct
28 Correct 553 ms 5612 KB Output is correct
29 Correct 565 ms 5868 KB Output is correct
30 Correct 247 ms 6380 KB Output is correct
31 Correct 237 ms 6380 KB Output is correct
32 Correct 241 ms 6252 KB Output is correct
33 Correct 235 ms 6380 KB Output is correct
34 Correct 236 ms 6252 KB Output is correct
35 Correct 367 ms 6508 KB Output is correct
36 Correct 358 ms 6252 KB Output is correct
37 Correct 358 ms 6380 KB Output is correct
38 Correct 351 ms 6252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 620 KB Output is correct
2 Correct 7 ms 620 KB Output is correct
3 Correct 7 ms 620 KB Output is correct
4 Correct 7 ms 620 KB Output is correct
5 Correct 7 ms 620 KB Output is correct
6 Correct 9 ms 620 KB Output is correct
7 Correct 8 ms 620 KB Output is correct
8 Correct 8 ms 620 KB Output is correct
9 Correct 8 ms 620 KB Output is correct
10 Correct 8 ms 620 KB Output is correct
11 Correct 8 ms 620 KB Output is correct
12 Correct 7 ms 620 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 8 ms 620 KB Output is correct
15 Correct 8 ms 620 KB Output is correct
16 Correct 5 ms 492 KB Output is correct
17 Correct 4 ms 492 KB Output is correct
18 Correct 5 ms 492 KB Output is correct
19 Correct 5 ms 492 KB Output is correct
20 Correct 5 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 357 ms 6440 KB Output is correct
2 Correct 350 ms 6380 KB Output is correct
3 Correct 350 ms 6292 KB Output is correct
4 Correct 343 ms 6380 KB Output is correct
5 Correct 345 ms 6380 KB Output is correct
6 Correct 239 ms 6380 KB Output is correct
7 Correct 232 ms 6488 KB Output is correct
8 Correct 240 ms 6380 KB Output is correct
9 Correct 241 ms 6380 KB Output is correct
10 Correct 237 ms 6400 KB Output is correct
11 Correct 236 ms 6400 KB Output is correct
12 Correct 240 ms 6380 KB Output is correct
13 Correct 235 ms 6528 KB Output is correct
14 Correct 230 ms 6380 KB Output is correct
15 Correct 240 ms 6380 KB Output is correct
16 Correct 68 ms 1388 KB Output is correct
17 Correct 71 ms 1516 KB Output is correct
18 Correct 71 ms 1388 KB Output is correct
19 Correct 72 ms 1516 KB Output is correct
20 Correct 72 ms 1388 KB Output is correct
21 Correct 481 ms 6044 KB Output is correct
22 Correct 479 ms 5996 KB Output is correct
23 Correct 466 ms 5796 KB Output is correct
24 Correct 469 ms 5996 KB Output is correct
25 Correct 487 ms 5740 KB Output is correct
26 Correct 587 ms 5656 KB Output is correct
27 Correct 569 ms 5888 KB Output is correct
28 Correct 553 ms 5612 KB Output is correct
29 Correct 565 ms 5868 KB Output is correct
30 Correct 247 ms 6380 KB Output is correct
31 Correct 237 ms 6380 KB Output is correct
32 Correct 241 ms 6252 KB Output is correct
33 Correct 235 ms 6380 KB Output is correct
34 Correct 236 ms 6252 KB Output is correct
35 Correct 367 ms 6508 KB Output is correct
36 Correct 358 ms 6252 KB Output is correct
37 Correct 358 ms 6380 KB Output is correct
38 Correct 351 ms 6252 KB Output is correct
39 Correct 8 ms 620 KB Output is correct
40 Correct 7 ms 620 KB Output is correct
41 Correct 7 ms 620 KB Output is correct
42 Correct 7 ms 620 KB Output is correct
43 Correct 7 ms 620 KB Output is correct
44 Correct 9 ms 620 KB Output is correct
45 Correct 8 ms 620 KB Output is correct
46 Correct 8 ms 620 KB Output is correct
47 Correct 8 ms 620 KB Output is correct
48 Correct 8 ms 620 KB Output is correct
49 Correct 8 ms 620 KB Output is correct
50 Correct 7 ms 620 KB Output is correct
51 Correct 8 ms 640 KB Output is correct
52 Correct 8 ms 620 KB Output is correct
53 Correct 8 ms 620 KB Output is correct
54 Correct 5 ms 492 KB Output is correct
55 Correct 4 ms 492 KB Output is correct
56 Correct 5 ms 492 KB Output is correct
57 Correct 5 ms 492 KB Output is correct
58 Correct 5 ms 492 KB Output is correct
59 Correct 537 ms 6508 KB Output is correct
60 Correct 521 ms 6380 KB Output is correct
61 Correct 510 ms 6508 KB Output is correct
62 Correct 534 ms 6560 KB Output is correct
63 Correct 523 ms 6312 KB Output is correct
64 Correct 659 ms 6508 KB Output is correct
65 Correct 643 ms 6380 KB Output is correct
66 Correct 639 ms 6340 KB Output is correct
67 Correct 646 ms 6508 KB Output is correct
68 Correct 658 ms 6508 KB Output is correct
69 Correct 538 ms 6496 KB Output is correct
70 Correct 552 ms 6316 KB Output is correct
71 Correct 553 ms 6568 KB Output is correct
72 Correct 556 ms 6380 KB Output is correct
73 Correct 557 ms 6636 KB Output is correct
74 Correct 104 ms 1388 KB Output is correct
75 Correct 111 ms 1608 KB Output is correct
76 Correct 109 ms 1516 KB Output is correct
77 Correct 109 ms 1388 KB Output is correct
78 Correct 103 ms 1388 KB Output is correct