Submission #558874

# Submission time Handle Problem Language Result Execution time Memory
558874 2022-05-08T22:30:28 Z Sweezy Boat (APIO16_boat) C++17
100 / 100
1008 ms 13380 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())
 
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]];
  }

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

Compilation message

boat.cpp: In function 'void solve()':
boat.cpp:240:11: warning: unused variable 'len' [-Wunused-variable]
  240 |       int len = r[seg] - l[seg] + 1;
      |           ^~~
boat.cpp:242:11: warning: unused variable 'prv' [-Wunused-variable]
  242 |       int prv = n;
      |           ^~~
# Verdict Execution time Memory Grader output
1 Correct 280 ms 7244 KB Output is correct
2 Correct 287 ms 7244 KB Output is correct
3 Correct 258 ms 7244 KB Output is correct
4 Correct 261 ms 7280 KB Output is correct
5 Correct 273 ms 7372 KB Output is correct
6 Correct 255 ms 7284 KB Output is correct
7 Correct 268 ms 7292 KB Output is correct
8 Correct 262 ms 7284 KB Output is correct
9 Correct 289 ms 7284 KB Output is correct
10 Correct 265 ms 7280 KB Output is correct
11 Correct 270 ms 7280 KB Output is correct
12 Correct 270 ms 7280 KB Output is correct
13 Correct 271 ms 7280 KB Output is correct
14 Correct 282 ms 7284 KB Output is correct
15 Correct 286 ms 7280 KB Output is correct
16 Correct 48 ms 2304 KB Output is correct
17 Correct 52 ms 2476 KB Output is correct
18 Correct 55 ms 2332 KB Output is correct
19 Correct 57 ms 2472 KB Output is correct
20 Correct 53 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 280 ms 7244 KB Output is correct
2 Correct 287 ms 7244 KB Output is correct
3 Correct 258 ms 7244 KB Output is correct
4 Correct 261 ms 7280 KB Output is correct
5 Correct 273 ms 7372 KB Output is correct
6 Correct 255 ms 7284 KB Output is correct
7 Correct 268 ms 7292 KB Output is correct
8 Correct 262 ms 7284 KB Output is correct
9 Correct 289 ms 7284 KB Output is correct
10 Correct 265 ms 7280 KB Output is correct
11 Correct 270 ms 7280 KB Output is correct
12 Correct 270 ms 7280 KB Output is correct
13 Correct 271 ms 7280 KB Output is correct
14 Correct 282 ms 7284 KB Output is correct
15 Correct 286 ms 7280 KB Output is correct
16 Correct 48 ms 2304 KB Output is correct
17 Correct 52 ms 2476 KB Output is correct
18 Correct 55 ms 2332 KB Output is correct
19 Correct 57 ms 2472 KB Output is correct
20 Correct 53 ms 2380 KB Output is correct
21 Correct 213 ms 11372 KB Output is correct
22 Correct 221 ms 11456 KB Output is correct
23 Correct 198 ms 11220 KB Output is correct
24 Correct 217 ms 11412 KB Output is correct
25 Correct 218 ms 11220 KB Output is correct
26 Correct 337 ms 10744 KB Output is correct
27 Correct 341 ms 10836 KB Output is correct
28 Correct 355 ms 10820 KB Output is correct
29 Correct 354 ms 10956 KB Output is correct
30 Correct 267 ms 12404 KB Output is correct
31 Correct 259 ms 12364 KB Output is correct
32 Correct 266 ms 12360 KB Output is correct
33 Correct 268 ms 12392 KB Output is correct
34 Correct 266 ms 12352 KB Output is correct
35 Correct 245 ms 7280 KB Output is correct
36 Correct 249 ms 7280 KB Output is correct
37 Correct 274 ms 7280 KB Output is correct
38 Correct 240 ms 7272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 852 KB Output is correct
2 Correct 8 ms 852 KB Output is correct
3 Correct 7 ms 852 KB Output is correct
4 Correct 8 ms 852 KB Output is correct
5 Correct 10 ms 820 KB Output is correct
6 Correct 10 ms 852 KB Output is correct
7 Correct 12 ms 852 KB Output is correct
8 Correct 10 ms 852 KB Output is correct
9 Correct 9 ms 856 KB Output is correct
10 Correct 9 ms 852 KB Output is correct
11 Correct 7 ms 852 KB Output is correct
12 Correct 7 ms 852 KB Output is correct
13 Correct 8 ms 772 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 600 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 4 ms 596 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 280 ms 7244 KB Output is correct
2 Correct 287 ms 7244 KB Output is correct
3 Correct 258 ms 7244 KB Output is correct
4 Correct 261 ms 7280 KB Output is correct
5 Correct 273 ms 7372 KB Output is correct
6 Correct 255 ms 7284 KB Output is correct
7 Correct 268 ms 7292 KB Output is correct
8 Correct 262 ms 7284 KB Output is correct
9 Correct 289 ms 7284 KB Output is correct
10 Correct 265 ms 7280 KB Output is correct
11 Correct 270 ms 7280 KB Output is correct
12 Correct 270 ms 7280 KB Output is correct
13 Correct 271 ms 7280 KB Output is correct
14 Correct 282 ms 7284 KB Output is correct
15 Correct 286 ms 7280 KB Output is correct
16 Correct 48 ms 2304 KB Output is correct
17 Correct 52 ms 2476 KB Output is correct
18 Correct 55 ms 2332 KB Output is correct
19 Correct 57 ms 2472 KB Output is correct
20 Correct 53 ms 2380 KB Output is correct
21 Correct 213 ms 11372 KB Output is correct
22 Correct 221 ms 11456 KB Output is correct
23 Correct 198 ms 11220 KB Output is correct
24 Correct 217 ms 11412 KB Output is correct
25 Correct 218 ms 11220 KB Output is correct
26 Correct 337 ms 10744 KB Output is correct
27 Correct 341 ms 10836 KB Output is correct
28 Correct 355 ms 10820 KB Output is correct
29 Correct 354 ms 10956 KB Output is correct
30 Correct 267 ms 12404 KB Output is correct
31 Correct 259 ms 12364 KB Output is correct
32 Correct 266 ms 12360 KB Output is correct
33 Correct 268 ms 12392 KB Output is correct
34 Correct 266 ms 12352 KB Output is correct
35 Correct 245 ms 7280 KB Output is correct
36 Correct 249 ms 7280 KB Output is correct
37 Correct 274 ms 7280 KB Output is correct
38 Correct 240 ms 7272 KB Output is correct
39 Correct 7 ms 852 KB Output is correct
40 Correct 8 ms 852 KB Output is correct
41 Correct 7 ms 852 KB Output is correct
42 Correct 8 ms 852 KB Output is correct
43 Correct 10 ms 820 KB Output is correct
44 Correct 10 ms 852 KB Output is correct
45 Correct 12 ms 852 KB Output is correct
46 Correct 10 ms 852 KB Output is correct
47 Correct 9 ms 856 KB Output is correct
48 Correct 9 ms 852 KB Output is correct
49 Correct 7 ms 852 KB Output is correct
50 Correct 7 ms 852 KB Output is correct
51 Correct 8 ms 772 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 600 KB Output is correct
55 Correct 3 ms 596 KB Output is correct
56 Correct 4 ms 596 KB Output is correct
57 Correct 3 ms 596 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
59 Correct 808 ms 13324 KB Output is correct
60 Correct 770 ms 13228 KB Output is correct
61 Correct 792 ms 13324 KB Output is correct
62 Correct 806 ms 13328 KB Output is correct
63 Correct 826 ms 13324 KB Output is correct
64 Correct 1008 ms 13328 KB Output is correct
65 Correct 974 ms 13376 KB Output is correct
66 Correct 968 ms 13352 KB Output is correct
67 Correct 982 ms 13340 KB Output is correct
68 Correct 1000 ms 13380 KB Output is correct
69 Correct 723 ms 13340 KB Output is correct
70 Correct 721 ms 13244 KB Output is correct
71 Correct 733 ms 13340 KB Output is correct
72 Correct 764 ms 13252 KB Output is correct
73 Correct 755 ms 13332 KB Output is correct
74 Correct 86 ms 2556 KB Output is correct
75 Correct 93 ms 2536 KB Output is correct
76 Correct 89 ms 2536 KB Output is correct
77 Correct 83 ms 2544 KB Output is correct
78 Correct 88 ms 2560 KB Output is correct