#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
int add(int a, int b) {
return a + b < M ? a + b : a + b - M;
}
int sub(int a, int b) {
return a - b >= 0 ? a - b : a - b + M;
}
int mul(int a, int b) {
return a * 1ll * b % M;
}
int pw(int a, int n) {
int res = 1;
for (; n > 0; n >>= 1, a = mul(a, a)) {
if (n & 1) {
res = mul(res, a);
}
}
return res;
}
int inv(int a) {
return pw(a, M - 2);
}
const int N = 505;
int n;
int a[N], b[N];
int m;
int x[2 * N];
int len[2 * N];
int bigc[2 * N][N];
int c[N][N];
int sumc[2 * N][N];
int dp[2 * N][N];
int pref[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
++b[i];
}
for (int i = 0; i < n; ++i) {
x[2 * i] = a[i];
x[2 * i + 1] = b[i];
}
sort(x, x + 2 * n);
m = unique(x, x + 2 * n) - x - 1;
for (int i = 0; i < m; ++i) {
len[i] = x[i + 1] - x[i];
bigc[i][0] = 1;
for (int j = 1; j <= min(len[i], n); ++j) {
bigc[i][j] = mul(bigc[i][j - 1], mul(len[i] - j + 1, inv(j)));
}
}
c[0][0] = 1;
for (int i = 1; i <= n; ++i) {
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; ++j) {
c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]);
}
}
for (int i = 0; i < m; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= min(len[i], j); ++k) {
sumc[i][j] = add(sumc[i][j], mul(bigc[i][k], c[j - 1][k - 1]));
}
}
}
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i] + (x[j] >= a[i] && x[j + 1] <= b[i]);
}
for (int i = 0; i < n; ++i) {
if (x[j] >= a[i] && x[j + 1] <= b[i]) {
dp[j][i] = sumc[j][pref[i + 1]];
}
}
if (j > 0) {
for (int i = 0; i < n; ++i) {
if (x[j] >= a[i] && x[j + 1] <= b[i]) {
for (int k = 0; k < i; ++k) {
dp[j][i] = add(dp[j][i], mul(dp[j - 1][k], sumc[j][pref[i + 1] - pref[k + 1]]));
}
}
}
for (int i = 0; i < n; ++i) {
dp[j][i] = add(dp[j][i], dp[j - 1][i]);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans = add(ans, dp[m - 1][i]);
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
7228 KB |
Output is correct |
2 |
Correct |
214 ms |
7216 KB |
Output is correct |
3 |
Correct |
215 ms |
7152 KB |
Output is correct |
4 |
Correct |
218 ms |
7240 KB |
Output is correct |
5 |
Correct |
215 ms |
7244 KB |
Output is correct |
6 |
Correct |
208 ms |
7264 KB |
Output is correct |
7 |
Correct |
220 ms |
7176 KB |
Output is correct |
8 |
Correct |
210 ms |
7152 KB |
Output is correct |
9 |
Correct |
211 ms |
7208 KB |
Output is correct |
10 |
Correct |
224 ms |
7172 KB |
Output is correct |
11 |
Correct |
210 ms |
7188 KB |
Output is correct |
12 |
Correct |
218 ms |
7308 KB |
Output is correct |
13 |
Correct |
214 ms |
7184 KB |
Output is correct |
14 |
Correct |
206 ms |
7160 KB |
Output is correct |
15 |
Correct |
228 ms |
7212 KB |
Output is correct |
16 |
Correct |
39 ms |
2304 KB |
Output is correct |
17 |
Correct |
42 ms |
2380 KB |
Output is correct |
18 |
Correct |
42 ms |
2384 KB |
Output is correct |
19 |
Correct |
41 ms |
2444 KB |
Output is correct |
20 |
Correct |
47 ms |
2336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
7228 KB |
Output is correct |
2 |
Correct |
214 ms |
7216 KB |
Output is correct |
3 |
Correct |
215 ms |
7152 KB |
Output is correct |
4 |
Correct |
218 ms |
7240 KB |
Output is correct |
5 |
Correct |
215 ms |
7244 KB |
Output is correct |
6 |
Correct |
208 ms |
7264 KB |
Output is correct |
7 |
Correct |
220 ms |
7176 KB |
Output is correct |
8 |
Correct |
210 ms |
7152 KB |
Output is correct |
9 |
Correct |
211 ms |
7208 KB |
Output is correct |
10 |
Correct |
224 ms |
7172 KB |
Output is correct |
11 |
Correct |
210 ms |
7188 KB |
Output is correct |
12 |
Correct |
218 ms |
7308 KB |
Output is correct |
13 |
Correct |
214 ms |
7184 KB |
Output is correct |
14 |
Correct |
206 ms |
7160 KB |
Output is correct |
15 |
Correct |
228 ms |
7212 KB |
Output is correct |
16 |
Correct |
39 ms |
2304 KB |
Output is correct |
17 |
Correct |
42 ms |
2380 KB |
Output is correct |
18 |
Correct |
42 ms |
2384 KB |
Output is correct |
19 |
Correct |
41 ms |
2444 KB |
Output is correct |
20 |
Correct |
47 ms |
2336 KB |
Output is correct |
21 |
Correct |
170 ms |
6676 KB |
Output is correct |
22 |
Correct |
155 ms |
6716 KB |
Output is correct |
23 |
Correct |
159 ms |
6684 KB |
Output is correct |
24 |
Correct |
160 ms |
6592 KB |
Output is correct |
25 |
Correct |
156 ms |
6584 KB |
Output is correct |
26 |
Correct |
207 ms |
6440 KB |
Output is correct |
27 |
Correct |
225 ms |
6724 KB |
Output is correct |
28 |
Correct |
213 ms |
6532 KB |
Output is correct |
29 |
Correct |
207 ms |
6496 KB |
Output is correct |
30 |
Correct |
226 ms |
7148 KB |
Output is correct |
31 |
Correct |
202 ms |
7172 KB |
Output is correct |
32 |
Correct |
213 ms |
7232 KB |
Output is correct |
33 |
Correct |
209 ms |
7244 KB |
Output is correct |
34 |
Correct |
220 ms |
7120 KB |
Output is correct |
35 |
Correct |
202 ms |
7388 KB |
Output is correct |
36 |
Correct |
203 ms |
7244 KB |
Output is correct |
37 |
Correct |
210 ms |
7244 KB |
Output is correct |
38 |
Correct |
199 ms |
7192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1684 KB |
Output is correct |
2 |
Correct |
10 ms |
1612 KB |
Output is correct |
3 |
Correct |
10 ms |
1680 KB |
Output is correct |
4 |
Correct |
9 ms |
1728 KB |
Output is correct |
5 |
Correct |
10 ms |
1624 KB |
Output is correct |
6 |
Correct |
9 ms |
1620 KB |
Output is correct |
7 |
Correct |
9 ms |
1620 KB |
Output is correct |
8 |
Correct |
10 ms |
1632 KB |
Output is correct |
9 |
Correct |
9 ms |
1620 KB |
Output is correct |
10 |
Correct |
10 ms |
1776 KB |
Output is correct |
11 |
Correct |
9 ms |
1740 KB |
Output is correct |
12 |
Correct |
8 ms |
1620 KB |
Output is correct |
13 |
Correct |
8 ms |
1620 KB |
Output is correct |
14 |
Correct |
8 ms |
1620 KB |
Output is correct |
15 |
Correct |
8 ms |
1620 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
5 ms |
1112 KB |
Output is correct |
19 |
Correct |
6 ms |
1108 KB |
Output is correct |
20 |
Correct |
5 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
7228 KB |
Output is correct |
2 |
Correct |
214 ms |
7216 KB |
Output is correct |
3 |
Correct |
215 ms |
7152 KB |
Output is correct |
4 |
Correct |
218 ms |
7240 KB |
Output is correct |
5 |
Correct |
215 ms |
7244 KB |
Output is correct |
6 |
Correct |
208 ms |
7264 KB |
Output is correct |
7 |
Correct |
220 ms |
7176 KB |
Output is correct |
8 |
Correct |
210 ms |
7152 KB |
Output is correct |
9 |
Correct |
211 ms |
7208 KB |
Output is correct |
10 |
Correct |
224 ms |
7172 KB |
Output is correct |
11 |
Correct |
210 ms |
7188 KB |
Output is correct |
12 |
Correct |
218 ms |
7308 KB |
Output is correct |
13 |
Correct |
214 ms |
7184 KB |
Output is correct |
14 |
Correct |
206 ms |
7160 KB |
Output is correct |
15 |
Correct |
228 ms |
7212 KB |
Output is correct |
16 |
Correct |
39 ms |
2304 KB |
Output is correct |
17 |
Correct |
42 ms |
2380 KB |
Output is correct |
18 |
Correct |
42 ms |
2384 KB |
Output is correct |
19 |
Correct |
41 ms |
2444 KB |
Output is correct |
20 |
Correct |
47 ms |
2336 KB |
Output is correct |
21 |
Correct |
170 ms |
6676 KB |
Output is correct |
22 |
Correct |
155 ms |
6716 KB |
Output is correct |
23 |
Correct |
159 ms |
6684 KB |
Output is correct |
24 |
Correct |
160 ms |
6592 KB |
Output is correct |
25 |
Correct |
156 ms |
6584 KB |
Output is correct |
26 |
Correct |
207 ms |
6440 KB |
Output is correct |
27 |
Correct |
225 ms |
6724 KB |
Output is correct |
28 |
Correct |
213 ms |
6532 KB |
Output is correct |
29 |
Correct |
207 ms |
6496 KB |
Output is correct |
30 |
Correct |
226 ms |
7148 KB |
Output is correct |
31 |
Correct |
202 ms |
7172 KB |
Output is correct |
32 |
Correct |
213 ms |
7232 KB |
Output is correct |
33 |
Correct |
209 ms |
7244 KB |
Output is correct |
34 |
Correct |
220 ms |
7120 KB |
Output is correct |
35 |
Correct |
202 ms |
7388 KB |
Output is correct |
36 |
Correct |
203 ms |
7244 KB |
Output is correct |
37 |
Correct |
210 ms |
7244 KB |
Output is correct |
38 |
Correct |
199 ms |
7192 KB |
Output is correct |
39 |
Correct |
8 ms |
1684 KB |
Output is correct |
40 |
Correct |
10 ms |
1612 KB |
Output is correct |
41 |
Correct |
10 ms |
1680 KB |
Output is correct |
42 |
Correct |
9 ms |
1728 KB |
Output is correct |
43 |
Correct |
10 ms |
1624 KB |
Output is correct |
44 |
Correct |
9 ms |
1620 KB |
Output is correct |
45 |
Correct |
9 ms |
1620 KB |
Output is correct |
46 |
Correct |
10 ms |
1632 KB |
Output is correct |
47 |
Correct |
9 ms |
1620 KB |
Output is correct |
48 |
Correct |
10 ms |
1776 KB |
Output is correct |
49 |
Correct |
9 ms |
1740 KB |
Output is correct |
50 |
Correct |
8 ms |
1620 KB |
Output is correct |
51 |
Correct |
8 ms |
1620 KB |
Output is correct |
52 |
Correct |
8 ms |
1620 KB |
Output is correct |
53 |
Correct |
8 ms |
1620 KB |
Output is correct |
54 |
Correct |
4 ms |
1236 KB |
Output is correct |
55 |
Correct |
4 ms |
1100 KB |
Output is correct |
56 |
Correct |
5 ms |
1112 KB |
Output is correct |
57 |
Correct |
6 ms |
1108 KB |
Output is correct |
58 |
Correct |
5 ms |
1108 KB |
Output is correct |
59 |
Correct |
580 ms |
7260 KB |
Output is correct |
60 |
Correct |
568 ms |
7144 KB |
Output is correct |
61 |
Correct |
568 ms |
7256 KB |
Output is correct |
62 |
Correct |
578 ms |
7220 KB |
Output is correct |
63 |
Correct |
569 ms |
7220 KB |
Output is correct |
64 |
Correct |
647 ms |
7204 KB |
Output is correct |
65 |
Correct |
646 ms |
7240 KB |
Output is correct |
66 |
Correct |
653 ms |
7216 KB |
Output is correct |
67 |
Correct |
645 ms |
7232 KB |
Output is correct |
68 |
Correct |
642 ms |
7288 KB |
Output is correct |
69 |
Correct |
548 ms |
7244 KB |
Output is correct |
70 |
Correct |
541 ms |
7224 KB |
Output is correct |
71 |
Correct |
539 ms |
7256 KB |
Output is correct |
72 |
Correct |
529 ms |
7212 KB |
Output is correct |
73 |
Correct |
557 ms |
7140 KB |
Output is correct |
74 |
Correct |
74 ms |
2408 KB |
Output is correct |
75 |
Correct |
79 ms |
2380 KB |
Output is correct |
76 |
Correct |
74 ms |
2372 KB |
Output is correct |
77 |
Correct |
73 ms |
2380 KB |
Output is correct |
78 |
Correct |
77 ms |
2348 KB |
Output is correct |