Submission #257034

# Submission time Handle Problem Language Result Execution time Memory
257034 2020-08-03T14:22:51 Z EntityIT Boat (APIO16_boat) C++14
100 / 100
1766 ms 3456 KB
#include <bits/stdc++.h>
using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())

template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
  template<class... Args>
  vec(size_t n = 0, Args... args)
      : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
  template<class... Args>
  vec(Args... args)
      : vector<T>(args...) {}
};

template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }

mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));

template<int Mod>
struct ModInt {
  int a;
  ModInt(int t_a = 0) {
    a = (t_a < Mod ? (t_a < 0 ? t_a + Mod : t_a) : t_a - Mod);
  }
  friend istream& operator>>(istream& in_stream, ModInt& o) {
    in_stream >> o.a;
    o.a = (o.a < Mod ? (o.a < 0 ? o.a + Mod : o.a) : o.a - Mod);
    return in_stream;
  }
  friend ostream& operator<<(ostream& out_stream, const ModInt& o) {
    out_stream << o.a;
    return out_stream;
  }

  bool operator!() const { return !a; }
  explicit operator bool() const { return !!a; }

  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt& o) const { return ModInt(a + o.a); }
  ModInt operator-(const ModInt& o) const { return ModInt(a - o.a); }
  ModInt operator*(const ModInt& o) const { return ModInt(static_cast<int>(static_cast<int64_t>(a) * o.a % Mod)); }
  ModInt operator/(const ModInt& o) const { return *this * o.Inverse(); }
  ModInt& operator+=(const ModInt& o) { return *this = *this + o; }
  ModInt& operator-=(const ModInt& o) { return *this = *this - o; }
  ModInt& operator*=(const ModInt& o) { return *this = *this * o; }
  ModInt& operator/=(const ModInt& o) { return *this = *this / o; }
  ModInt& operator++() { return *this = *this + ModInt(1); }
  ModInt& operator--() { return *this = *this - ModInt(1); }
  ModInt Inverse() const { return Power(Mod - 2); }
  template<class T>
  ModInt Power(T exp) const {
    ModInt ret(1), c = *this;
    for (; exp; exp >>= 1, c *= c) {
      if (exp & 1) {
        ret *= c;
      }
    }
    return ret;
  }
};
using Mint = ModInt<static_cast<int>(1e9) + 7>;

