답안 #558872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
558872 2022-05-08T22:25:41 Z Sweezy Boat (APIO16_boat) C++17
58 / 100
2000 ms 12444 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>>;

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

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;
      |           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 841 ms 6280 KB Output is correct
2 Correct 843 ms 6284 KB Output is correct
3 Correct 853 ms 6284 KB Output is correct
4 Correct 854 ms 6280 KB Output is correct
5 Correct 851 ms 6280 KB Output is correct
6 Correct 851 ms 6288 KB Output is correct
7 Correct 844 ms 6296 KB Output is correct
8 Correct 842 ms 6288 KB Output is correct
9 Correct 841 ms 6288 KB Output is correct
10 Correct 830 ms 6348 KB Output is correct
11 Correct 833 ms 6284 KB Output is correct
12 Correct 841 ms 6284 KB Output is correct
13 Correct 837 ms 6280 KB Output is correct
14 Correct 857 ms 6280 KB Output is correct
15 Correct 841 ms 6220 KB Output is correct
16 Correct 153 ms 1296 KB Output is correct
17 Correct 161 ms 1476 KB Output is correct
18 Correct 156 ms 1328 KB Output is correct
19 Correct 165 ms 1476 KB Output is correct
20 Correct 157 ms 1340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 841 ms 6280 KB Output is correct
2 Correct 843 ms 6284 KB Output is correct
3 Correct 853 ms 6284 KB Output is correct
4 Correct 854 ms 6280 KB Output is correct
5 Correct 851 ms 6280 KB Output is correct
6 Correct 851 ms 6288 KB Output is correct
7 Correct 844 ms 6296 KB Output is correct
8 Correct 842 ms 6288 KB Output is correct
9 Correct 841 ms 6288 KB Output is correct
10 Correct 830 ms 6348 KB Output is correct
11 Correct 833 ms 6284 KB Output is correct
12 Correct 841 ms 6284 KB Output is correct
13 Correct 837 ms 6280 KB Output is correct
14 Correct 857 ms 6280 KB Output is correct
15 Correct 841 ms 6220 KB Output is correct
16 Correct 153 ms 1296 KB Output is correct
17 Correct 161 ms 1476 KB Output is correct
18 Correct 156 ms 1328 KB Output is correct
19 Correct 165 ms 1476 KB Output is correct
20 Correct 157 ms 1340 KB Output is correct
21 Correct 226 ms 10288 KB Output is correct
22 Correct 213 ms 10432 KB Output is correct
23 Correct 197 ms 10288 KB Output is correct
24 Correct 209 ms 10456 KB Output is correct
25 Correct 207 ms 10300 KB Output is correct
26 Correct 314 ms 9752 KB Output is correct
27 Correct 323 ms 9808 KB Output is correct
28 Correct 323 ms 9828 KB Output is correct
29 Correct 329 ms 9932 KB Output is correct
30 Correct 786 ms 11460 KB Output is correct
31 Correct 792 ms 11372 KB Output is correct
32 Correct 797 ms 11408 KB Output is correct
33 Correct 802 ms 11400 KB Output is correct
34 Correct 788 ms 11368 KB Output is correct
35 Correct 779 ms 6320 KB Output is correct
36 Correct 792 ms 6296 KB Output is correct
37 Correct 793 ms 6332 KB Output is correct
38 Correct 775 ms 6288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 724 KB Output is correct
2 Correct 19 ms 828 KB Output is correct
3 Correct 18 ms 724 KB Output is correct
4 Correct 19 ms 724 KB Output is correct
5 Correct 16 ms 820 KB Output is correct
6 Correct 19 ms 836 KB Output is correct
7 Correct 17 ms 832 KB Output is correct
8 Correct 21 ms 824 KB Output is correct
9 Correct 18 ms 724 KB Output is correct
10 Correct 18 ms 820 KB Output is correct
11 Correct 17 ms 820 KB Output is correct
12 Correct 17 ms 724 KB Output is correct
13 Correct 18 ms 812 KB Output is correct
14 Correct 16 ms 812 KB Output is correct
15 Correct 19 ms 724 KB Output is correct
16 Correct 8 ms 468 KB Output is correct
17 Correct 8 ms 468 KB Output is correct
18 Correct 9 ms 636 KB Output is correct
19 Correct 8 ms 472 KB Output is correct
20 Correct 8 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 841 ms 6280 KB Output is correct
2 Correct 843 ms 6284 KB Output is correct
3 Correct 853 ms 6284 KB Output is correct
4 Correct 854 ms 6280 KB Output is correct
5 Correct 851 ms 6280 KB Output is correct
6 Correct 851 ms 6288 KB Output is correct
7 Correct 844 ms 6296 KB Output is correct
8 Correct 842 ms 6288 KB Output is correct
9 Correct 841 ms 6288 KB Output is correct
10 Correct 830 ms 6348 KB Output is correct
11 Correct 833 ms 6284 KB Output is correct
12 Correct 841 ms 6284 KB Output is correct
13 Correct 837 ms 6280 KB Output is correct
14 Correct 857 ms 6280 KB Output is correct
15 Correct 841 ms 6220 KB Output is correct
16 Correct 153 ms 1296 KB Output is correct
17 Correct 161 ms 1476 KB Output is correct
18 Correct 156 ms 1328 KB Output is correct
19 Correct 165 ms 1476 KB Output is correct
20 Correct 157 ms 1340 KB Output is correct
21 Correct 226 ms 10288 KB Output is correct
22 Correct 213 ms 10432 KB Output is correct
23 Correct 197 ms 10288 KB Output is correct
24 Correct 209 ms 10456 KB Output is correct
25 Correct 207 ms 10300 KB Output is correct
26 Correct 314 ms 9752 KB Output is correct
27 Correct 323 ms 9808 KB Output is correct
28 Correct 323 ms 9828 KB Output is correct
29 Correct 329 ms 9932 KB Output is correct
30 Correct 786 ms 11460 KB Output is correct
31 Correct 792 ms 11372 KB Output is correct
32 Correct 797 ms 11408 KB Output is correct
33 Correct 802 ms 11400 KB Output is correct
34 Correct 788 ms 11368 KB Output is correct
35 Correct 779 ms 6320 KB Output is correct
36 Correct 792 ms 6296 KB Output is correct
37 Correct 793 ms 6332 KB Output is correct
38 Correct 775 ms 6288 KB Output is correct
39 Correct 16 ms 724 KB Output is correct
40 Correct 19 ms 828 KB Output is correct
41 Correct 18 ms 724 KB Output is correct
42 Correct 19 ms 724 KB Output is correct
43 Correct 16 ms 820 KB Output is correct
44 Correct 19 ms 836 KB Output is correct
45 Correct 17 ms 832 KB Output is correct
46 Correct 21 ms 824 KB Output is correct
47 Correct 18 ms 724 KB Output is correct
48 Correct 18 ms 820 KB Output is correct
49 Correct 17 ms 820 KB Output is correct
50 Correct 17 ms 724 KB Output is correct
51 Correct 18 ms 812 KB Output is correct
52 Correct 16 ms 812 KB Output is correct
53 Correct 19 ms 724 KB Output is correct
54 Correct 8 ms 468 KB Output is correct
55 Correct 8 ms 468 KB Output is correct
56 Correct 9 ms 636 KB Output is correct
57 Correct 8 ms 472 KB Output is correct
58 Correct 8 ms 492 KB Output is correct
59 Correct 1871 ms 12244 KB Output is correct
60 Correct 1849 ms 12348 KB Output is correct
61 Correct 1833 ms 12244 KB Output is correct
62 Correct 1884 ms 12304 KB Output is correct
63 Correct 1846 ms 12252 KB Output is correct
64 Execution timed out 2021 ms 12444 KB Time limit exceeded
65 Halted 0 ms 0 KB -