Submission #558872

#TimeUsernameProblemLanguageResultExecution timeMemory
558872SweezyBoat (APIO16_boat)C++17
58 / 100
2021 ms12444 KiB
#include <bits/stdc++.h>
 
using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}
 
template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;
 
  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
    value = normalize(x);
  }
 
  template <typename U>
  static Type normalize(const U& x) {
    Type v;
    if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    else v = static_cast<Type>(x % mod());
    if (v < 0) v += mod();
    return v;
  }
 
  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }
 
  Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
  Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }
 
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
    uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
    uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (mod())
    );
    value = m;
#else
    value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
    long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }
 
  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
 
  friend const Type& abs(const Modular& x) { return x.value; }
 
  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
 
  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
 
  template <typename V, typename U>
  friend V& operator>>(V& stream, Modular<U>& number);
 
 private:
  Type value;
};
 
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
 
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
 
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
 
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
 
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
 
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
 
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
 
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1) res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}
 
template <typename T>
bool IsZero(const Modular<T>& number) {
  return number() == 0;
}
 
template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}
 
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
  return stream << number();
}
 
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, long long>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}
 
constexpr int md = 1000000007;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

vector<Mint> fact(1, 1);
vector<Mint> inv_fact(1, 1);
 
Mint choose(int n, int k) {
  if (k < 0 || k > n) {
    return 0;
  }
  while ((int) fact.size() < n + 1) {
    fact.push_back(fact.back() * (int) fact.size());
    inv_fact.push_back(1 / fact.back());
  }
  return fact[n] * inv_fact[k] * inv_fact[n - k];
}

#define int long long
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, s, n) for (int i = s; i < (n); ++i)
#define pb push_back
#define sz(a) (int) (a.size())
 
const int mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), cs;
  rep(i, n) {
    cin >> a[i] >> b[i];
    cs.pb(a[i]);
    cs.pb(b[i]);
  }
 
  sort(all(cs));
  cs.erase(unique(all(cs)), cs.end());
  map<int, int> mp;
  vector<int> l, r;
  rep(i, sz(cs)) {
    if ((i == 0 ? 0 : cs[i - 1]) + 1 <= cs[i] - 1) {
      l.pb((i == 0 ? 0 : cs[i - 1]) + 1);
      r.pb(cs[i] - 1);
    }
    mp[cs[i]] = sz(l);
    l.pb(cs[i]);
    r.pb(cs[i]);
  }
  rep(i, n) {
    a[i] = mp[a[i]];
    b[i] = mp[b[i]];
    // debug(a[i], b[i]);
  }

  // debug(l);
  // debug(r);

  int segs = sz(l);
  vector<vector<Mint>> C(segs, vector<Mint> (n + 1));
  rep(seg, segs) {
    C[seg][0] = 1;
    int len = r[seg] - l[seg] + 1;
    reps(j, 1, min(n, len) + 1) {
      C[seg][j] = C[seg][j - 1] * Mint(len - j + 1) / Mint(j);
    }
  }

  vector<vector<Mint>> ways(segs, vector<Mint> (n + 1));
  rep(seg, segs) {
    int len = r[seg] - l[seg] + 1;
    ways[seg][1] = len;
    // debug(seg);
    reps(cnt, 2, n + 1) {
      reps(take, 2, min(len, cnt) + 1) {
        ways[seg][cnt] += C[seg][take] * choose(cnt - 2, take - 2);
      }
    }
    // debug(ways[seg]);
  }

  // [i, j, x, cnt, dp[i][x]]: 1 1 1 1 1
  // [i, j, x, cnt, dp[i][x]]: 1 1 2 1 2
  // [i, j, x, cnt, dp[i][x]]: 1 1 3 1 1
  // [i, j, x, cnt, dp[i][x]]: 1 1 4 1 1
  // [i, j, x, cnt, dp[i][x]]: 2 2 1 1 1
  // [i, j, x, cnt, dp[i][x]]: 2 1 1 2 1
  // [i, j, x, cnt, dp[i][x]]: 2 2 2 1 4
  // [i, j, x, cnt, dp[i][x]]: 2 1 2 2 5
  // [i, j, x, cnt, dp[i][x]]: 2 2 3 1 4
  // [i, j, x, cnt, dp[i][x]]: 2 1 3 2 4
  // [i, j, x, cnt, dp[i][x]]: 3 3 1 1 1
  // [i, j, x, cnt, dp[i][x]]: 3 2 1 2 1
  // [i, j, x, cnt, dp[i][x]]: 3 1 1 3 1
  // [i, j, x, cnt, dp[i][x]]: 3 3 2 1 6
  // [i, j, x, cnt, dp[i][x]]: 3 2 2 2 8
  // [i, j, x, cnt, dp[i][x]]: 3 1 2 3 9
  // [i, j, x, cnt, dp[i][x]]: 3 3 3 1 10
  // [i, j, x, cnt, dp[i][x]]: 3 2 3 2 10
  // [i, j, x, cnt, dp[i][x]]: 3 1 3 3 10

  // [i]: 1
  // 0 1 2 1 1 
  // [i]: 2
  // 0 1 5 4 0 
  // [i]: 3
  // 0 1 9 10 0 

  Mint answer = 0;
  vector<vector<Mint>> dp(n, vector<Mint> (segs));
  rep(i, n) {
    reps(seg, a[i], b[i] + 1) {
      int len = r[seg] - l[seg] + 1;
      int cnt = 0;
      int prv = n;
      for (int j = i; j >= 0; j--) {
        if (a[j] <= seg && seg <= b[j]) {
          cnt++;
          Mint ways_prev = (j > 0 && seg > 0 ? dp[j - 1][seg - 1] : Mint(1));
          dp[i][seg] += ways_prev * ways[seg][cnt];
          // debug(i, j, seg, cnt, dp[i][seg], ways[seg][cnt], ways_prev);
          // debug(i, cnt, len, ways[seg][cnt]);
          // dp[i][seg] += ways_prev * ways[seg][cnt];
        }
      }
    }
    // debug(i, dp[i]);
    reps(j, 1, segs) {
      dp[i][j] += dp[i][j - 1];
    }
    answer += dp[i][b[i]];
    rep(j, segs) {
      dp[i][j] += (i ? dp[i - 1][j] : 1);
    }
  }

  // debug(dp);

  cout << answer;
}
 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

Compilation message (stderr)

boat.cpp: In function 'void solve()':
boat.cpp:277:11: warning: unused variable 'len' [-Wunused-variable]
  277 |       int len = r[seg] - l[seg] + 1;
      |           ^~~
boat.cpp:279:11: warning: unused variable 'prv' [-Wunused-variable]
  279 |       int prv = n;
      |           ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...