답안 #263033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
263033 2020-08-13T12:11:55 Z KoD Boat (APIO16_boat) C++11
100 / 100
573 ms 4480 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);
    m32 cur(1);
    for (auto j: range(1, std::min(N, len[i]) + 1)) {
      cur *= m32(len[i] - j + 1);
      coeff[i][j] = cur * fact.fact_inv[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 145 ms 2424 KB Output is correct
2 Correct 147 ms 2424 KB Output is correct
3 Correct 147 ms 2432 KB Output is correct
4 Correct 153 ms 2424 KB Output is correct
5 Correct 148 ms 2424 KB Output is correct
6 Correct 144 ms 2432 KB Output is correct
7 Correct 145 ms 2424 KB Output is correct
8 Correct 147 ms 2428 KB Output is correct
9 Correct 148 ms 2424 KB Output is correct
10 Correct 147 ms 2424 KB Output is correct
11 Correct 149 ms 2424 KB Output is correct
12 Correct 148 ms 2432 KB Output is correct
13 Correct 148 ms 2432 KB Output is correct
14 Correct 149 ms 2424 KB Output is correct
15 Correct 147 ms 2424 KB Output is correct
16 Correct 20 ms 640 KB Output is correct
17 Correct 21 ms 640 KB Output is correct
18 Correct 23 ms 764 KB Output is correct
19 Correct 21 ms 768 KB Output is correct
20 Correct 24 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 2424 KB Output is correct
2 Correct 147 ms 2424 KB Output is correct
3 Correct 147 ms 2432 KB Output is correct
4 Correct 153 ms 2424 KB Output is correct
5 Correct 148 ms 2424 KB Output is correct
6 Correct 144 ms 2432 KB Output is correct
7 Correct 145 ms 2424 KB Output is correct
8 Correct 147 ms 2428 KB Output is correct
9 Correct 148 ms 2424 KB Output is correct
10 Correct 147 ms 2424 KB Output is correct
11 Correct 149 ms 2424 KB Output is correct
12 Correct 148 ms 2432 KB Output is correct
13 Correct 148 ms 2432 KB Output is correct
14 Correct 149 ms 2424 KB Output is correct
15 Correct 147 ms 2424 KB Output is correct
16 Correct 20 ms 640 KB Output is correct
17 Correct 21 ms 640 KB Output is correct
18 Correct 23 ms 764 KB Output is correct
19 Correct 21 ms 768 KB Output is correct
20 Correct 24 ms 640 KB Output is correct
21 Correct 9 ms 512 KB Output is correct
22 Correct 10 ms 512 KB Output is correct
23 Correct 8 ms 384 KB Output is correct
24 Correct 9 ms 384 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 8 ms 384 KB Output is correct
27 Correct 8 ms 384 KB Output is correct
28 Correct 7 ms 384 KB Output is correct
29 Correct 8 ms 512 KB Output is correct
30 Correct 155 ms 2244 KB Output is correct
31 Correct 135 ms 2176 KB Output is correct
32 Correct 140 ms 2296 KB Output is correct
33 Correct 148 ms 2296 KB Output is correct
34 Correct 134 ms 2176 KB Output is correct
35 Correct 131 ms 2176 KB Output is correct
36 Correct 129 ms 2252 KB Output is correct
37 Correct 135 ms 2296 KB Output is correct
38 Correct 138 ms 2296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 8 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 544 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 2424 KB Output is correct
2 Correct 147 ms 2424 KB Output is correct
3 Correct 147 ms 2432 KB Output is correct
4 Correct 153 ms 2424 KB Output is correct
5 Correct 148 ms 2424 KB Output is correct
6 Correct 144 ms 2432 KB Output is correct
7 Correct 145 ms 2424 KB Output is correct
8 Correct 147 ms 2428 KB Output is correct
9 Correct 148 ms 2424 KB Output is correct
10 Correct 147 ms 2424 KB Output is correct
11 Correct 149 ms 2424 KB Output is correct
12 Correct 148 ms 2432 KB Output is correct
13 Correct 148 ms 2432 KB Output is correct
14 Correct 149 ms 2424 KB Output is correct
15 Correct 147 ms 2424 KB Output is correct
16 Correct 20 ms 640 KB Output is correct
17 Correct 21 ms 640 KB Output is correct
18 Correct 23 ms 764 KB Output is correct
19 Correct 21 ms 768 KB Output is correct
20 Correct 24 ms 640 KB Output is correct
21 Correct 9 ms 512 KB Output is correct
22 Correct 10 ms 512 KB Output is correct
23 Correct 8 ms 384 KB Output is correct
24 Correct 9 ms 384 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 8 ms 384 KB Output is correct
27 Correct 8 ms 384 KB Output is correct
28 Correct 7 ms 384 KB Output is correct
29 Correct 8 ms 512 KB Output is correct
30 Correct 155 ms 2244 KB Output is correct
31 Correct 135 ms 2176 KB Output is correct
32 Correct 140 ms 2296 KB Output is correct
33 Correct 148 ms 2296 KB Output is correct
34 Correct 134 ms 2176 KB Output is correct
35 Correct 131 ms 2176 KB Output is correct
36 Correct 129 ms 2252 KB Output is correct
37 Correct 135 ms 2296 KB Output is correct
38 Correct 138 ms 2296 KB Output is correct
39 Correct 5 ms 512 KB Output is correct
40 Correct 5 ms 512 KB Output is correct
41 Correct 5 ms 512 KB Output is correct
42 Correct 5 ms 512 KB Output is correct
43 Correct 5 ms 512 KB Output is correct
44 Correct 5 ms 512 KB Output is correct
45 Correct 5 ms 512 KB Output is correct
46 Correct 8 ms 512 KB Output is correct
47 Correct 5 ms 512 KB Output is correct
48 Correct 5 ms 512 KB Output is correct
49 Correct 5 ms 512 KB Output is correct
50 Correct 5 ms 544 KB Output is correct
51 Correct 5 ms 512 KB Output is correct
52 Correct 5 ms 512 KB Output is correct
53 Correct 5 ms 512 KB Output is correct
54 Correct 2 ms 384 KB Output is correct
55 Correct 2 ms 384 KB Output is correct
56 Correct 3 ms 384 KB Output is correct
57 Correct 2 ms 384 KB Output is correct
58 Correct 2 ms 384 KB Output is correct
59 Correct 499 ms 4356 KB Output is correct
60 Correct 477 ms 4352 KB Output is correct
61 Correct 466 ms 4472 KB Output is correct
62 Correct 543 ms 4352 KB Output is correct
63 Correct 469 ms 4480 KB Output is correct
64 Correct 571 ms 4472 KB Output is correct
65 Correct 573 ms 4472 KB Output is correct
66 Correct 561 ms 4356 KB Output is correct
67 Correct 554 ms 4360 KB Output is correct
68 Correct 571 ms 4472 KB Output is correct
69 Correct 472 ms 4472 KB Output is correct
70 Correct 459 ms 4360 KB Output is correct
71 Correct 469 ms 4472 KB Output is correct
72 Correct 468 ms 4472 KB Output is correct
73 Correct 473 ms 4472 KB Output is correct
74 Correct 48 ms 768 KB Output is correct
75 Correct 48 ms 768 KB Output is correct
76 Correct 48 ms 768 KB Output is correct
77 Correct 49 ms 768 KB Output is correct
78 Correct 48 ms 768 KB Output is correct