Submission #547918

# Submission time Handle Problem Language Result Execution time Memory
547918 2022-04-12T03:08:19 Z Olympia Boat (APIO16_boat) C++17
100 / 100
986 ms 8348 KB
#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>

const int M = 1e9 + 7;
const int MOD = 1e9 + 7;
using namespace std;
int64_t binPow (int64_t x, int64_t y) {
    int64_t ans = 1, res = x;
    while (y) {
        if (y & 1) ans *= res, ans %= MOD;
        res *= res, res %= MOD, y /= 2;
    }
    return ans;
}
int64_t inv (int64_t x) {
    return binPow(x, MOD - 2);
}
struct modint {
    modint() : n(0) {}
    template <class T> modint(T n) { n %= M; if (n < 0) n += M; this->n = n; }

    modint & operator+=(const modint &r) { n += r.n; if (n >= M) n -= M; return *this; }
    modint & operator-=(const modint &r) { n -= r.n; if (n < 0) n += M; return *this; }
    modint & operator*=(const modint &r) { n = (int) ((long long) n * r.n % M); return *this; }

    modint & operator--() { if (--n == -1) n = M - 1; return *this; }
    modint & operator++() { if (++n == M) n = 0; return *this; }
    modint operator--(int) { modint t = *this; --*this; return t; }
    modint operator++(int) { modint t = *this; ++*this; return t; }

    modint operator-() const { return 0 - *this; }
    modint operator+() const { return *this; }

    modint pow(long long k = M - 2) const {
        modint f = *this, p = 1;
        while (k > 0) {
            if (k % 2 == 1) f *= p;
            p *= p, k /= 2;
        }
        return f;
    }

    int mod() const { return M; }

    friend modint operator+(modint l, const modint &r) { return l += r; }
    friend modint operator-(modint l, const modint &r) { return l -= r; }
    friend modint operator*(modint l, const modint &r) { return l *= r; }

    friend bool operator==(const modint &l, const modint &r) { return l.n == r.n; }
    friend bool operator!=(const modint &l, const modint &r) { return l.n != r.n; }

    friend ostream & operator<<(ostream &out, const modint &r) { return out << r.n; }

    int n;
};

vector<pair<int,int>> compress (vector<pair<int,int>> &vec) {
    set<int> s;
    for (auto& p: vec) {
        s.insert(p.first), s.insert(p.second);
    }
    vector<int> v; for (int i: s) v.push_back(i);
    vector<pair<int,int>> grids;
    grids.emplace_back(-1, -1);
    for (int i = 0; i < v.size(); i++) {
        grids.emplace_back(v[i], v[i]);
        if (i + 1 != v.size() && v[i] + 1 <= v[i + 1] - 1) grids.emplace_back(v[i] + 1, v[i + 1] - 1);
    }
    return grids;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N; cin >> N;
    vector<pair<int,int>> points(N);
    for (int i = 0; i < N; i++) cin >> points[i].first >> points[i].second;
    vector<pair<int,int>> grid = compress(points);
    modint combo[grid.size()][N + 1];
    for (int i = 0; i < grid.size(); i++) {
        combo[i][0] = 1;
        for (int j = 1; j <= N; j++) {
            combo[i][j] = combo[i][j - 1] * binPow(j, MOD - 2) * (grid[i].second - grid[i].first + j);
        }
    }
    modint dp[N + 1][grid.size()];
    for (int i = 0; i <= N; i++) {
        dp[i][0] = 1;
    }
    for (int i = 1; i < grid.size(); i++) {
        dp[0][i] = 1;
    }
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j < grid.size(); j++) {
            dp[i][j] = dp[i][j -1];
            int pos = 0;
            for (int k = i; k >= 1; k--) {
                if (grid[j].first >= points[k - 1].first && grid[j].second <= points[k - 1].second) {
                    dp[i][j] += dp[k - 1][j - 1] * combo[j][pos + 1];
                    pos++;
                }
            }
        }
    }
    cout << dp[N][grid.size() - 1] - 1;
}

Compilation message

