Submission #376782

# Submission time Handle Problem Language Result Execution time Memory
376782 2021-03-12T03:47:07 Z KoD Boat (APIO16_boat) C++17
100 / 100
1060 ms 6788 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

constexpr int MOD = 1000000007;

struct Fp {
    int val;
    Fp(const int val = 0): val(((val % MOD) + MOD) % MOD) { }
    Fp& operator += (const Fp &other) {
        if ((val += other.val) >= MOD) val -= MOD;
        return *this;
    }
    Fp operator + (const Fp &other) const {
        return Fp(*this) += other;
    }
    Fp& operator -= (const Fp &other) {
        if ((val += MOD - other.val) >= MOD) val -= MOD;
        return *this;
    }
    Fp operator - (const Fp &other) const {
        return Fp(*this) -= other;
    }
    Fp& operator *= (const Fp &other) {
        val = (long long) val * other.val % MOD;
        return *this;
    }
    Fp operator * (const Fp &other) const {
        return Fp(*this) *= other;
    }
    Fp& operator /= (const Fp &other) {
        return *this *= other.inv();
    }
    Fp operator / (const Fp &other) const {
        return Fp(*this) /= other;
    }
    Fp inv() const {
        return pow(MOD - 2);
    }
    Fp pow(int e) const {
        Fp ret(1), mult(*this);
        while (e > 0) {
            if (e & 1) ret *= mult;
            mult *= mult;
            e >>= 1;
        }
        return ret;
    }
};

