#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MOD = 1e9 + 7;
int add(int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int mul(long long a, int b) {
return a * b % MOD;
}
int binpow(int base, int power) {
int res = 1;
while (power > 0) {
if (power & 1) {
res = mul(res, base);
}
base = mul(base, base);
power >>= 1;
}
return res;
}
const int MAX_N = 500 + 5;
int dp[MAX_N][MAX_N * 2];
int c[MAX_N * 2][MAX_N];
int a[MAX_N];
int b[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> p;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
p.push_back(a[i]);
p.push_back(b[i] + 1);
}
sort(p.begin(), p.end());
p.resize(unique(p.begin(), p.end()) - p.begin());
vector<pair<int, int>> segments;
for (int i = 0; i < (int)p.size() - 1; ++i) {
for (int j = 1; j <= n; ++j) {
if (a[j] <= p[i] && p[i + 1] - 1 <= b[j]) {
segments.emplace_back(p[i], p[i + 1] - 1);
break;
}
}
}
int m = segments.size();
segments.insert(segments.begin(), {-1, -1});
for (int i = 1; i <= m; ++i) {
auto [l, r] = segments[i];
c[i][1] = r - l + 1;
for (int j = 2; j <= n; ++j) {
c[i][j] = mul(c[i][j - 1], mul(r - l + j, binpow(j, MOD - 2)));
}
}
for (int j = 0; j <= m; ++j) {
dp[0][j] = 1;
}
for (int i = 1; i <= n; ++i) {
dp[i][0] = 1;
for (int j = 1; j <= m; ++j) {
auto [l, r] = segments[j];
dp[i][j] = dp[i][j - 1];
int cnt = 0;
for (int k = i; k >= 1; --k) {
if (a[k] <= l && r <= b[k]) {
++cnt;
dp[i][j] = add(dp[i][j], mul(dp[k - 1][j - 1], c[j][cnt]));
}
}
}
}
cout << dp[n][m] - 1 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
3284 KB |
Output is correct |
2 |
Correct |
171 ms |
3236 KB |
Output is correct |
3 |
Correct |
173 ms |
3284 KB |
Output is correct |
4 |
Correct |
170 ms |
3336 KB |
Output is correct |
5 |
Correct |
172 ms |
3180 KB |
Output is correct |
6 |
Correct |
125 ms |
3224 KB |
Output is correct |
7 |
Correct |
123 ms |
3184 KB |
Output is correct |
8 |
Correct |
128 ms |
3344 KB |
Output is correct |
9 |
Correct |
125 ms |
3276 KB |
Output is correct |
10 |
Correct |
127 ms |
3276 KB |
Output is correct |
11 |
Correct |
121 ms |
3280 KB |
Output is correct |
12 |
Correct |
124 ms |
3172 KB |
Output is correct |
13 |
Correct |
124 ms |
3244 KB |
Output is correct |
14 |
Correct |
123 ms |
3320 KB |
Output is correct |
15 |
Correct |
119 ms |
3296 KB |
Output is correct |
16 |
Correct |
35 ms |
2380 KB |
Output is correct |
17 |
Correct |
36 ms |
2380 KB |
Output is correct |
18 |
Correct |
35 ms |
2412 KB |
Output is correct |
19 |
Correct |
39 ms |
2380 KB |
Output is correct |
20 |
Correct |
38 ms |
2480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
3284 KB |
Output is correct |
2 |
Correct |
171 ms |
3236 KB |
Output is correct |
3 |
Correct |
173 ms |
3284 KB |
Output is correct |
4 |
Correct |
170 ms |
3336 KB |
Output is correct |
5 |
Correct |
172 ms |
3180 KB |
Output is correct |
6 |
Correct |
125 ms |
3224 KB |
Output is correct |
7 |
Correct |
123 ms |
3184 KB |
Output is correct |
8 |
Correct |
128 ms |
3344 KB |
Output is correct |
9 |
Correct |
125 ms |
3276 KB |
Output is correct |
10 |
Correct |
127 ms |
3276 KB |
Output is correct |
11 |
Correct |
121 ms |
3280 KB |
Output is correct |
12 |
Correct |
124 ms |
3172 KB |
Output is correct |
13 |
Correct |
124 ms |
3244 KB |
Output is correct |
14 |
Correct |
123 ms |
3320 KB |
Output is correct |
15 |
Correct |
119 ms |
3296 KB |
Output is correct |
16 |
Correct |
35 ms |
2380 KB |
Output is correct |
17 |
Correct |
36 ms |
2380 KB |
Output is correct |
18 |
Correct |
35 ms |
2412 KB |
Output is correct |
19 |
Correct |
39 ms |
2380 KB |
Output is correct |
20 |
Correct |
38 ms |
2480 KB |
Output is correct |
21 |
Correct |
348 ms |
4044 KB |
Output is correct |
22 |
Correct |
351 ms |
4080 KB |
Output is correct |
23 |
Correct |
356 ms |
4024 KB |
Output is correct |
24 |
Correct |
338 ms |
3980 KB |
Output is correct |
25 |
Correct |
355 ms |
4132 KB |
Output is correct |
26 |
Correct |
347 ms |
4052 KB |
Output is correct |
27 |
Correct |
368 ms |
4172 KB |
Output is correct |
28 |
Correct |
345 ms |
3976 KB |
Output is correct |
29 |
Correct |
359 ms |
3968 KB |
Output is correct |
30 |
Correct |
245 ms |
4244 KB |
Output is correct |
31 |
Correct |
237 ms |
4224 KB |
Output is correct |
32 |
Correct |
257 ms |
4304 KB |
Output is correct |
33 |
Correct |
244 ms |
4176 KB |
Output is correct |
34 |
Correct |
238 ms |
4192 KB |
Output is correct |
35 |
Correct |
335 ms |
4300 KB |
Output is correct |
36 |
Correct |
329 ms |
4296 KB |
Output is correct |
37 |
Correct |
337 ms |
4248 KB |
Output is correct |
38 |
Correct |
325 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1108 KB |
Output is correct |
2 |
Correct |
7 ms |
1124 KB |
Output is correct |
3 |
Correct |
6 ms |
1068 KB |
Output is correct |
4 |
Correct |
7 ms |
1108 KB |
Output is correct |
5 |
Correct |
6 ms |
1108 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
7 ms |
1108 KB |
Output is correct |
8 |
Correct |
7 ms |
1108 KB |
Output is correct |
9 |
Correct |
7 ms |
1036 KB |
Output is correct |
10 |
Correct |
7 ms |
1108 KB |
Output is correct |
11 |
Correct |
6 ms |
1108 KB |
Output is correct |
12 |
Correct |
6 ms |
1040 KB |
Output is correct |
13 |
Correct |
7 ms |
1052 KB |
Output is correct |
14 |
Correct |
7 ms |
1116 KB |
Output is correct |
15 |
Correct |
6 ms |
1108 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
4 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
4 ms |
852 KB |
Output is correct |
20 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
3284 KB |
Output is correct |
2 |
Correct |
171 ms |
3236 KB |
Output is correct |
3 |
Correct |
173 ms |
3284 KB |
Output is correct |
4 |
Correct |
170 ms |
3336 KB |
Output is correct |
5 |
Correct |
172 ms |
3180 KB |
Output is correct |
6 |
Correct |
125 ms |
3224 KB |
Output is correct |
7 |
Correct |
123 ms |
3184 KB |
Output is correct |
8 |
Correct |
128 ms |
3344 KB |
Output is correct |
9 |
Correct |
125 ms |
3276 KB |
Output is correct |
10 |
Correct |
127 ms |
3276 KB |
Output is correct |
11 |
Correct |
121 ms |
3280 KB |
Output is correct |
12 |
Correct |
124 ms |
3172 KB |
Output is correct |
13 |
Correct |
124 ms |
3244 KB |
Output is correct |
14 |
Correct |
123 ms |
3320 KB |
Output is correct |
15 |
Correct |
119 ms |
3296 KB |
Output is correct |
16 |
Correct |
35 ms |
2380 KB |
Output is correct |
17 |
Correct |
36 ms |
2380 KB |
Output is correct |
18 |
Correct |
35 ms |
2412 KB |
Output is correct |
19 |
Correct |
39 ms |
2380 KB |
Output is correct |
20 |
Correct |
38 ms |
2480 KB |
Output is correct |
21 |
Correct |
348 ms |
4044 KB |
Output is correct |
22 |
Correct |
351 ms |
4080 KB |
Output is correct |
23 |
Correct |
356 ms |
4024 KB |
Output is correct |
24 |
Correct |
338 ms |
3980 KB |
Output is correct |
25 |
Correct |
355 ms |
4132 KB |
Output is correct |
26 |
Correct |
347 ms |
4052 KB |
Output is correct |
27 |
Correct |
368 ms |
4172 KB |
Output is correct |
28 |
Correct |
345 ms |
3976 KB |
Output is correct |
29 |
Correct |
359 ms |
3968 KB |
Output is correct |
30 |
Correct |
245 ms |
4244 KB |
Output is correct |
31 |
Correct |
237 ms |
4224 KB |
Output is correct |
32 |
Correct |
257 ms |
4304 KB |
Output is correct |
33 |
Correct |
244 ms |
4176 KB |
Output is correct |
34 |
Correct |
238 ms |
4192 KB |
Output is correct |
35 |
Correct |
335 ms |
4300 KB |
Output is correct |
36 |
Correct |
329 ms |
4296 KB |
Output is correct |
37 |
Correct |
337 ms |
4248 KB |
Output is correct |
38 |
Correct |
325 ms |
4172 KB |
Output is correct |
39 |
Correct |
6 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
1124 KB |
Output is correct |
41 |
Correct |
6 ms |
1068 KB |
Output is correct |
42 |
Correct |
7 ms |
1108 KB |
Output is correct |
43 |
Correct |
6 ms |
1108 KB |
Output is correct |
44 |
Correct |
7 ms |
1108 KB |
Output is correct |
45 |
Correct |
7 ms |
1108 KB |
Output is correct |
46 |
Correct |
7 ms |
1108 KB |
Output is correct |
47 |
Correct |
7 ms |
1036 KB |
Output is correct |
48 |
Correct |
7 ms |
1108 KB |
Output is correct |
49 |
Correct |
6 ms |
1108 KB |
Output is correct |
50 |
Correct |
6 ms |
1040 KB |
Output is correct |
51 |
Correct |
7 ms |
1052 KB |
Output is correct |
52 |
Correct |
7 ms |
1116 KB |
Output is correct |
53 |
Correct |
6 ms |
1108 KB |
Output is correct |
54 |
Correct |
4 ms |
852 KB |
Output is correct |
55 |
Correct |
4 ms |
852 KB |
Output is correct |
56 |
Correct |
4 ms |
852 KB |
Output is correct |
57 |
Correct |
4 ms |
852 KB |
Output is correct |
58 |
Correct |
4 ms |
852 KB |
Output is correct |
59 |
Correct |
395 ms |
4264 KB |
Output is correct |
60 |
Correct |
371 ms |
4256 KB |
Output is correct |
61 |
Correct |
387 ms |
4360 KB |
Output is correct |
62 |
Correct |
380 ms |
4296 KB |
Output is correct |
63 |
Correct |
374 ms |
4188 KB |
Output is correct |
64 |
Correct |
395 ms |
4180 KB |
Output is correct |
65 |
Correct |
406 ms |
4260 KB |
Output is correct |
66 |
Correct |
408 ms |
4208 KB |
Output is correct |
67 |
Correct |
402 ms |
4364 KB |
Output is correct |
68 |
Correct |
392 ms |
4172 KB |
Output is correct |
69 |
Correct |
364 ms |
4284 KB |
Output is correct |
70 |
Correct |
368 ms |
4232 KB |
Output is correct |
71 |
Correct |
369 ms |
4300 KB |
Output is correct |
72 |
Correct |
374 ms |
4200 KB |
Output is correct |
73 |
Correct |
383 ms |
4384 KB |
Output is correct |
74 |
Correct |
84 ms |
2596 KB |
Output is correct |
75 |
Correct |
87 ms |
2652 KB |
Output is correct |
76 |
Correct |
85 ms |
2652 KB |
Output is correct |
77 |
Correct |
86 ms |
2620 KB |
Output is correct |
78 |
Correct |
84 ms |
2596 KB |
Output is correct |