Submission #558876

# Submission time Handle Problem Language Result Execution time Memory
558876 2022-05-08T22:35:00 Z Sweezy Boat (APIO16_boat) C++17
100 / 100
970 ms 13376 KB
#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>>;

#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())

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]];
  }

  int segs = sz(l);

  vector<vector<Mint>> choose(n + 1, vector<Mint> (n + 1));
  choose[0][0] = 1;
  reps(i, 1, n + 1) {
    choose[i][0] = 1;
    reps(j, 1, n + 1) {
      choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j];
    }
  }

  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;
    reps(cnt, 2, n + 1) {
      reps(take, 2, min(len, cnt) + 1) {
        ways[seg][cnt] += C[seg][take] * choose[cnt - 2][take - 2];
      }
    }
  }

  Mint answer = 0;
  vector<vector<Mint>> dp(n, vector<Mint> (segs));
  rep(i, n) {
    reps(seg, a[i], b[i] + 1) {
      int cnt = 0;
      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];
        }
      }
    }
    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);
    }
  }

  cout << answer;
}
 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 262 ms 7280 KB Output is correct
2 Correct 261 ms 7276 KB Output is correct
3 Correct 262 ms 7276 KB Output is correct
4 Correct 257 ms 7280 KB Output is correct
5 Correct 264 ms 7284 KB Output is correct
6 Correct 263 ms 7332 KB Output is correct
7 Correct 266 ms 7300 KB Output is correct
8 Correct 254 ms 7276 KB Output is correct
9 Correct 262 ms 7292 KB Output is correct
10 Correct 266 ms 7344 KB Output is correct
11 Correct 274 ms 7284 KB Output is correct
12 Correct 257 ms 7296 KB Output is correct
13 Correct 260 ms 7280 KB Output is correct
14 Correct 253 ms 7276 KB Output is correct
15 Correct 258 ms 7292 KB Output is correct
16 Correct 47 ms 2300 KB Output is correct
17 Correct 51 ms 2380 KB Output is correct
18 Correct 48 ms 2324 KB Output is correct
19 Correct 50 ms 2380 KB Output is correct
20 Correct 51 ms 2324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 7280 KB Output is correct
2 Correct 261 ms 7276 KB Output is correct
3 Correct 262 ms 7276 KB Output is correct
4 Correct 257 ms 7280 KB Output is correct
5 Correct 264 ms 7284 KB Output is correct
6 Correct 263 ms 7332 KB Output is correct
7 Correct 266 ms 7300 KB Output is correct
8 Correct 254 ms 7276 KB Output is correct
9 Correct 262 ms 7292 KB Output is correct
10 Correct 266 ms 7344 KB Output is correct
11 Correct 274 ms 7284 KB Output is correct
12 Correct 257 ms 7296 KB Output is correct
13 Correct 260 ms 7280 KB Output is correct
14 Correct 253 ms 7276 KB Output is correct
15 Correct 258 ms 7292 KB Output is correct
16 Correct 47 ms 2300 KB Output is correct
17 Correct 51 ms 2380 KB Output is correct
18 Correct 48 ms 2324 KB Output is correct
19 Correct 50 ms 2380 KB Output is correct
20 Correct 51 ms 2324 KB Output is correct
21 Correct 212 ms 11348 KB Output is correct
22 Correct 205 ms 11416 KB Output is correct
23 Correct 185 ms 11224 KB Output is correct
24 Correct 213 ms 11348 KB Output is correct
25 Correct 203 ms 11216 KB Output is correct
26 Correct 332 ms 10744 KB Output is correct
27 Correct 335 ms 10836 KB Output is correct
28 Correct 333 ms 10820 KB Output is correct
29 Correct 332 ms 10852 KB Output is correct
30 Correct 251 ms 12420 KB Output is correct
31 Correct 249 ms 12360 KB Output is correct
32 Correct 252 ms 12360 KB Output is correct
33 Correct 263 ms 12388 KB Output is correct
34 Correct 257 ms 12392 KB Output is correct
35 Correct 238 ms 7292 KB Output is correct
36 Correct 246 ms 7280 KB Output is correct
37 Correct 255 ms 7280 KB Output is correct
38 Correct 254 ms 7272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 852 KB Output is correct
2 Correct 7 ms 852 KB Output is correct
3 Correct 9 ms 852 KB Output is correct
4 Correct 8 ms 852 KB Output is correct
5 Correct 7 ms 852 KB Output is correct
6 Correct 9 ms 852 KB Output is correct
7 Correct 9 ms 852 KB Output is correct
8 Correct 8 ms 852 KB Output is correct
9 Correct 9 ms 852 KB Output is correct
10 Correct 9 ms 852 KB Output is correct
11 Correct 8 ms 852 KB Output is correct
12 Correct 7 ms 852 KB Output is correct
13 Correct 7 ms 852 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 8 ms 852 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 4 ms 596 KB Output is correct
18 Correct 4 ms 468 KB Output is correct
19 Correct 3 ms 596 KB Output is correct
20 Correct 4 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 7280 KB Output is correct
2 Correct 261 ms 7276 KB Output is correct
3 Correct 262 ms 7276 KB Output is correct
4 Correct 257 ms 7280 KB Output is correct
5 Correct 264 ms 7284 KB Output is correct
6 Correct 263 ms 7332 KB Output is correct
7 Correct 266 ms 7300 KB Output is correct
8 Correct 254 ms 7276 KB Output is correct
9 Correct 262 ms 7292 KB Output is correct
10 Correct 266 ms 7344 KB Output is correct
11 Correct 274 ms 7284 KB Output is correct
12 Correct 257 ms 7296 KB Output is correct
13 Correct 260 ms 7280 KB Output is correct
14 Correct 253 ms 7276 KB Output is correct
15 Correct 258 ms 7292 KB Output is correct
16 Correct 47 ms 2300 KB Output is correct
17 Correct 51 ms 2380 KB Output is correct
18 Correct 48 ms 2324 KB Output is correct
19 Correct 50 ms 2380 KB Output is correct
20 Correct 51 ms 2324 KB Output is correct
21 Correct 212 ms 11348 KB Output is correct
22 Correct 205 ms 11416 KB Output is correct
23 Correct 185 ms 11224 KB Output is correct
24 Correct 213 ms 11348 KB Output is correct
25 Correct 203 ms 11216 KB Output is correct
26 Correct 332 ms 10744 KB Output is correct
27 Correct 335 ms 10836 KB Output is correct
28 Correct 333 ms 10820 KB Output is correct
29 Correct 332 ms 10852 KB Output is correct
30 Correct 251 ms 12420 KB Output is correct
31 Correct 249 ms 12360 KB Output is correct
32 Correct 252 ms 12360 KB Output is correct
33 Correct 263 ms 12388 KB Output is correct
34 Correct 257 ms 12392 KB Output is correct
35 Correct 238 ms 7292 KB Output is correct
36 Correct 246 ms 7280 KB Output is correct
37 Correct 255 ms 7280 KB Output is correct
38 Correct 254 ms 7272 KB Output is correct
39 Correct 7 ms 852 KB Output is correct
40 Correct 7 ms 852 KB Output is correct
41 Correct 9 ms 852 KB Output is correct
42 Correct 8 ms 852 KB Output is correct
43 Correct 7 ms 852 KB Output is correct
44 Correct 9 ms 852 KB Output is correct
45 Correct 9 ms 852 KB Output is correct
46 Correct 8 ms 852 KB Output is correct
47 Correct 9 ms 852 KB Output is correct
48 Correct 9 ms 852 KB Output is correct
49 Correct 8 ms 852 KB Output is correct
50 Correct 7 ms 852 KB Output is correct
51 Correct 7 ms 852 KB Output is correct
52 Correct 7 ms 852 KB Output is correct
53 Correct 8 ms 852 KB Output is correct
54 Correct 3 ms 596 KB Output is correct
55 Correct 4 ms 596 KB Output is correct
56 Correct 4 ms 468 KB Output is correct
57 Correct 3 ms 596 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
59 Correct 743 ms 13324 KB Output is correct
60 Correct 725 ms 13360 KB Output is correct
61 Correct 712 ms 13376 KB Output is correct
62 Correct 737 ms 13352 KB Output is correct
63 Correct 753 ms 13260 KB Output is correct
64 Correct 949 ms 13332 KB Output is correct
65 Correct 953 ms 13232 KB Output is correct
66 Correct 970 ms 13360 KB Output is correct
67 Correct 946 ms 13320 KB Output is correct
68 Correct 953 ms 13324 KB Output is correct
69 Correct 699 ms 13320 KB Output is correct
70 Correct 704 ms 13368 KB Output is correct
71 Correct 725 ms 13320 KB Output is correct
72 Correct 701 ms 13328 KB Output is correct
73 Correct 706 ms 13364 KB Output is correct
74 Correct 86 ms 2540 KB Output is correct
75 Correct 83 ms 2520 KB Output is correct
76 Correct 83 ms 2520 KB Output is correct
77 Correct 87 ms 2532 KB Output is correct
78 Correct 89 ms 2528 KB Output is correct