답안 #263031

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
263031 2020-08-13T12:10:27 Z KoD Boat (APIO16_boat) C++11
100 / 100
678 ms 4472 KB
/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>


template <class T, class U>
bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

template <class T, class U>
bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */


class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { ++M_position; }
    bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };

  class reverse_iterator {
  private:
    int64_t M_position;
  
  public:
    reverse_iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { --M_position; }
    bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };
  
private:
  const iterator M_first, M_last;

public:
  range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
  iterator begin() const noexcept { return M_first; }
  iterator end() const noexcept { return M_last; }
  reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } 
  reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } 

};

/**
 * @title Range
 */

#include <type_traits>
#include <iterator>

template <class T>
class rev_impl {
public:
  using iterator = decltype(std::declval<T>().rbegin());

private:
  const iterator M_begin;
  const iterator M_end;

public:
  rev_impl(T &&cont) noexcept: M_begin(cont.rbegin()), M_end(cont.rend()) { }
  iterator begin() const noexcept { return M_begin; }
  iterator end() const noexcept { return M_end; }

};

template <class T>
rev_impl<T> rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */

#include <cstdint>

template <class Modulus>
class modular {
public:
  using value_type = uint32_t;
  using cover_type = uint64_t;
  static constexpr value_type mod() { return Modulus::value(); }

  template <class T>
  static value_type normalize(T value_) noexcept {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod();
      if (value_ == 0) return 0;
      return mod() - value_;
    }
    return value_ % mod();
  }

private:
  value_type value;

public:
  modular() noexcept : value(0) { }
  template <class T>
  explicit modular(T value_) noexcept : value(normalize(value_)) { }
  template <class T>
  explicit operator T() const noexcept { return static_cast<T>(value); }

  value_type get() const noexcept { return value; }
  modular operator - () const noexcept { return modular(mod() - value); }
  modular operator ~ () const noexcept { return inverse(); }

  value_type &extract() noexcept { return value; }
  modular inverse() const noexcept { return power(mod() - 2); }
  modular power(cover_type exp) const noexcept {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  modular operator + (const modular &rhs) const noexcept { return modular(*this) += rhs; }
  modular& operator += (const modular &rhs) noexcept { 
    if ((value += rhs.value) >= mod()) value -= mod(); 
    return *this; 
  }

  modular operator - (const modular &rhs) const noexcept { return modular(*this) -= rhs; }
  modular& operator -= (const modular &rhs) noexcept { 
    if ((value += mod() - rhs.value) >= mod()) value -= mod(); 
    return *this; 
  }

  modular operator * (const modular &rhs) const noexcept { return modular(*this) *= rhs; }
  modular& operator *= (const modular &rhs) noexcept { 
    value = (cover_type) value * rhs.value % mod();
    return *this;
  }

  modular operator / (const modular &rhs) const noexcept { return modular(*this) /= rhs; }
  modular& operator /= (const modular &rhs) noexcept { return (*this) *= rhs.inverse(); }

  bool zero() const noexcept { return value == 0; }
  bool operator == (const modular &rhs) const noexcept { return value == rhs.value; }
  bool operator != (const modular &rhs) const noexcept { return value != rhs.value; }

  friend std::ostream& operator << (std::ostream &stream, const modular &rhs) { return stream << rhs.value; }
  friend modular power(modular val, cover_type exp) noexcept { return val.power(exp); }
  friend modular inverse(modular val) noexcept { return val.inverse(); }

};

template <uint32_t Val>
struct modulus_impl { static constexpr uint32_t value() noexcept { return Val; } };
template <uint32_t Val>
using mint32_t = modular<modulus_impl<Val>>;

struct runtime_mod { static uint32_t &value() noexcept { static uint32_t val = 0; return val; } };
using rmint32_t = modular<runtime_mod>;

/**
 * @title Modint
 */

#include <cstddef>

template <class T, size_t N>
class factorials {
public:
  using value_type = T;
  static constexpr size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator () (size_t n, size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }

};

/**
 * @title Factorial
 */

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

using m32 = mint32_t<1000000007>;
factorials<m32, 500> fact;