boat.cpp: In function 'std::vector<std::pair<int, int> > compress(std::vector<std::pair<int, int> >&)':
boat.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = 0; i < v.size(); i++) {
      |                     ~~^~~~~~~~~~
boat.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |         if (i + 1 != v.size() && v[i] + 1 <= v[i + 1] - 1) grids.emplace_back(v[i] + 1, v[i + 1] - 1);
      |             ~~~~~~^~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < grid.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
boat.cpp:99:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |     for (int i = 1; i < grid.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
boat.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for (int j = 1; j < grid.size(); j++) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 296 ms 4232 KB Output is correct
2 Correct 354 ms 4276 KB Output is correct
3 Correct 298 ms 4172 KB Output is correct
4 Correct 300 ms 4272 KB Output is correct
5 Correct 343 ms 4272 KB Output is correct
6 Correct 212 ms 4268 KB Output is correct
7 Correct 225 ms 4400 KB Output is correct
8 Correct 230 ms 4232 KB Output is correct
9 Correct 231 ms 4244 KB Output is correct
10 Correct 243 ms 4232 KB Output is correct
11 Correct 219 ms 4272 KB Output is correct
12 Correct 228 ms 4244 KB Output is correct
13 Correct 231 ms 4172 KB Output is correct
14 Correct 209 ms 4228 KB Output is correct
15 Correct 244 ms 4272 KB Output is correct
16 Correct 64 ms 1008 KB Output is correct
17 Correct 65 ms 1068 KB Output is correct
18 Correct 62 ms 956 KB Output is correct
19 Correct 68 ms 1064 KB Output is correct
20 Correct 63 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 296 ms 4232 KB Output is correct
2 Correct 354 ms 4276 KB Output is correct
3 Correct 298 ms 4172 KB Output is correct
4 Correct 300 ms 4272 KB Output is correct
5 Correct 343 ms 4272 KB Output is correct
6 Correct 212 ms 4268 KB Output is correct
7 Correct 225 ms 4400 KB Output is correct
8 Correct 230 ms 4232 KB Output is correct
9 Correct 231 ms 4244 KB Output is correct
10 Correct 243 ms 4232 KB Output is correct
11 Correct 219 ms 4272 KB Output is correct
12 Correct 228 ms 4244 KB Output is correct
13 Correct 231 ms 4172 KB Output is correct
14 Correct 209 ms 4228 KB Output is correct
15 Correct 244 ms 4272 KB Output is correct
16 Correct 64 ms 1008 KB Output is correct
17 Correct 65 ms 1068 KB Output is correct
18 Correct 62 ms 956 KB Output is correct
19 Correct 68 ms 1064 KB Output is correct
20 Correct 63 ms 980 KB Output is correct
21 Correct 659 ms 6936 KB Output is correct
22 Correct 658 ms 6968 KB Output is correct
23 Correct 634 ms 6828 KB Output is correct
24 Correct 667 ms 6960 KB Output is correct
25 Correct 605 ms 6936 KB Output is correct
26 Correct 673 ms 6604 KB Output is correct
27 Correct 698 ms 6584 KB Output is correct
28 Correct 641 ms 6568 KB Output is correct
29 Correct 704 ms 6688 KB Output is correct
30 Correct 466 ms 7668 KB Output is correct
31 Correct 402 ms 7720 KB Output is correct
32 Correct 430 ms 7628 KB Output is correct
33 Correct 399 ms 7668 KB Output is correct
34 Correct 413 ms 7592 KB Output is correct
35 Correct 322 ms 4280 KB Output is correct
36 Correct 341 ms 4244 KB Output is correct
37 Correct 311 ms 4240 KB Output is correct
38 Correct 349 ms 4264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 596 KB Output is correct
2 Correct 11 ms 656 KB Output is correct
3 Correct 12 ms 576 KB Output is correct
4 Correct 12 ms 596 KB Output is correct
5 Correct 13 ms 624 KB Output is correct
6 Correct 14 ms 596 KB Output is correct
7 Correct 16 ms 596 KB Output is correct
8 Correct 13 ms 652 KB Output is correct
9 Correct 15 ms 740 KB Output is correct
10 Correct 14 ms 652 KB Output is correct
11 Correct 11 ms 652 KB Output is correct
12 Correct 11 ms 648 KB Output is correct
13 Correct 11 ms 652 KB Output is correct
14 Correct 11 ms 652 KB Output is correct
15 Correct 12 ms 572 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 5 ms 448 KB Output is correct
18 Correct 6 ms 392 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 5 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 296 ms 4232 KB Output is correct
2 Correct 354 ms 4276 KB Output is correct
3 Correct 298 ms 4172 KB Output is correct
4 Correct 300 ms 4272 KB Output is correct
5 Correct 343 ms 4272 KB Output is correct
6 Correct 212 ms 4268 KB Output is correct
7 Correct 225 ms 4400 KB Output is correct
8 Correct 230 ms 4232 KB Output is correct
9 Correct 231 ms 4244 KB Output is correct
10 Correct 243 ms 4232 KB Output is correct
11 Correct 219 ms 4272 KB Output is correct
12 Correct 228 ms 4244 KB Output is correct
13 Correct 231 ms 4172 KB Output is correct
14 Correct 209 ms 4228 KB Output is correct
15 Correct 244 ms 4272 KB Output is correct
16 Correct 64 ms 1008 KB Output is correct
17 Correct 65 ms 1068 KB Output is correct
18 Correct 62 ms 956 KB Output is correct
19 Correct 68 ms 1064 KB Output is correct
20 Correct 63 ms 980 KB Output is correct
21 Correct 659 ms 6936 KB Output is correct
22 Correct 658 ms 6968 KB Output is correct
23 Correct 634 ms 6828 KB Output is correct
24 Correct 667 ms 6960 KB Output is correct
25 Correct 605 ms 6936 KB Output is correct
26 Correct 673 ms 6604 KB Output is correct
27 Correct 698 ms 6584 KB Output is correct
28 Correct 641 ms 6568 KB Output is correct
29 Correct 704 ms 6688 KB Output is correct
30 Correct 466 ms 7668 KB Output is correct
31 Correct 402 ms 7720 KB Output is correct
32 Correct 430 ms 7628 KB Output is correct
33 Correct 399 ms 7668 KB Output is correct
34 Correct 413 ms 7592 KB Output is correct
35 Correct 322 ms 4280 KB Output is correct
36 Correct 341 ms 4244 KB Output is correct
37 Correct 311 ms 4240 KB Output is correct
38 Correct 349 ms 4264 KB Output is correct
39 Correct 11 ms 596 KB Output is correct
40 Correct 11 ms 656 KB Output is correct
41 Correct 12 ms 576 KB Output is correct
42 Correct 12 ms 596 KB Output is correct
43 Correct 13 ms 624 KB Output is correct
44 Correct 14 ms 596 KB Output is correct
45 Correct 16 ms 596 KB Output is correct
46 Correct 13 ms 652 KB Output is correct
47 Correct 15 ms 740 KB Output is correct
48 Correct 14 ms 652 KB Output is correct
49 Correct 11 ms 652 KB Output is correct
50 Correct 11 ms 648 KB Output is correct
51 Correct 11 ms 652 KB Output is correct
52 Correct 11 ms 652 KB Output is correct
53 Correct 12 ms 572 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
55 Correct 5 ms 448 KB Output is correct
56 Correct 6 ms 392 KB Output is correct
57 Correct 5 ms 340 KB Output is correct
58 Correct 5 ms 340 KB Output is correct
59 Correct 798 ms 8228 KB Output is correct
60 Correct 769 ms 8232 KB Output is correct
61 Correct 753 ms 8284 KB Output is correct
62 Correct 781 ms 8292 KB Output is correct
63 Correct 769 ms 8292 KB Output is correct
64 Correct 986 ms 8232 KB Output is correct
65 Correct 923 ms 8236 KB Output is correct
66 Correct 922 ms 8232 KB Output is correct
67 Correct 875 ms 8148 KB Output is correct
68 Correct 889 ms 8232 KB Output is correct
69 Correct 782 ms 8232 KB Output is correct
70 Correct 802 ms 8348 KB Output is correct
71 Correct 798 ms 8288 KB Output is correct
72 Correct 811 ms 8156 KB Output is correct
73 Correct 765 ms 8268 KB Output is correct
74 Correct 99 ms 1112 KB Output is correct
75 Correct 92 ms 1108 KB Output is correct
76 Correct 90 ms 1120 KB Output is correct
77 Correct 90 ms 1108 KB Output is correct
78 Correct 98 ms 1116 KB Output is correct