/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 500;
const int M_MAX = N_MAX * 4;
const int MOD = (int) 1e9 + 7;
int pwr (const int &a, const int &b) {
if (b == 0) {
return 1;
} else if (b & 1) {
return (ll) a * pwr(a, (b ^ 1)) % MOD;
} else {
int aux = pwr(a, (b >> 1));
return (ll) aux * aux % MOD;
}
}
int inv (const int &a) {
return pwr(a, MOD - 2);
}
int N;
int a[N_MAX + 2], b[N_MAX + 2];
int M;
int l[M_MAX + 2], r[M_MAX + 2];
void compress () {
vector <int> v;
for (int i = 1; i <= N; i++) {
v.push_back(a[i]);
v.push_back(b[i]);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
map <int, int> mp;
for (int i = 0; i < (int) v.size(); i++) {
if ((i == 0 ? 1 : v[i - 1] + 1) <= v[i] - 1) {
M++;
l[M] = (i == 0 ? 1 : v[i - 1] + 1);
r[M] = v[i] - 1;
}
mp[v[i]] = ++M;
l[M] = r[M] = v[i];
}
for (int i = 1; i <= N; i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
}
int chooseVals[M_MAX + 2][N_MAX + 2];
int chooseBoxes[N_MAX + 2][N_MAX + 2];
int choose[M_MAX + 2][N_MAX + 2];
void precalc () {
for (int cnt = 1; cnt <= N; cnt++) {
chooseBoxes[cnt][1] = 1;
chooseBoxes[cnt][2] = 1;
for (int take = 3; take <= cnt; take++) {
chooseBoxes[cnt][take] = chooseBoxes[cnt - 1][take] + chooseBoxes[cnt - 1][take - 1];
if (chooseBoxes[cnt][take] >= MOD) {
chooseBoxes[cnt][take] -= MOD;
}
}
}
for (int x = 1; x <= M; x++) {
int from = r[x] - l[x] + 1;
chooseVals[x][0] = 1;
for (int take = 1; take <= min(N, from); take++) {
chooseVals[x][take] = (ll) chooseVals[x][take - 1] * inv(take) % MOD * (from - (take - 1)) % MOD;
}
for (int cnt = 1; cnt <= N; cnt++) {
for (int take = 1 + (cnt > 1); take <= cnt; take++) {
choose[x][cnt] = (choose[x][cnt] + (ll) chooseVals[x][take] * chooseBoxes[cnt][take]) % MOD;
}
}
}
}
int dp[N_MAX + 2][M_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i] >> b[i];
}
compress();
precalc();
int answer = 0;
fill(dp[0], dp[0] + M + 1, 1);
for (int i = 1; i <= N; i++) {
for (int x = a[i]; x <= b[i]; x++) {
int cnt = 0;
for (int j = i; j >= 1; j--) {
if (a[j] <= x && x <= b[j]) {
cnt++;
dp[i][x] = (dp[i][x] + (ll) dp[j - 1][x - 1] * choose[x][cnt]) % MOD;
}
}
}
for (int x = 1; x <= M; x++) {
dp[i][x] += dp[i][x - 1];
if (dp[i][x] >= MOD) {
dp[i][x] -= MOD;
}
}
answer += dp[i][b[i]];
if (answer >= MOD) {
answer -= MOD;
}
for (int x = 0; x <= M; x++) {
dp[i][x] += dp[i - 1][x];
if (dp[i][x] >= MOD) {
dp[i][x] -= MOD;
}
}
}
cout << answer << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
620 ms |
9272 KB |
Output is correct |
2 |
Correct |
650 ms |
9180 KB |
Output is correct |
3 |
Correct |
630 ms |
9124 KB |
Output is correct |
4 |
Correct |
635 ms |
9160 KB |
Output is correct |
5 |
Correct |
628 ms |
9256 KB |
Output is correct |
6 |
Correct |
619 ms |
9164 KB |
Output is correct |
7 |
Correct |
634 ms |
9200 KB |
Output is correct |
8 |
Correct |
637 ms |
9280 KB |
Output is correct |
9 |
Correct |
627 ms |
9168 KB |
Output is correct |
10 |
Correct |
608 ms |
9180 KB |
Output is correct |
11 |
Correct |
669 ms |
9212 KB |
Output is correct |
12 |
Correct |
633 ms |
9084 KB |
Output is correct |
13 |
Correct |
628 ms |
9392 KB |
Output is correct |
14 |
Correct |
631 ms |
9164 KB |
Output is correct |
15 |
Correct |
627 ms |
9168 KB |
Output is correct |
16 |
Correct |
112 ms |
4432 KB |
Output is correct |
17 |
Correct |
127 ms |
4448 KB |
Output is correct |
18 |
Correct |
114 ms |
4364 KB |
Output is correct |
19 |
Correct |
118 ms |
4424 KB |
Output is correct |
20 |
Correct |
118 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
620 ms |
9272 KB |
Output is correct |
2 |
Correct |
650 ms |
9180 KB |
Output is correct |
3 |
Correct |
630 ms |
9124 KB |
Output is correct |
4 |
Correct |
635 ms |
9160 KB |
Output is correct |
5 |
Correct |
628 ms |
9256 KB |
Output is correct |
6 |
Correct |
619 ms |
9164 KB |
Output is correct |
7 |
Correct |
634 ms |
9200 KB |
Output is correct |
8 |
Correct |
637 ms |
9280 KB |
Output is correct |
9 |
Correct |
627 ms |
9168 KB |
Output is correct |
10 |
Correct |
608 ms |
9180 KB |
Output is correct |
11 |
Correct |
669 ms |
9212 KB |
Output is correct |
12 |
Correct |
633 ms |
9084 KB |
Output is correct |
13 |
Correct |
628 ms |
9392 KB |
Output is correct |
14 |
Correct |
631 ms |
9164 KB |
Output is correct |
15 |
Correct |
627 ms |
9168 KB |
Output is correct |
16 |
Correct |
112 ms |
4432 KB |
Output is correct |
17 |
Correct |
127 ms |
4448 KB |
Output is correct |
18 |
Correct |
114 ms |
4364 KB |
Output is correct |
19 |
Correct |
118 ms |
4424 KB |
Output is correct |
20 |
Correct |
118 ms |
4424 KB |
Output is correct |
21 |
Correct |
1165 ms |
11976 KB |
Output is correct |
22 |
Correct |
1133 ms |
11900 KB |
Output is correct |
23 |
Correct |
1105 ms |
11852 KB |
Output is correct |
24 |
Correct |
1145 ms |
11808 KB |
Output is correct |
25 |
Correct |
1109 ms |
11764 KB |
Output is correct |
26 |
Correct |
1315 ms |
11532 KB |
Output is correct |
27 |
Correct |
1308 ms |
11456 KB |
Output is correct |
28 |
Correct |
1313 ms |
11460 KB |
Output is correct |
29 |
Correct |
1319 ms |
11596 KB |
Output is correct |
30 |
Correct |
1066 ms |
12604 KB |
Output is correct |
31 |
Correct |
1053 ms |
12504 KB |
Output is correct |
32 |
Correct |
1057 ms |
12552 KB |
Output is correct |
33 |
Correct |
1065 ms |
12580 KB |
Output is correct |
34 |
Correct |
1052 ms |
12504 KB |
Output is correct |
35 |
Correct |
605 ms |
9120 KB |
Output is correct |
36 |
Correct |
605 ms |
9296 KB |
Output is correct |
37 |
Correct |
609 ms |
9208 KB |
Output is correct |
38 |
Correct |
606 ms |
9108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2560 KB |
Output is correct |
2 |
Correct |
17 ms |
2660 KB |
Output is correct |
3 |
Correct |
18 ms |
2668 KB |
Output is correct |
4 |
Correct |
18 ms |
2656 KB |
Output is correct |
5 |
Correct |
18 ms |
2636 KB |
Output is correct |
6 |
Correct |
21 ms |
2656 KB |
Output is correct |
7 |
Correct |
20 ms |
2664 KB |
Output is correct |
8 |
Correct |
22 ms |
2628 KB |
Output is correct |
9 |
Correct |
20 ms |
2644 KB |
Output is correct |
10 |
Correct |
20 ms |
2616 KB |
Output is correct |
11 |
Correct |
18 ms |
2644 KB |
Output is correct |
12 |
Correct |
18 ms |
2592 KB |
Output is correct |
13 |
Correct |
18 ms |
2632 KB |
Output is correct |
14 |
Correct |
17 ms |
2636 KB |
Output is correct |
15 |
Correct |
18 ms |
2644 KB |
Output is correct |
16 |
Correct |
9 ms |
1620 KB |
Output is correct |
17 |
Correct |
8 ms |
1620 KB |
Output is correct |
18 |
Correct |
9 ms |
1604 KB |
Output is correct |
19 |
Correct |
8 ms |
1620 KB |
Output is correct |
20 |
Correct |
9 ms |
1692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
620 ms |
9272 KB |
Output is correct |
2 |
Correct |
650 ms |
9180 KB |
Output is correct |
3 |
Correct |
630 ms |
9124 KB |
Output is correct |
4 |
Correct |
635 ms |
9160 KB |
Output is correct |
5 |
Correct |
628 ms |
9256 KB |
Output is correct |
6 |
Correct |
619 ms |
9164 KB |
Output is correct |
7 |
Correct |
634 ms |
9200 KB |
Output is correct |
8 |
Correct |
637 ms |
9280 KB |
Output is correct |
9 |
Correct |
627 ms |
9168 KB |
Output is correct |
10 |
Correct |
608 ms |
9180 KB |
Output is correct |
11 |
Correct |
669 ms |
9212 KB |
Output is correct |
12 |
Correct |
633 ms |
9084 KB |
Output is correct |
13 |
Correct |
628 ms |
9392 KB |
Output is correct |
14 |
Correct |
631 ms |
9164 KB |
Output is correct |
15 |
Correct |
627 ms |
9168 KB |
Output is correct |
16 |
Correct |
112 ms |
4432 KB |
Output is correct |
17 |
Correct |
127 ms |
4448 KB |
Output is correct |
18 |
Correct |
114 ms |
4364 KB |
Output is correct |
19 |
Correct |
118 ms |
4424 KB |
Output is correct |
20 |
Correct |
118 ms |
4424 KB |
Output is correct |
21 |
Correct |
1165 ms |
11976 KB |
Output is correct |
22 |
Correct |
1133 ms |
11900 KB |
Output is correct |
23 |
Correct |
1105 ms |
11852 KB |
Output is correct |
24 |
Correct |
1145 ms |
11808 KB |
Output is correct |
25 |
Correct |
1109 ms |
11764 KB |
Output is correct |
26 |
Correct |
1315 ms |
11532 KB |
Output is correct |
27 |
Correct |
1308 ms |
11456 KB |
Output is correct |
28 |
Correct |
1313 ms |
11460 KB |
Output is correct |
29 |
Correct |
1319 ms |
11596 KB |
Output is correct |
30 |
Correct |
1066 ms |
12604 KB |
Output is correct |
31 |
Correct |
1053 ms |
12504 KB |
Output is correct |
32 |
Correct |
1057 ms |
12552 KB |
Output is correct |
33 |
Correct |
1065 ms |
12580 KB |
Output is correct |
34 |
Correct |
1052 ms |
12504 KB |
Output is correct |
35 |
Correct |
605 ms |
9120 KB |
Output is correct |
36 |
Correct |
605 ms |
9296 KB |
Output is correct |
37 |
Correct |
609 ms |
9208 KB |
Output is correct |
38 |
Correct |
606 ms |
9108 KB |
Output is correct |
39 |
Correct |
17 ms |
2560 KB |
Output is correct |
40 |
Correct |
17 ms |
2660 KB |
Output is correct |
41 |
Correct |
18 ms |
2668 KB |
Output is correct |
42 |
Correct |
18 ms |
2656 KB |
Output is correct |
43 |
Correct |
18 ms |
2636 KB |
Output is correct |
44 |
Correct |
21 ms |
2656 KB |
Output is correct |
45 |
Correct |
20 ms |
2664 KB |
Output is correct |
46 |
Correct |
22 ms |
2628 KB |
Output is correct |
47 |
Correct |
20 ms |
2644 KB |
Output is correct |
48 |
Correct |
20 ms |
2616 KB |
Output is correct |
49 |
Correct |
18 ms |
2644 KB |
Output is correct |
50 |
Correct |
18 ms |
2592 KB |
Output is correct |
51 |
Correct |
18 ms |
2632 KB |
Output is correct |
52 |
Correct |
17 ms |
2636 KB |
Output is correct |
53 |
Correct |
18 ms |
2644 KB |
Output is correct |
54 |
Correct |
9 ms |
1620 KB |
Output is correct |
55 |
Correct |
8 ms |
1620 KB |
Output is correct |
56 |
Correct |
9 ms |
1604 KB |
Output is correct |
57 |
Correct |
8 ms |
1620 KB |
Output is correct |
58 |
Correct |
9 ms |
1692 KB |
Output is correct |
59 |
Correct |
1483 ms |
13192 KB |
Output is correct |
60 |
Correct |
1442 ms |
13288 KB |
Output is correct |
61 |
Correct |
1447 ms |
13192 KB |
Output is correct |
62 |
Correct |
1472 ms |
13044 KB |
Output is correct |
63 |
Correct |
1479 ms |
13172 KB |
Output is correct |
64 |
Correct |
1789 ms |
13212 KB |
Output is correct |
65 |
Correct |
1797 ms |
13172 KB |
Output is correct |
66 |
Correct |
1794 ms |
13096 KB |
Output is correct |
67 |
Correct |
1791 ms |
13208 KB |
Output is correct |
68 |
Correct |
1804 ms |
13092 KB |
Output is correct |
69 |
Correct |
1431 ms |
13140 KB |
Output is correct |
70 |
Correct |
1420 ms |
13160 KB |
Output is correct |
71 |
Correct |
1426 ms |
13080 KB |
Output is correct |
72 |
Correct |
1451 ms |
13164 KB |
Output is correct |
73 |
Correct |
1433 ms |
13152 KB |
Output is correct |
74 |
Correct |
155 ms |
4476 KB |
Output is correct |
75 |
Correct |
153 ms |
4652 KB |
Output is correct |
76 |
Correct |
155 ms |
4456 KB |
Output is correct |
77 |
Correct |
153 ms |
4556 KB |
Output is correct |
78 |
Correct |
155 ms |
4440 KB |
Output is correct |