int main() {
    int N;
    std::cin >> N;
    Vec<int> A(N), B(N);
    Vec<int> cmp;
    cmp.reserve(2 * N);
    for (int i = 0; i < N; ++i) {
        std::cin >> A[i] >> B[i];
        B[i] += 1;
        cmp.push_back(A[i]);
        cmp.push_back(B[i]);
    }
    std::sort(cmp.begin(), cmp.end());
    cmp.erase(std::unique(cmp.begin(), cmp.end()), cmp.end());
    for (auto &x: A) {
        x = std::lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
    }
    for (auto &x: B) {
        x = std::lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
    }
    const auto size = (int) cmp.size() - 1;
    Vec<Vec<Fp>> coeff(size, Vec<Fp>(N + 1));
    for (int k = 0; k < size; ++k) {
        Fp val(cmp[k + 1] - cmp[k]);
        for (int j = 1; j <= N; ++j) {
            if (cmp[k + 1] - cmp[k] < j) {
                break;
            }
            coeff[k][j] = val;
            val *= Fp(cmp[k + 1] - cmp[k] - j);
            val /= Fp(j + 1);
        }
    }
    Vec<Vec<Fp>> dp(size, Vec<Fp>(N + 1));
    for (int k = 0; k < size; ++k) {
        dp[k][0] = Fp(1);
    }
    Fp ans;
    for (int i = 0; i < N; ++i) {
        Vec<Vec<Fp>> next(size, Vec<Fp>(N + 1));
        Fp sum;
        for (int k = 0; k < size; ++k) {
            next[k][0] += sum;
            for (int j = 0; j <= N; ++j) {
                if (dp[k][j].val == 0) {
                    continue;
                }
                next[k][j] += dp[k][j];
                if (A[i] <= k && k < B[i]) {
                    next[k][j + 1] += dp[k][j];
                    const auto end = dp[k][j] * coeff[k][j + 1];
                    ans += end;
                    sum += end;
                }
            }
        }
        dp = std::move(next);
    }
    std::cout << ans.val << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 688 ms 6424 KB Output is correct
2 Correct 705 ms 6468 KB Output is correct
3 Correct 694 ms 6636 KB Output is correct
4 Correct 699 ms 6416 KB Output is correct
5 Correct 719 ms 6684 KB Output is correct
6 Correct 711 ms 6520 KB Output is correct
7 Correct 749 ms 6556 KB Output is correct
8 Correct 737 ms 6556 KB Output is correct
9 Correct 715 ms 6468 KB Output is correct
10 Correct 703 ms 6616 KB Output is correct
11 Correct 740 ms 6552 KB Output is correct
12 Correct 746 ms 6464 KB Output is correct
13 Correct 688 ms 6424 KB Output is correct
14 Correct 707 ms 6764 KB Output is correct
15 Correct 703 ms 6544 KB Output is correct
16 Correct 107 ms 1464 KB Output is correct
17 Correct 142 ms 1620 KB Output is correct
18 Correct 112 ms 1552 KB Output is correct
19 Correct 115 ms 1516 KB Output is correct
20 Correct 117 ms 1556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 688 ms 6424 KB Output is correct
2 Correct 705 ms 6468 KB Output is correct
3 Correct 694 ms 6636 KB Output is correct
4 Correct 699 ms 6416 KB Output is correct
5 Correct 719 ms 6684 KB Output is correct
6 Correct 711 ms 6520 KB Output is correct
7 Correct 749 ms 6556 KB Output is correct
8 Correct 737 ms 6556 KB Output is correct
9 Correct 715 ms 6468 KB Output is correct
10 Correct 703 ms 6616 KB Output is correct
11 Correct 740 ms 6552 KB Output is correct
12 Correct 746 ms 6464 KB Output is correct
13 Correct 688 ms 6424 KB Output is correct
14 Correct 707 ms 6764 KB Output is correct
15 Correct 703 ms 6544 KB Output is correct
16 Correct 107 ms 1464 KB Output is correct
17 Correct 142 ms 1620 KB Output is correct
18 Correct 112 ms 1552 KB Output is correct
19 Correct 115 ms 1516 KB Output is correct
20 Correct 117 ms 1556 KB Output is correct
21 Correct 718 ms 5900 KB Output is correct
22 Correct 749 ms 6148 KB Output is correct
23 Correct 718 ms 6168 KB Output is correct
24 Correct 703 ms 6040 KB Output is correct
25 Correct 719 ms 5908 KB Output is correct
26 Correct 790 ms 5836 KB Output is correct
27 Correct 843 ms 6032 KB Output is correct
28 Correct 798 ms 5700 KB Output is correct
29 Correct 810 ms 5928 KB Output is correct
30 Correct 706 ms 6576 KB Output is correct
31 Correct 694 ms 6720 KB Output is correct
32 Correct 702 ms 6508 KB Output is correct
33 Correct 711 ms 6420 KB Output is correct
34 Correct 692 ms 6672 KB Output is correct
35 Correct 707 ms 6596 KB Output is correct
36 Correct 705 ms 6668 KB Output is correct
37 Correct 683 ms 6788 KB Output is correct
38 Correct 693 ms 6672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 620 KB Output is correct
2 Correct 10 ms 636 KB Output is correct
3 Correct 10 ms 620 KB Output is correct
4 Correct 11 ms 748 KB Output is correct
5 Correct 10 ms 620 KB Output is correct
6 Correct 11 ms 620 KB Output is correct
7 Correct 11 ms 620 KB Output is correct
8 Correct 11 ms 620 KB Output is correct
9 Correct 11 ms 620 KB Output is correct
10 Correct 11 ms 620 KB Output is correct
11 Correct 11 ms 620 KB Output is correct
12 Correct 10 ms 620 KB Output is correct
13 Correct 10 ms 620 KB Output is correct
14 Correct 10 ms 620 KB Output is correct
15 Correct 10 ms 620 KB Output is correct
16 Correct 6 ms 492 KB Output is correct
17 Correct 5 ms 492 KB Output is correct
18 Correct 7 ms 492 KB Output is correct
19 Correct 6 ms 492 KB Output is correct
20 Correct 8 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 688 ms 6424 KB Output is correct
2 Correct 705 ms 6468 KB Output is correct
3 Correct 694 ms 6636 KB Output is correct
4 Correct 699 ms 6416 KB Output is correct
5 Correct 719 ms 6684 KB Output is correct
6 Correct 711 ms 6520 KB Output is correct
7 Correct 749 ms 6556 KB Output is correct
8 Correct 737 ms 6556 KB Output is correct
9 Correct 715 ms 6468 KB Output is correct
10 Correct 703 ms 6616 KB Output is correct
11 Correct 740 ms 6552 KB Output is correct
12 Correct 746 ms 6464 KB Output is correct
13 Correct 688 ms 6424 KB Output is correct
14 Correct 707 ms 6764 KB Output is correct
15 Correct 703 ms 6544 KB Output is correct
16 Correct 107 ms 1464 KB Output is correct
17 Correct 142 ms 1620 KB Output is correct
18 Correct 112 ms 1552 KB Output is correct
19 Correct 115 ms 1516 KB Output is correct
20 Correct 117 ms 1556 KB Output is correct
21 Correct 718 ms 5900 KB Output is correct
22 Correct 749 ms 6148 KB Output is correct
23 Correct 718 ms 6168 KB Output is correct
24 Correct 703 ms 6040 KB Output is correct
25 Correct 719 ms 5908 KB Output is correct
26 Correct 790 ms 5836 KB Output is correct
27 Correct 843 ms 6032 KB Output is correct
28 Correct 798 ms 5700 KB Output is correct
29 Correct 810 ms 5928 KB Output is correct
30 Correct 706 ms 6576 KB Output is correct
31 Correct 694 ms 6720 KB Output is correct
32 Correct 702 ms 6508 KB Output is correct
33 Correct 711 ms 6420 KB Output is correct
34 Correct 692 ms 6672 KB Output is correct
35 Correct 707 ms 6596 KB Output is correct
36 Correct 705 ms 6668 KB Output is correct
37 Correct 683 ms 6788 KB Output is correct
38 Correct 693 ms 6672 KB Output is correct
39 Correct 10 ms 620 KB Output is correct
40 Correct 10 ms 636 KB Output is correct
41 Correct 10 ms 620 KB Output is correct
42 Correct 11 ms 748 KB Output is correct
43 Correct 10 ms 620 KB Output is correct
44 Correct 11 ms 620 KB Output is correct
45 Correct 11 ms 620 KB Output is correct
46 Correct 11 ms 620 KB Output is correct
47 Correct 11 ms 620 KB Output is correct
48 Correct 11 ms 620 KB Output is correct
49 Correct 11 ms 620 KB Output is correct
50 Correct 10 ms 620 KB Output is correct
51 Correct 10 ms 620 KB Output is correct
52 Correct 10 ms 620 KB Output is correct
53 Correct 10 ms 620 KB Output is correct
54 Correct 6 ms 492 KB Output is correct
55 Correct 5 ms 492 KB Output is correct
56 Correct 7 ms 492 KB Output is correct
57 Correct 6 ms 492 KB Output is correct
58 Correct 8 ms 512 KB Output is correct
59 Correct 907 ms 6552 KB Output is correct
60 Correct 879 ms 6584 KB Output is correct
61 Correct 918 ms 6620 KB Output is correct
62 Correct 905 ms 6684 KB Output is correct
63 Correct 930 ms 6536 KB Output is correct
64 Correct 1060 ms 6724 KB Output is correct
65 Correct 1000 ms 6504 KB Output is correct
66 Correct 1012 ms 6520 KB Output is correct
67 Correct 1032 ms 6448 KB Output is correct
68 Correct 1035 ms 6464 KB Output is correct
69 Correct 887 ms 6496 KB Output is correct
70 Correct 875 ms 6504 KB Output is correct
71 Correct 864 ms 6468 KB Output is correct
72 Correct 919 ms 6580 KB Output is correct
73 Correct 881 ms 6584 KB Output is correct
74 Correct 132 ms 1460 KB Output is correct
75 Correct 151 ms 1484 KB Output is correct
76 Correct 137 ms 1608 KB Output is correct
77 Correct 165 ms 1476 KB Output is correct
78 Correct 134 ms 1460 KB Output is correct