#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cstring>
#define MOD 1000000007LL
using namespace std;
int A[500], B[500];
vector<int> T;
int start_i[500], end_i[500];
long long dp[501][1000 + 10];
long long pre_inv[501];
long long modpow(long long base, long long exp) {
long long res = 1;
while(exp) {
if(exp & 1) res = (res * base) % MOD;
base = (base * base) % MOD;
exp >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int N; cin >> N;
for(int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
T.push_back(A[i]);
T.push_back(B[i] + 1);
}
for(int i = 1; i <= N; i++) {
pre_inv[i] = modpow(i, MOD - 2);
}
sort(T.begin(), T.end());
T.resize(unique(T.begin(), T.end()) - T.begin());
int M = T.size();
for(int i = 0; i < N; i++) {
start_i[i] = lower_bound(T.begin(), T.end(), A[i]) - T.begin();
end_i[i] = upper_bound(T.begin(), T.end(), B[i]) - T.begin();
}
for(int j = 0; j < M; j++) dp[N][j] = 1;
for(int i = N - 1; i >= 0; i--) {
for(int j = start_i[i]; j < end_i[i]; j++) {
int len = T[j + 1] - T[j];
dp[i][j] += len * dp[i + 1][j + 1];
dp[i][j] %= MOD;
int cnt = 1;
long long curr = len - 1;
for(int k = i + 1; k < N; k++) {
if(start_i[k] <= j && j < end_i[k]) {
cnt++;
curr = (curr * (len + cnt - 2)) % MOD;
curr = (curr * pre_inv[cnt]) % MOD;
dp[i][j] += curr * dp[k + 1][j + 1];
dp[i][j] %= MOD;
}
}
}
for(int j = M - 1; j >= 0; j--) {
dp[i][j] += dp[i][j + 1];
dp[i][j] %= MOD;
}
for(int j = M - 1; j >= 0; j--) {
dp[i][j] += dp[i + 1][j];
dp[i][j] %= MOD;
}
}
cout << (dp[0][0] - 1 + MOD) % MOD << endl;
// for(int j = 0; j < M; j++) cout << setw(5) << T[j];
// cout << endl << endl;
// for(int i = 0; i < N; i++) {
// for(int j = 0; j < M; j++) {
// cout << setw(5) << dp[i][j];
// }
// cout << endl;
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6148 KB |
Output is correct |
2 |
Correct |
9 ms |
6148 KB |
Output is correct |
3 |
Correct |
6 ms |
6148 KB |
Output is correct |
4 |
Correct |
6 ms |
6148 KB |
Output is correct |
5 |
Correct |
6 ms |
6148 KB |
Output is correct |
6 |
Correct |
6 ms |
6148 KB |
Output is correct |
7 |
Correct |
9 ms |
6148 KB |
Output is correct |
8 |
Correct |
6 ms |
6148 KB |
Output is correct |
9 |
Correct |
6 ms |
6148 KB |
Output is correct |
10 |
Correct |
3 ms |
6148 KB |
Output is correct |
11 |
Correct |
6 ms |
6148 KB |
Output is correct |
12 |
Correct |
6 ms |
6148 KB |
Output is correct |
13 |
Correct |
9 ms |
6148 KB |
Output is correct |
14 |
Correct |
9 ms |
6148 KB |
Output is correct |
15 |
Correct |
6 ms |
6148 KB |
Output is correct |
16 |
Correct |
0 ms |
6148 KB |
Output is correct |
17 |
Correct |
3 ms |
6148 KB |
Output is correct |
18 |
Correct |
0 ms |
6148 KB |
Output is correct |
19 |
Correct |
3 ms |
6148 KB |
Output is correct |
20 |
Correct |
0 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6148 KB |
Output is correct |
2 |
Correct |
9 ms |
6148 KB |
Output is correct |
3 |
Correct |
6 ms |
6148 KB |
Output is correct |
4 |
Correct |
6 ms |
6148 KB |
Output is correct |
5 |
Correct |
6 ms |
6148 KB |
Output is correct |
6 |
Correct |
6 ms |
6148 KB |
Output is correct |
7 |
Correct |
9 ms |
6148 KB |
Output is correct |
8 |
Correct |
6 ms |
6148 KB |
Output is correct |
9 |
Correct |
6 ms |
6148 KB |
Output is correct |
10 |
Correct |
3 ms |
6148 KB |
Output is correct |
11 |
Correct |
6 ms |
6148 KB |
Output is correct |
12 |
Correct |
6 ms |
6148 KB |
Output is correct |
13 |
Correct |
9 ms |
6148 KB |
Output is correct |
14 |
Correct |
9 ms |
6148 KB |
Output is correct |
15 |
Correct |
6 ms |
6148 KB |
Output is correct |
16 |
Correct |
0 ms |
6148 KB |
Output is correct |
17 |
Correct |
3 ms |
6148 KB |
Output is correct |
18 |
Correct |
0 ms |
6148 KB |
Output is correct |
19 |
Correct |
3 ms |
6148 KB |
Output is correct |
20 |
Correct |
0 ms |
6148 KB |
Output is correct |
21 |
Correct |
279 ms |
6148 KB |
Output is correct |
22 |
Correct |
249 ms |
6148 KB |
Output is correct |
23 |
Correct |
236 ms |
6148 KB |
Output is correct |
24 |
Correct |
256 ms |
6148 KB |
Output is correct |
25 |
Correct |
266 ms |
6148 KB |
Output is correct |
26 |
Correct |
526 ms |
6148 KB |
Output is correct |
27 |
Correct |
566 ms |
6148 KB |
Output is correct |
28 |
Correct |
569 ms |
6148 KB |
Output is correct |
29 |
Correct |
543 ms |
6148 KB |
Output is correct |
30 |
Correct |
6 ms |
6148 KB |
Output is correct |
31 |
Correct |
6 ms |
6148 KB |
Output is correct |
32 |
Correct |
9 ms |
6148 KB |
Output is correct |
33 |
Correct |
6 ms |
6148 KB |
Output is correct |
34 |
Correct |
9 ms |
6148 KB |
Output is correct |
35 |
Correct |
6 ms |
6148 KB |
Output is correct |
36 |
Correct |
6 ms |
6148 KB |
Output is correct |
37 |
Correct |
3 ms |
6148 KB |
Output is correct |
38 |
Correct |
9 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6148 KB |
Output is correct |
2 |
Correct |
0 ms |
6148 KB |
Output is correct |
3 |
Correct |
0 ms |
6148 KB |
Output is correct |
4 |
Correct |
3 ms |
6148 KB |
Output is correct |
5 |
Correct |
3 ms |
6148 KB |
Output is correct |
6 |
Correct |
3 ms |
6148 KB |
Output is correct |
7 |
Correct |
6 ms |
6148 KB |
Output is correct |
8 |
Correct |
3 ms |
6148 KB |
Output is correct |
9 |
Correct |
3 ms |
6148 KB |
Output is correct |
10 |
Correct |
3 ms |
6148 KB |
Output is correct |
11 |
Correct |
3 ms |
6148 KB |
Output is correct |
12 |
Correct |
0 ms |
6148 KB |
Output is correct |
13 |
Correct |
3 ms |
6148 KB |
Output is correct |
14 |
Correct |
3 ms |
6148 KB |
Output is correct |
15 |
Correct |
3 ms |
6148 KB |
Output is correct |
16 |
Correct |
0 ms |
6148 KB |
Output is correct |
17 |
Correct |
0 ms |
6148 KB |
Output is correct |
18 |
Correct |
0 ms |
6148 KB |
Output is correct |
19 |
Correct |
0 ms |
6148 KB |
Output is correct |
20 |
Correct |
0 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6148 KB |
Output is correct |
2 |
Correct |
9 ms |
6148 KB |
Output is correct |
3 |
Correct |
6 ms |
6148 KB |
Output is correct |
4 |
Correct |
6 ms |
6148 KB |
Output is correct |
5 |
Correct |
6 ms |
6148 KB |
Output is correct |
6 |
Correct |
6 ms |
6148 KB |
Output is correct |
7 |
Correct |
9 ms |
6148 KB |
Output is correct |
8 |
Correct |
6 ms |
6148 KB |
Output is correct |
9 |
Correct |
6 ms |
6148 KB |
Output is correct |
10 |
Correct |
3 ms |
6148 KB |
Output is correct |
11 |
Correct |
6 ms |
6148 KB |
Output is correct |
12 |
Correct |
6 ms |
6148 KB |
Output is correct |
13 |
Correct |
9 ms |
6148 KB |
Output is correct |
14 |
Correct |
9 ms |
6148 KB |
Output is correct |
15 |
Correct |
6 ms |
6148 KB |
Output is correct |
16 |
Correct |
0 ms |
6148 KB |
Output is correct |
17 |
Correct |
3 ms |
6148 KB |
Output is correct |
18 |
Correct |
0 ms |
6148 KB |
Output is correct |
19 |
Correct |
3 ms |
6148 KB |
Output is correct |
20 |
Correct |
0 ms |
6148 KB |
Output is correct |
21 |
Correct |
279 ms |
6148 KB |
Output is correct |
22 |
Correct |
249 ms |
6148 KB |
Output is correct |
23 |
Correct |
236 ms |
6148 KB |
Output is correct |
24 |
Correct |
256 ms |
6148 KB |
Output is correct |
25 |
Correct |
266 ms |
6148 KB |
Output is correct |
26 |
Correct |
526 ms |
6148 KB |
Output is correct |
27 |
Correct |
566 ms |
6148 KB |
Output is correct |
28 |
Correct |
569 ms |
6148 KB |
Output is correct |
29 |
Correct |
543 ms |
6148 KB |
Output is correct |
30 |
Correct |
6 ms |
6148 KB |
Output is correct |
31 |
Correct |
6 ms |
6148 KB |
Output is correct |
32 |
Correct |
9 ms |
6148 KB |
Output is correct |
33 |
Correct |
6 ms |
6148 KB |
Output is correct |
34 |
Correct |
9 ms |
6148 KB |
Output is correct |
35 |
Correct |
6 ms |
6148 KB |
Output is correct |
36 |
Correct |
6 ms |
6148 KB |
Output is correct |
37 |
Correct |
3 ms |
6148 KB |
Output is correct |
38 |
Correct |
9 ms |
6148 KB |
Output is correct |
39 |
Correct |
3 ms |
6148 KB |
Output is correct |
40 |
Correct |
0 ms |
6148 KB |
Output is correct |
41 |
Correct |
0 ms |
6148 KB |
Output is correct |
42 |
Correct |
3 ms |
6148 KB |
Output is correct |
43 |
Correct |
3 ms |
6148 KB |
Output is correct |
44 |
Correct |
3 ms |
6148 KB |
Output is correct |
45 |
Correct |
6 ms |
6148 KB |
Output is correct |
46 |
Correct |
3 ms |
6148 KB |
Output is correct |
47 |
Correct |
3 ms |
6148 KB |
Output is correct |
48 |
Correct |
3 ms |
6148 KB |
Output is correct |
49 |
Correct |
3 ms |
6148 KB |
Output is correct |
50 |
Correct |
0 ms |
6148 KB |
Output is correct |
51 |
Correct |
3 ms |
6148 KB |
Output is correct |
52 |
Correct |
3 ms |
6148 KB |
Output is correct |
53 |
Correct |
3 ms |
6148 KB |
Output is correct |
54 |
Correct |
0 ms |
6148 KB |
Output is correct |
55 |
Correct |
0 ms |
6148 KB |
Output is correct |
56 |
Correct |
0 ms |
6148 KB |
Output is correct |
57 |
Correct |
0 ms |
6148 KB |
Output is correct |
58 |
Correct |
0 ms |
6148 KB |
Output is correct |
59 |
Correct |
283 ms |
6148 KB |
Output is correct |
60 |
Correct |
266 ms |
6148 KB |
Output is correct |
61 |
Correct |
253 ms |
6148 KB |
Output is correct |
62 |
Correct |
336 ms |
6148 KB |
Output is correct |
63 |
Correct |
273 ms |
6148 KB |
Output is correct |
64 |
Correct |
619 ms |
6148 KB |
Output is correct |
65 |
Correct |
736 ms |
6148 KB |
Output is correct |
66 |
Correct |
609 ms |
6148 KB |
Output is correct |
67 |
Correct |
653 ms |
6148 KB |
Output is correct |
68 |
Correct |
633 ms |
6148 KB |
Output is correct |
69 |
Correct |
273 ms |
6148 KB |
Output is correct |
70 |
Correct |
293 ms |
6148 KB |
Output is correct |
71 |
Correct |
253 ms |
6148 KB |
Output is correct |
72 |
Correct |
303 ms |
6148 KB |
Output is correct |
73 |
Correct |
336 ms |
6148 KB |
Output is correct |
74 |
Correct |
56 ms |
6148 KB |
Output is correct |
75 |
Correct |
59 ms |
6148 KB |
Output is correct |
76 |
Correct |
69 ms |
6148 KB |
Output is correct |
77 |
Correct |
59 ms |
6148 KB |
Output is correct |
78 |
Correct |
73 ms |
6148 KB |
Output is correct |