#include <bits/stdc++.h>
#define nl '\n'
#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
template <int md>
struct Modular {
int v;
constexpr Modular() : v(0) {}
template <typename T>
static inline int normalize(const T& x) {
int res = -md <= x && x < md ? static_cast<int>(x) : static_cast<int>(x % md);
return res + (res < 0) * md;
}
static constexpr int mod() {
return md;
}
template <typename U>
Modular(const U& x) : v(normalize(x)) {}
const int& operator()() const { return v; }
template <typename U>
explicit operator U() const {
return static_cast<U>(v);
}
using M = Modular;
constexpr static inline M _raw(int x) {
static_assert(x >= 0 && x < md);
M res;
res.v = x;
return res;
}
template <typename U>
friend std::enable_if_t<std::is_integral_v<U>, M> power(M b, U e) {
assert(e >= 0);
M ans = 1;
while (e) {
if (e & 1) ans *= b;
b *= b;
e >>= 1;
}
return ans;
}
M inv() const {
M res = power(*this, md - 2);
return res;
}
M& operator+=(const M& y) { return v += y.v, v -= (v >= md) * md, *this; }
M& operator-=(const M& y) { return v -= y.v, v += (v < 0) * md, *this; }
M& operator*=(const M& y) { return v = (int64_t) v * y.v % md, *this; }
M& operator/=(const M& y) { return *this *= y.inv(); }
M& operator++() { return *this += _raw(1); }
M& operator--() { return *this -= _raw(1); }
M operator++(int) {
M res(*this);
return *this += _raw(1), res;
}
M operator--(int) {
M res(*this);
return *this -= _raw(1), res;
}
M operator-() const { return M(-v); }
friend bool operator==(const M& x, const M& y) { return x.v == y.v; }
friend bool operator<(const M& x, const M& y) { return x.v < y.v; }
friend bool operator>(const M& x, const M& y) { return x.v > y.v; }
friend bool operator<=(const M& x, const M& y) { return x.v <= y.v; }
friend bool operator>=(const M& x, const M& y) { return x.v >= y.v; }
friend bool operator!=(const M& x, const M& y) { return x.v != y.v; }
template <typename Istream>
friend Istream& operator>>(Istream& is, M& y) {
int64_t x;
is >> x;
y.v = y.normalize(x);
return is;
}
template <typename Ostream>
friend Ostream& operator<<(Ostream& os, const M& y) {
return os << y.v;
}
friend M operator+(const M& x, const M& y) { return M(x) += y; }
friend M operator-(const M& x, const M& y) { return M(x) -= y; }
friend M operator*(const M& x, const M& y) { return M(x) *= y; }
friend M operator/(const M& x, const M& y) { return M(x) /= y; }
};
constexpr int md = 1e9 + 7;
using Mint = Modular<md>;
template <typename M>
struct Comb {
std::vector<M> _fact, _finv;
Comb() : _fact(1, 1), _finv(1, 1) {}
inline int size() const { return static_cast<int>(_fact.size()); }
void _double_extend() {
int old_size = size();
int new_size = std::min(M::mod(), size() * 2);
_fact.resize(new_size);
_finv.resize(new_size);
for (int i = old_size; i < new_size; i++) {
_fact[i] = _fact[i - 1] * i;
}
_finv[new_size - 1] = _fact[new_size - 1].inv();
for (int i = new_size - 2; i >= old_size; i--) {
_finv[i] = _finv[i + 1] * (i + 1);
}
}
/**
* Calculate ***n** choose **k***.
*/
M operator()(int n, int k) {
if (k > n || k < 0) return M();
while (size() <= n) {
_double_extend();
}
return _fact[n] * _finv[n - k] * _finv[k];
}
M lucas(int64_t n, int64_t k) {
if (k > n) return 0;
if (n < md) return this->operator()(n, k);
return lucas(n / md, k / md) * this->operator()(n % md, k % md);
}
};
Comb<Mint> C;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; i++) std::cin >> a[i] >> b[i], b[i]++;
std::vector<int> vals(a.begin(), a.end());
vals.insert(vals.end(), b.begin(), b.end());
std::sort(vals.begin(), vals.end());
vals.erase(std::unique(vals.begin(), vals.end()), vals.end());
dbg(vals);
int num_segments = vals.size() - 1;
for (int i = 0; i < n; i++) {
a[i] = std::lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin();
b[i] = std::lower_bound(vals.begin(), vals.end(), b[i]) - vals.begin();
dbg(a[i], b[i]);
}
std::vector<std::vector<Mint>> choose(num_segments, std::vector<Mint>(n + 1));
std::vector<std::vector<Mint>> prepared(num_segments, std::vector<Mint>(n + 1));
for (int i = 0; i < num_segments; i++) {
int len = vals[i + 1] - vals[i];
choose[i][0] = 1;
for (int j = 0; j < n; j++) {
choose[i][j + 1] = choose[i][j] * (len - j) / (j + 1);
}
for (int cnt = 1; cnt <= n; cnt++) {
prepared[i][cnt] = 0;
for (int j = 0; j < cnt; j++) {
prepared[i][cnt] += C(cnt - 1, j) * choose[i][j + 1];
}
}
}
std::vector<std::vector<Mint>> dp(n + 1, std::vector<Mint>(num_segments + 1));
std::vector<std::vector<Mint>> ff(n + 1, std::vector<Mint>(num_segments + 1));
dp[n].back() = 1;
for (int j = 0; j <= num_segments; j++) {
ff[n][j] = 1;
}
Mint ans = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = num_segments - 1; j >= 0; j--) {
if (a[i] <= j && j < b[i]) {
int cnt = 0;
for (int k = i; k < n; k++) {
cnt += a[k] <= j && j < b[k];
Mint mul = prepared[j][cnt];
dbg(i, j, k, cnt, mul, ff[k + 1][j + 1], mul * ff[k + 1][j + 1]);
dp[i][j] += ff[k + 1][j + 1] * mul;
}
}
ans += dp[i][j];
dbg(i, j, dp[i][j]);
ff[i][j] = ff[i][j + 1] + dp[i][j];
}
}
std::cout << ans << nl;
// 4
// 4 7
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1036 ms |
8276 KB |
Output is correct |
2 |
Correct |
1074 ms |
8272 KB |
Output is correct |
3 |
Correct |
1065 ms |
8276 KB |
Output is correct |
4 |
Correct |
1052 ms |
8272 KB |
Output is correct |
5 |
Correct |
1035 ms |
8276 KB |
Output is correct |
6 |
Correct |
1034 ms |
8272 KB |
Output is correct |
7 |
Correct |
1029 ms |
8272 KB |
Output is correct |
8 |
Correct |
1031 ms |
8272 KB |
Output is correct |
9 |
Correct |
1043 ms |
8280 KB |
Output is correct |
10 |
Correct |
1039 ms |
8272 KB |
Output is correct |
11 |
Correct |
1067 ms |
8272 KB |
Output is correct |
12 |
Correct |
1045 ms |
8272 KB |
Output is correct |
13 |
Correct |
1072 ms |
8276 KB |
Output is correct |
14 |
Correct |
1060 ms |
8296 KB |
Output is correct |
15 |
Correct |
1054 ms |
8272 KB |
Output is correct |
16 |
Correct |
181 ms |
1876 KB |
Output is correct |
17 |
Correct |
207 ms |
1964 KB |
Output is correct |
18 |
Correct |
187 ms |
2128 KB |
Output is correct |
19 |
Correct |
206 ms |
1724 KB |
Output is correct |
20 |
Correct |
199 ms |
1692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1036 ms |
8276 KB |
Output is correct |
2 |
Correct |
1074 ms |
8272 KB |
Output is correct |
3 |
Correct |
1065 ms |
8276 KB |
Output is correct |
4 |
Correct |
1052 ms |
8272 KB |
Output is correct |
5 |
Correct |
1035 ms |
8276 KB |
Output is correct |
6 |
Correct |
1034 ms |
8272 KB |
Output is correct |
7 |
Correct |
1029 ms |
8272 KB |
Output is correct |
8 |
Correct |
1031 ms |
8272 KB |
Output is correct |
9 |
Correct |
1043 ms |
8280 KB |
Output is correct |
10 |
Correct |
1039 ms |
8272 KB |
Output is correct |
11 |
Correct |
1067 ms |
8272 KB |
Output is correct |
12 |
Correct |
1045 ms |
8272 KB |
Output is correct |
13 |
Correct |
1072 ms |
8276 KB |
Output is correct |
14 |
Correct |
1060 ms |
8296 KB |
Output is correct |
15 |
Correct |
1054 ms |
8272 KB |
Output is correct |
16 |
Correct |
181 ms |
1876 KB |
Output is correct |
17 |
Correct |
207 ms |
1964 KB |
Output is correct |
18 |
Correct |
187 ms |
2128 KB |
Output is correct |
19 |
Correct |
206 ms |
1724 KB |
Output is correct |
20 |
Correct |
199 ms |
1692 KB |
Output is correct |
21 |
Correct |
1074 ms |
7700 KB |
Output is correct |
22 |
Correct |
1115 ms |
7808 KB |
Output is correct |
23 |
Correct |
1101 ms |
7916 KB |
Output is correct |
24 |
Correct |
1061 ms |
7508 KB |
Output is correct |
25 |
Correct |
1089 ms |
7760 KB |
Output is correct |
26 |
Correct |
1129 ms |
7456 KB |
Output is correct |
27 |
Correct |
1117 ms |
7560 KB |
Output is correct |
28 |
Correct |
1116 ms |
7508 KB |
Output is correct |
29 |
Correct |
1118 ms |
7428 KB |
Output is correct |
30 |
Correct |
1057 ms |
8532 KB |
Output is correct |
31 |
Correct |
1055 ms |
8280 KB |
Output is correct |
32 |
Correct |
1046 ms |
8532 KB |
Output is correct |
33 |
Correct |
1057 ms |
8280 KB |
Output is correct |
34 |
Correct |
1069 ms |
8268 KB |
Output is correct |
35 |
Correct |
1027 ms |
8532 KB |
Output is correct |
36 |
Correct |
1053 ms |
8276 KB |
Output is correct |
37 |
Correct |
1051 ms |
8276 KB |
Output is correct |
38 |
Correct |
1060 ms |
8268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
600 KB |
Output is correct |
2 |
Correct |
13 ms |
800 KB |
Output is correct |
3 |
Correct |
13 ms |
604 KB |
Output is correct |
4 |
Correct |
14 ms |
604 KB |
Output is correct |
5 |
Correct |
13 ms |
604 KB |
Output is correct |
6 |
Correct |
14 ms |
604 KB |
Output is correct |
7 |
Correct |
13 ms |
632 KB |
Output is correct |
8 |
Correct |
13 ms |
604 KB |
Output is correct |
9 |
Correct |
13 ms |
632 KB |
Output is correct |
10 |
Correct |
13 ms |
604 KB |
Output is correct |
11 |
Correct |
13 ms |
636 KB |
Output is correct |
12 |
Correct |
13 ms |
604 KB |
Output is correct |
13 |
Correct |
13 ms |
604 KB |
Output is correct |
14 |
Correct |
13 ms |
604 KB |
Output is correct |
15 |
Correct |
14 ms |
604 KB |
Output is correct |
16 |
Correct |
9 ms |
660 KB |
Output is correct |
17 |
Correct |
7 ms |
600 KB |
Output is correct |
18 |
Correct |
7 ms |
600 KB |
Output is correct |
19 |
Correct |
7 ms |
604 KB |
Output is correct |
20 |
Correct |
7 ms |
552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1036 ms |
8276 KB |
Output is correct |
2 |
Correct |
1074 ms |
8272 KB |
Output is correct |
3 |
Correct |
1065 ms |
8276 KB |
Output is correct |
4 |
Correct |
1052 ms |
8272 KB |
Output is correct |
5 |
Correct |
1035 ms |
8276 KB |
Output is correct |
6 |
Correct |
1034 ms |
8272 KB |
Output is correct |
7 |
Correct |
1029 ms |
8272 KB |
Output is correct |
8 |
Correct |
1031 ms |
8272 KB |
Output is correct |
9 |
Correct |
1043 ms |
8280 KB |
Output is correct |
10 |
Correct |
1039 ms |
8272 KB |
Output is correct |
11 |
Correct |
1067 ms |
8272 KB |
Output is correct |
12 |
Correct |
1045 ms |
8272 KB |
Output is correct |
13 |
Correct |
1072 ms |
8276 KB |
Output is correct |
14 |
Correct |
1060 ms |
8296 KB |
Output is correct |
15 |
Correct |
1054 ms |
8272 KB |
Output is correct |
16 |
Correct |
181 ms |
1876 KB |
Output is correct |
17 |
Correct |
207 ms |
1964 KB |
Output is correct |
18 |
Correct |
187 ms |
2128 KB |
Output is correct |
19 |
Correct |
206 ms |
1724 KB |
Output is correct |
20 |
Correct |
199 ms |
1692 KB |
Output is correct |
21 |
Correct |
1074 ms |
7700 KB |
Output is correct |
22 |
Correct |
1115 ms |
7808 KB |
Output is correct |
23 |
Correct |
1101 ms |
7916 KB |
Output is correct |
24 |
Correct |
1061 ms |
7508 KB |
Output is correct |
25 |
Correct |
1089 ms |
7760 KB |
Output is correct |
26 |
Correct |
1129 ms |
7456 KB |
Output is correct |
27 |
Correct |
1117 ms |
7560 KB |
Output is correct |
28 |
Correct |
1116 ms |
7508 KB |
Output is correct |
29 |
Correct |
1118 ms |
7428 KB |
Output is correct |
30 |
Correct |
1057 ms |
8532 KB |
Output is correct |
31 |
Correct |
1055 ms |
8280 KB |
Output is correct |
32 |
Correct |
1046 ms |
8532 KB |
Output is correct |
33 |
Correct |
1057 ms |
8280 KB |
Output is correct |
34 |
Correct |
1069 ms |
8268 KB |
Output is correct |
35 |
Correct |
1027 ms |
8532 KB |
Output is correct |
36 |
Correct |
1053 ms |
8276 KB |
Output is correct |
37 |
Correct |
1051 ms |
8276 KB |
Output is correct |
38 |
Correct |
1060 ms |
8268 KB |
Output is correct |
39 |
Correct |
16 ms |
600 KB |
Output is correct |
40 |
Correct |
13 ms |
800 KB |
Output is correct |
41 |
Correct |
13 ms |
604 KB |
Output is correct |
42 |
Correct |
14 ms |
604 KB |
Output is correct |
43 |
Correct |
13 ms |
604 KB |
Output is correct |
44 |
Correct |
14 ms |
604 KB |
Output is correct |
45 |
Correct |
13 ms |
632 KB |
Output is correct |
46 |
Correct |
13 ms |
604 KB |
Output is correct |
47 |
Correct |
13 ms |
632 KB |
Output is correct |
48 |
Correct |
13 ms |
604 KB |
Output is correct |
49 |
Correct |
13 ms |
636 KB |
Output is correct |
50 |
Correct |
13 ms |
604 KB |
Output is correct |
51 |
Correct |
13 ms |
604 KB |
Output is correct |
52 |
Correct |
13 ms |
604 KB |
Output is correct |
53 |
Correct |
14 ms |
604 KB |
Output is correct |
54 |
Correct |
9 ms |
660 KB |
Output is correct |
55 |
Correct |
7 ms |
600 KB |
Output is correct |
56 |
Correct |
7 ms |
600 KB |
Output is correct |
57 |
Correct |
7 ms |
604 KB |
Output is correct |
58 |
Correct |
7 ms |
552 KB |
Output is correct |
59 |
Correct |
1194 ms |
8528 KB |
Output is correct |
60 |
Correct |
1208 ms |
8656 KB |
Output is correct |
61 |
Correct |
1198 ms |
8288 KB |
Output is correct |
62 |
Correct |
1294 ms |
8292 KB |
Output is correct |
63 |
Correct |
1202 ms |
8412 KB |
Output is correct |
64 |
Correct |
1301 ms |
8416 KB |
Output is correct |
65 |
Correct |
1336 ms |
8288 KB |
Output is correct |
66 |
Correct |
1298 ms |
8284 KB |
Output is correct |
67 |
Correct |
1363 ms |
8288 KB |
Output is correct |
68 |
Correct |
1279 ms |
8288 KB |
Output is correct |
69 |
Correct |
1206 ms |
8284 KB |
Output is correct |
70 |
Correct |
1188 ms |
8288 KB |
Output is correct |
71 |
Correct |
1208 ms |
8288 KB |
Output is correct |
72 |
Correct |
1205 ms |
8292 KB |
Output is correct |
73 |
Correct |
1217 ms |
8308 KB |
Output is correct |
74 |
Correct |
212 ms |
1924 KB |
Output is correct |
75 |
Correct |
229 ms |
1712 KB |
Output is correct |
76 |
Correct |
228 ms |
1876 KB |
Output is correct |
77 |
Correct |
241 ms |
1692 KB |
Output is correct |
78 |
Correct |
221 ms |
1944 KB |
Output is correct |