constexpr int kMod = static_cast<int>(1e9) + 7;

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  int n_schools; cin >> n_schools;
  vec<pair<int, int>, 1> ranges(n_schools);
  vec<int, 1> marks; marks.reserve(n_schools << 1);
  for (auto& range : ranges) {
    cin >> range.first >> range.second; ++range.second;
    marks.emplace_back(range.first);
    marks.emplace_back(range.second);
  }
  sort(ALL(marks)); marks.erase(unique(ALL(marks)), marks.end());

  vec<Mint, 2> binomials(n_schools + 1, n_schools + 1);
  binomials[0][0] = Mint(1);
  for (int i = 1; i < SZ(binomials); ++i) {
    for (int j = 0; j <= i; ++j) {
      binomials[i][j] = (j ? binomials[i - 1][j - 1] : Mint()) + binomials[i - 1][j];
    }
  }

  vec<Mint, 1> inverses(n_schools + 1);
  inverses[1] = Mint(1);
  for (int i = 2; i < SZ(inverses); ++i) {
    inverses[i] = inverses[kMod % i] * Mint(kMod - kMod / i);
  }

  vec<Mint, 2> f(SZ(marks), n_schools + 1);
  f[0][0] = Mint(1);
  for (int i = 1; i < SZ(marks); ++i) {
    int const n = marks[i] - marks[i - 1];
    vec<Mint, 1> binomials_2(min(n_schools, n) + 1);
    binomials_2[0] = Mint(1);
    for (int j = 1; j < SZ(binomials_2); ++j) {
      binomials_2[j] = binomials_2[j - 1] * Mint(n - j + 1) * inverses[j];
    }
    vec<Mint, 1> g(n_schools + 1);
    for (int amount = 1; amount < SZ(g); ++amount) {
      for (int j = 1; j <= min(amount, n); ++j) {
        g[amount] += binomials_2[j] * binomials[amount - 1][j - 1];
      }
    }

    for (int j = 0; j <= n_schools; ++j) {
      f[i][j] += f[i - 1][j];

      if (!j || marks[i] <= ranges[j - 1].first || ranges[j - 1].second <= marks[i - 1]) {
        continue;
      }
      int amount = 0;
      for (int prev_j = j - 1; ~prev_j; --prev_j) {
        amount += ranges[prev_j].first <= marks[i - 1] && marks[i] <= ranges[prev_j].second;
        f[i][j] += f[i - 1][prev_j] * g[amount];
      }
    }
  }

  cout << accumulate(1 + ALL(f.back()), Mint()) << '\n';

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 532 ms 3448 KB Output is correct
2 Correct 533 ms 3448 KB Output is correct
3 Correct 532 ms 3328 KB Output is correct
4 Correct 542 ms 3448 KB Output is correct
5 Correct 534 ms 3448 KB Output is correct
6 Correct 532 ms 3328 KB Output is correct
7 Correct 532 ms 3448 KB Output is correct
8 Correct 539 ms 3448 KB Output is correct
9 Correct 537 ms 3396 KB Output is correct
10 Correct 535 ms 3328 KB Output is correct
11 Correct 533 ms 3448 KB Output is correct
12 Correct 539 ms 3448 KB Output is correct
13 Correct 539 ms 3328 KB Output is correct
14 Correct 538 ms 3328 KB Output is correct
15 Correct 532 ms 3448 KB Output is correct
16 Correct 96 ms 1784 KB Output is correct
17 Correct 102 ms 1664 KB Output is correct
18 Correct 98 ms 1664 KB Output is correct
19 Correct 106 ms 1784 KB Output is correct
20 Correct 102 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 532 ms 3448 KB Output is correct
2 Correct 533 ms 3448 KB Output is correct
3 Correct 532 ms 3328 KB Output is correct
4 Correct 542 ms 3448 KB Output is correct
5 Correct 534 ms 3448 KB Output is correct
6 Correct 532 ms 3328 KB Output is correct
7 Correct 532 ms 3448 KB Output is correct
8 Correct 539 ms 3448 KB Output is correct
9 Correct 537 ms 3396 KB Output is correct
10 Correct 535 ms 3328 KB Output is correct
11 Correct 533 ms 3448 KB Output is correct
12 Correct 539 ms 3448 KB Output is correct
13 Correct 539 ms 3328 KB Output is correct
14 Correct 538 ms 3328 KB Output is correct
15 Correct 532 ms 3448 KB Output is correct
16 Correct 96 ms 1784 KB Output is correct
17 Correct 102 ms 1664 KB Output is correct
18 Correct 98 ms 1664 KB Output is correct
19 Correct 106 ms 1784 KB Output is correct
20 Correct 102 ms 1664 KB Output is correct
21 Correct 436 ms 3220 KB Output is correct
22 Correct 418 ms 3200 KB Output is correct
23 Correct 390 ms 3320 KB Output is correct
24 Correct 428 ms 3236 KB Output is correct
25 Correct 419 ms 3320 KB Output is correct
26 Correct 587 ms 3192 KB Output is correct
27 Correct 602 ms 3192 KB Output is correct
28 Correct 579 ms 3072 KB Output is correct
29 Correct 580 ms 3192 KB Output is correct
30 Correct 509 ms 3448 KB Output is correct
31 Correct 507 ms 3456 KB Output is correct
32 Correct 512 ms 3408 KB Output is correct
33 Correct 515 ms 3448 KB Output is correct
34 Correct 506 ms 3448 KB Output is correct
35 Correct 500 ms 3404 KB Output is correct
36 Correct 505 ms 3448 KB Output is correct
37 Correct 512 ms 3328 KB Output is correct
38 Correct 504 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 512 KB Output is correct
2 Correct 17 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
4 Correct 13 ms 512 KB Output is correct
5 Correct 17 ms 512 KB Output is correct
6 Correct 18 ms 512 KB Output is correct
7 Correct 15 ms 512 KB Output is correct
8 Correct 15 ms 512 KB Output is correct
9 Correct 15 ms 512 KB Output is correct
10 Correct 16 ms 512 KB Output is correct
11 Correct 13 ms 512 KB Output is correct
12 Correct 12 ms 512 KB Output is correct
13 Correct 12 ms 512 KB Output is correct
14 Correct 13 ms 512 KB Output is correct
15 Correct 13 ms 512 KB Output is correct
16 Correct 7 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 6 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 7 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 532 ms 3448 KB Output is correct
2 Correct 533 ms 3448 KB Output is correct
3 Correct 532 ms 3328 KB Output is correct
4 Correct 542 ms 3448 KB Output is correct
5 Correct 534 ms 3448 KB Output is correct
6 Correct 532 ms 3328 KB Output is correct
7 Correct 532 ms 3448 KB Output is correct
8 Correct 539 ms 3448 KB Output is correct
9 Correct 537 ms 3396 KB Output is correct
10 Correct 535 ms 3328 KB Output is correct
11 Correct 533 ms 3448 KB Output is correct
12 Correct 539 ms 3448 KB Output is correct
13 Correct 539 ms 3328 KB Output is correct
14 Correct 538 ms 3328 KB Output is correct
15 Correct 532 ms 3448 KB Output is correct
16 Correct 96 ms 1784 KB Output is correct
17 Correct 102 ms 1664 KB Output is correct
18 Correct 98 ms 1664 KB Output is correct
19 Correct 106 ms 1784 KB Output is correct
20 Correct 102 ms 1664 KB Output is correct
21 Correct 436 ms 3220 KB Output is correct
22 Correct 418 ms 3200 KB Output is correct
23 Correct 390 ms 3320 KB Output is correct
24 Correct 428 ms 3236 KB Output is correct
25 Correct 419 ms 3320 KB Output is correct
26 Correct 587 ms 3192 KB Output is correct
27 Correct 602 ms 3192 KB Output is correct
28 Correct 579 ms 3072 KB Output is correct
29 Correct 580 ms 3192 KB Output is correct
30 Correct 509 ms 3448 KB Output is correct
31 Correct 507 ms 3456 KB Output is correct
32 Correct 512 ms 3408 KB Output is correct
33 Correct 515 ms 3448 KB Output is correct
34 Correct 506 ms 3448 KB Output is correct
35 Correct 500 ms 3404 KB Output is correct
36 Correct 505 ms 3448 KB Output is correct
37 Correct 512 ms 3328 KB Output is correct
38 Correct 504 ms 3448 KB Output is correct
39 Correct 13 ms 512 KB Output is correct
40 Correct 17 ms 512 KB Output is correct
41 Correct 13 ms 512 KB Output is correct
42 Correct 13 ms 512 KB Output is correct
43 Correct 17 ms 512 KB Output is correct
44 Correct 18 ms 512 KB Output is correct
45 Correct 15 ms 512 KB Output is correct
46 Correct 15 ms 512 KB Output is correct
47 Correct 15 ms 512 KB Output is correct
48 Correct 16 ms 512 KB Output is correct
49 Correct 13 ms 512 KB Output is correct
50 Correct 12 ms 512 KB Output is correct
51 Correct 12 ms 512 KB Output is correct
52 Correct 13 ms 512 KB Output is correct
53 Correct 13 ms 512 KB Output is correct
54 Correct 7 ms 384 KB Output is correct
55 Correct 6 ms 384 KB Output is correct
56 Correct 6 ms 384 KB Output is correct
57 Correct 6 ms 384 KB Output is correct
58 Correct 7 ms 384 KB Output is correct
59 Correct 1521 ms 3412 KB Output is correct
60 Correct 1506 ms 3448 KB Output is correct
61 Correct 1477 ms 3448 KB Output is correct
62 Correct 1534 ms 3456 KB Output is correct
63 Correct 1519 ms 3328 KB Output is correct
64 Correct 1766 ms 3412 KB Output is correct
65 Correct 1726 ms 3328 KB Output is correct
66 Correct 1731 ms 3448 KB Output is correct
67 Correct 1732 ms 3448 KB Output is correct
68 Correct 1737 ms 3328 KB Output is correct
69 Correct 1414 ms 3448 KB Output is correct
70 Correct 1403 ms 3448 KB Output is correct
71 Correct 1396 ms 3412 KB Output is correct
72 Correct 1440 ms 3408 KB Output is correct
73 Correct 1428 ms 3404 KB Output is correct
74 Correct 194 ms 1768 KB Output is correct
75 Correct 184 ms 1784 KB Output is correct
76 Correct 198 ms 1664 KB Output is correct
77 Correct 196 ms 1664 KB Output is correct
78 Correct 189 ms 1664 KB Output is correct