int main() {
  i32 N;
  std::cin >> N;
  std::vector<i32> L(N), R(N);
  std::vector<i32> comp;
  comp.reserve(2 * N);
  for (auto i: range(0, N)) {
    std::cin >> L[i] >> R[i];
    ++R[i];
    comp.push_back(L[i]);
    comp.push_back(R[i]);
  }
  std::sort(comp.begin(), comp.end());
  comp.erase(std::unique(comp.begin(), comp.end()), comp.end());
  for (auto i: range(0, N)) {
    L[i] = std::lower_bound(comp.cbegin(), comp.cend(), L[i]) - comp.cbegin();
    R[i] = std::lower_bound(comp.cbegin(), comp.cend(), R[i]) - comp.cbegin();
  }
  const i32 size = comp.size() - 1;
  std::vector<i32> len(size);
  std::vector<std::vector<m32>> dp(size);
  std::vector<std::vector<m32>> coeff(size);
  for (auto i: range(0, size)) {
    len[i] = comp[i + 1] - comp[i];
    dp[i].resize(std::min(N, len[i]) + 1);
    coeff[i].resize(std::min(N, len[i]) + 1);
    coeff[i][0] = m32(1);
    for (auto j: range(1, std::min(N, len[i]) + 1)) {
      coeff[i][j] = coeff[i][j - 1] * m32(len[i] - j + 1) / m32(j);
    }
  }
  for (auto i: range(0, N)) {
    m32 sum;
    for (auto j: range(0, L[i])) {
      for (auto k: range(1, std::min(N, len[j]) + 1)) {
        sum += dp[j][k] * coeff[j][k];
      }
    }
    for (auto j: range(L[i], R[i])) {
      m32 add;
      for (auto k: range(1, std::min(N, len[j]) + 1)) {
        add += dp[j][k] * coeff[j][k];
      }
      for (auto k: rev(range(1, std::min(N, len[j])))) {
        dp[j][k + 1] += dp[j][k];
      }
      dp[j][1] += sum + m32(1);
      sum += add;
    }
  }
  m32 ans;
  for (auto j: range(0, size)) {
    for (auto k: range(1, std::min(N, len[j]) + 1)) {
      ans += dp[j][k] * coeff[j][k];
    }
  }
  std::cout << ans << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 2424 KB Output is correct
2 Correct 185 ms 2424 KB Output is correct
3 Correct 179 ms 2424 KB Output is correct
4 Correct 183 ms 2424 KB Output is correct
5 Correct 181 ms 2424 KB Output is correct
6 Correct 182 ms 2424 KB Output is correct
7 Correct 187 ms 2424 KB Output is correct
8 Correct 184 ms 2424 KB Output is correct
9 Correct 180 ms 2424 KB Output is correct
10 Correct 182 ms 2424 KB Output is correct
11 Correct 181 ms 2424 KB Output is correct
12 Correct 180 ms 2428 KB Output is correct
13 Correct 186 ms 2424 KB Output is correct
14 Correct 181 ms 2428 KB Output is correct
15 Correct 179 ms 2552 KB Output is correct
16 Correct 26 ms 640 KB Output is correct
17 Correct 27 ms 640 KB Output is correct
18 Correct 28 ms 640 KB Output is correct
19 Correct 27 ms 760 KB Output is correct
20 Correct 28 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 2424 KB Output is correct
2 Correct 185 ms 2424 KB Output is correct
3 Correct 179 ms 2424 KB Output is correct
4 Correct 183 ms 2424 KB Output is correct
5 Correct 181 ms 2424 KB Output is correct
6 Correct 182 ms 2424 KB Output is correct
7 Correct 187 ms 2424 KB Output is correct
8 Correct 184 ms 2424 KB Output is correct
9 Correct 180 ms 2424 KB Output is correct
10 Correct 182 ms 2424 KB Output is correct
11 Correct 181 ms 2424 KB Output is correct
12 Correct 180 ms 2428 KB Output is correct
13 Correct 186 ms 2424 KB Output is correct
14 Correct 181 ms 2428 KB Output is correct
15 Correct 179 ms 2552 KB Output is correct
16 Correct 26 ms 640 KB Output is correct
17 Correct 27 ms 640 KB Output is correct
18 Correct 28 ms 640 KB Output is correct
19 Correct 27 ms 760 KB Output is correct
20 Correct 28 ms 640 KB Output is correct
21 Correct 9 ms 512 KB Output is correct
22 Correct 9 ms 512 KB Output is correct
23 Correct 9 ms 512 KB Output is correct
24 Correct 9 ms 512 KB Output is correct
25 Correct 10 ms 512 KB Output is correct
26 Correct 8 ms 384 KB Output is correct
27 Correct 8 ms 384 KB Output is correct
28 Correct 8 ms 384 KB Output is correct
29 Correct 9 ms 384 KB Output is correct
30 Correct 170 ms 2296 KB Output is correct
31 Correct 168 ms 2168 KB Output is correct
32 Correct 169 ms 2168 KB Output is correct
33 Correct 173 ms 2176 KB Output is correct
34 Correct 171 ms 2168 KB Output is correct
35 Correct 164 ms 2168 KB Output is correct
36 Correct 179 ms 2168 KB Output is correct
37 Correct 173 ms 2296 KB Output is correct
38 Correct 172 ms 2168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 512 KB Output is correct
2 Correct 7 ms 512 KB Output is correct
3 Correct 8 ms 512 KB Output is correct
4 Correct 8 ms 512 KB Output is correct
5 Correct 8 ms 512 KB Output is correct
6 Correct 9 ms 512 KB Output is correct
7 Correct 10 ms 512 KB Output is correct
8 Correct 9 ms 512 KB Output is correct
9 Correct 9 ms 512 KB Output is correct
10 Correct 8 ms 512 KB Output is correct
11 Correct 8 ms 512 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 9 ms 512 KB Output is correct
14 Correct 9 ms 512 KB Output is correct
15 Correct 8 ms 512 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 2424 KB Output is correct
2 Correct 185 ms 2424 KB Output is correct
3 Correct 179 ms 2424 KB Output is correct
4 Correct 183 ms 2424 KB Output is correct
5 Correct 181 ms 2424 KB Output is correct
6 Correct 182 ms 2424 KB Output is correct
7 Correct 187 ms 2424 KB Output is correct
8 Correct 184 ms 2424 KB Output is correct
9 Correct 180 ms 2424 KB Output is correct
10 Correct 182 ms 2424 KB Output is correct
11 Correct 181 ms 2424 KB Output is correct
12 Correct 180 ms 2428 KB Output is correct
13 Correct 186 ms 2424 KB Output is correct
14 Correct 181 ms 2428 KB Output is correct
15 Correct 179 ms 2552 KB Output is correct
16 Correct 26 ms 640 KB Output is correct
17 Correct 27 ms 640 KB Output is correct
18 Correct 28 ms 640 KB Output is correct
19 Correct 27 ms 760 KB Output is correct
20 Correct 28 ms 640 KB Output is correct
21 Correct 9 ms 512 KB Output is correct
22 Correct 9 ms 512 KB Output is correct
23 Correct 9 ms 512 KB Output is correct
24 Correct 9 ms 512 KB Output is correct
25 Correct 10 ms 512 KB Output is correct
26 Correct 8 ms 384 KB Output is correct
27 Correct 8 ms 384 KB Output is correct
28 Correct 8 ms 384 KB Output is correct
29 Correct 9 ms 384 KB Output is correct
30 Correct 170 ms 2296 KB Output is correct
31 Correct 168 ms 2168 KB Output is correct
32 Correct 169 ms 2168 KB Output is correct
33 Correct 173 ms 2176 KB Output is correct
34 Correct 171 ms 2168 KB Output is correct
35 Correct 164 ms 2168 KB Output is correct
36 Correct 179 ms 2168 KB Output is correct
37 Correct 173 ms 2296 KB Output is correct
38 Correct 172 ms 2168 KB Output is correct
39 Correct 9 ms 512 KB Output is correct
40 Correct 7 ms 512 KB Output is correct
41 Correct 8 ms 512 KB Output is correct
42 Correct 8 ms 512 KB Output is correct
43 Correct 8 ms 512 KB Output is correct
44 Correct 9 ms 512 KB Output is correct
45 Correct 10 ms 512 KB Output is correct
46 Correct 9 ms 512 KB Output is correct
47 Correct 9 ms 512 KB Output is correct
48 Correct 8 ms 512 KB Output is correct
49 Correct 8 ms 512 KB Output is correct
50 Correct 7 ms 512 KB Output is correct
51 Correct 9 ms 512 KB Output is correct
52 Correct 9 ms 512 KB Output is correct
53 Correct 8 ms 512 KB Output is correct
54 Correct 4 ms 384 KB Output is correct
55 Correct 4 ms 384 KB Output is correct
56 Correct 4 ms 384 KB Output is correct
57 Correct 4 ms 384 KB Output is correct
58 Correct 4 ms 384 KB Output is correct
59 Correct 559 ms 4372 KB Output is correct
60 Correct 540 ms 4472 KB Output is correct
61 Correct 536 ms 4344 KB Output is correct
62 Correct 557 ms 4472 KB Output is correct
63 Correct 544 ms 4472 KB Output is correct
64 Correct 651 ms 4472 KB Output is correct
65 Correct 641 ms 4472 KB Output is correct
66 Correct 654 ms 4472 KB Output is correct
67 Correct 678 ms 4376 KB Output is correct
68 Correct 645 ms 4388 KB Output is correct
69 Correct 582 ms 4344 KB Output is correct
70 Correct 589 ms 4348 KB Output is correct
71 Correct 540 ms 4348 KB Output is correct
72 Correct 562 ms 4448 KB Output is correct
73 Correct 550 ms 4472 KB Output is correct
74 Correct 55 ms 768 KB Output is correct
75 Correct 61 ms 792 KB Output is correct
76 Correct 57 ms 888 KB Output is correct
77 Correct 66 ms 800 KB Output is correct
78 Correct 56 ms 792 KB Output is correct