#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = (int) 1e9 + 7;
inline int logpow(int a, int b) {
int answer = 1;
while (b > 0) {
if (b & 1) answer = (1LL * answer * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return answer;
}
inline void add(int& x, int y) {
x += y;
if (x >= MOD)
x -= MOD;
}
constexpr int MAXN = 505;
int dp[2][4 * MAXN + 1][MAXN + 1];
int sp[2][4 * MAXN + 1];
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<int> a(n + 1), b(n + 1);
vector<int> x;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
x.push_back(a[i]);
x.push_back(b[i]);
}
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
vector<pair<int, int>> segs;
segs.push_back({0, 0});
for (int i = 0; i < (int)x.size(); i++) {
segs.push_back({x[i], x[i]});
if (i + 1 < (int)x.size() && x[i] + 1 <= x[i + 1] - 1) {
segs.push_back({x[i] + 1, x[i + 1] - 1});
}
}
vector<vector<int>> comb(segs.size(), vector<int>(n + 1));
for (int i = 0; i < (int)segs.size(); i++) {
auto s = segs[i];
int size = s.second - s.first + 1;
int fact = 1, prod = 1;
comb[i][0] = 1;
for (int j = 1; j <= n; j++) {
if (j > size) {
comb[i][j] = 0;
continue;
}
prod = (1LL * prod * (size - j + 1)) % MOD;
fact = (1LL * fact * j) % MOD;
comb[i][j] = (1LL * logpow(fact, MOD - 2) * prod) % MOD;
}
}
// for (auto itr : segs) {
// cerr << itr.first << " " << itr.second << " ";
// }
// cerr << "\n";
auto Comb = [&segs, &comb](int j, int k) {
return segs[j].second - segs[j].first + 1 < k ? 0 : comb[j][k];
};
auto Calc = [&segs, &Comb](int i) {
for (int j = 0; j < (int)segs.size(); j++) {
for (int k = 1; k <= i + 1; k++) {
int curr = dp[i & 1][j][k];
if (curr == 0) continue;
sp[i & 1][j] = (sp[i & 1][j] + 1LL * Comb(j, k) * curr) % MOD;
}
}
for (int j = 1; j < (int)segs.size(); j++) {
add(sp[i & 1][j], sp[i & 1][j - 1]);
}
};
dp[0][0][1] = 1;
Calc(0);
for (int i = 1; i <= n; i++) {
memset(sp[i & 1], 0, sizeof(sp[i & 1]));
memset(dp[i & 1], 0, sizeof(dp[i & 1]));
for (int j = 0; j < (int)segs.size(); j++) {
for (int k = 1; k <= i; k++) {
dp[i & 1][j][k] = dp[1 - i & 1][j][k];
}
if (a[i] <= segs[j].first && segs[j].second <= b[i]) {
add(dp[i & 1][j][1], sp[1 - i & 1][j - 1]);
for (int k = 1; k <= i; k++) {
add(dp[i & 1][j][k], dp[1 - i & 1][j][k - 1]);
}
}
}
// for (int j = 0; j < (int)segs.size(); j++) {
// cerr << sp[1 - i & 1][j] << " ";
// }
// cerr << "\n";
// for (int j = 0; j < (int)segs.size(); j++) {
// for (int k = 1; k <= i; k++) {
// cerr << dp[i & 1][j][k] << " ";
// }
// cerr << "\n";
// }
// cerr << "\n";
Calc(i);
}
cout << (sp[n & 1][(int)segs.size() - 1] + MOD - 1) % MOD;
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:113:40: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
113 | dp[i & 1][j][k] = dp[1 - i & 1][j][k];
| ~~^~~
boat.cpp:116:43: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
116 | add(dp[i & 1][j][1], sp[1 - i & 1][j - 1]);
| ~~^~~
boat.cpp:118:47: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
118 | add(dp[i & 1][j][k], dp[1 - i & 1][j][k - 1]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
10332 KB |
Output is correct |
2 |
Correct |
237 ms |
10332 KB |
Output is correct |
3 |
Correct |
234 ms |
10332 KB |
Output is correct |
4 |
Correct |
241 ms |
10340 KB |
Output is correct |
5 |
Correct |
238 ms |
10408 KB |
Output is correct |
6 |
Correct |
231 ms |
10224 KB |
Output is correct |
7 |
Correct |
243 ms |
10316 KB |
Output is correct |
8 |
Correct |
231 ms |
10316 KB |
Output is correct |
9 |
Correct |
235 ms |
10452 KB |
Output is correct |
10 |
Correct |
239 ms |
10336 KB |
Output is correct |
11 |
Correct |
238 ms |
10316 KB |
Output is correct |
12 |
Correct |
232 ms |
10336 KB |
Output is correct |
13 |
Correct |
230 ms |
10316 KB |
Output is correct |
14 |
Correct |
239 ms |
10336 KB |
Output is correct |
15 |
Correct |
234 ms |
10332 KB |
Output is correct |
16 |
Correct |
74 ms |
8660 KB |
Output is correct |
17 |
Correct |
78 ms |
8660 KB |
Output is correct |
18 |
Correct |
80 ms |
8660 KB |
Output is correct |
19 |
Correct |
79 ms |
8716 KB |
Output is correct |
20 |
Correct |
84 ms |
8704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
10332 KB |
Output is correct |
2 |
Correct |
237 ms |
10332 KB |
Output is correct |
3 |
Correct |
234 ms |
10332 KB |
Output is correct |
4 |
Correct |
241 ms |
10340 KB |
Output is correct |
5 |
Correct |
238 ms |
10408 KB |
Output is correct |
6 |
Correct |
231 ms |
10224 KB |
Output is correct |
7 |
Correct |
243 ms |
10316 KB |
Output is correct |
8 |
Correct |
231 ms |
10316 KB |
Output is correct |
9 |
Correct |
235 ms |
10452 KB |
Output is correct |
10 |
Correct |
239 ms |
10336 KB |
Output is correct |
11 |
Correct |
238 ms |
10316 KB |
Output is correct |
12 |
Correct |
232 ms |
10336 KB |
Output is correct |
13 |
Correct |
230 ms |
10316 KB |
Output is correct |
14 |
Correct |
239 ms |
10336 KB |
Output is correct |
15 |
Correct |
234 ms |
10332 KB |
Output is correct |
16 |
Correct |
74 ms |
8660 KB |
Output is correct |
17 |
Correct |
78 ms |
8660 KB |
Output is correct |
18 |
Correct |
80 ms |
8660 KB |
Output is correct |
19 |
Correct |
79 ms |
8716 KB |
Output is correct |
20 |
Correct |
84 ms |
8704 KB |
Output is correct |
21 |
Correct |
838 ms |
11680 KB |
Output is correct |
22 |
Correct |
860 ms |
11604 KB |
Output is correct |
23 |
Correct |
796 ms |
11656 KB |
Output is correct |
24 |
Correct |
822 ms |
11704 KB |
Output is correct |
25 |
Correct |
836 ms |
11632 KB |
Output is correct |
26 |
Correct |
1120 ms |
11488 KB |
Output is correct |
27 |
Correct |
1111 ms |
11512 KB |
Output is correct |
28 |
Correct |
1092 ms |
11636 KB |
Output is correct |
29 |
Correct |
1109 ms |
11600 KB |
Output is correct |
30 |
Correct |
368 ms |
11996 KB |
Output is correct |
31 |
Correct |
410 ms |
11980 KB |
Output is correct |
32 |
Correct |
371 ms |
12108 KB |
Output is correct |
33 |
Correct |
373 ms |
12056 KB |
Output is correct |
34 |
Correct |
367 ms |
12028 KB |
Output is correct |
35 |
Correct |
236 ms |
10316 KB |
Output is correct |
36 |
Correct |
237 ms |
10316 KB |
Output is correct |
37 |
Correct |
240 ms |
10348 KB |
Output is correct |
38 |
Correct |
252 ms |
10340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
8480 KB |
Output is correct |
2 |
Correct |
19 ms |
8440 KB |
Output is correct |
3 |
Correct |
21 ms |
8404 KB |
Output is correct |
4 |
Correct |
23 ms |
8520 KB |
Output is correct |
5 |
Correct |
23 ms |
8532 KB |
Output is correct |
6 |
Correct |
31 ms |
8512 KB |
Output is correct |
7 |
Correct |
26 ms |
8404 KB |
Output is correct |
8 |
Correct |
28 ms |
8404 KB |
Output is correct |
9 |
Correct |
25 ms |
8404 KB |
Output is correct |
10 |
Correct |
29 ms |
8516 KB |
Output is correct |
11 |
Correct |
23 ms |
8448 KB |
Output is correct |
12 |
Correct |
30 ms |
8404 KB |
Output is correct |
13 |
Correct |
23 ms |
8404 KB |
Output is correct |
14 |
Correct |
22 ms |
8404 KB |
Output is correct |
15 |
Correct |
23 ms |
8532 KB |
Output is correct |
16 |
Correct |
15 ms |
8404 KB |
Output is correct |
17 |
Correct |
19 ms |
8404 KB |
Output is correct |
18 |
Correct |
17 ms |
8404 KB |
Output is correct |
19 |
Correct |
17 ms |
8532 KB |
Output is correct |
20 |
Correct |
15 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
10332 KB |
Output is correct |
2 |
Correct |
237 ms |
10332 KB |
Output is correct |
3 |
Correct |
234 ms |
10332 KB |
Output is correct |
4 |
Correct |
241 ms |
10340 KB |
Output is correct |
5 |
Correct |
238 ms |
10408 KB |
Output is correct |
6 |
Correct |
231 ms |
10224 KB |
Output is correct |
7 |
Correct |
243 ms |
10316 KB |
Output is correct |
8 |
Correct |
231 ms |
10316 KB |
Output is correct |
9 |
Correct |
235 ms |
10452 KB |
Output is correct |
10 |
Correct |
239 ms |
10336 KB |
Output is correct |
11 |
Correct |
238 ms |
10316 KB |
Output is correct |
12 |
Correct |
232 ms |
10336 KB |
Output is correct |
13 |
Correct |
230 ms |
10316 KB |
Output is correct |
14 |
Correct |
239 ms |
10336 KB |
Output is correct |
15 |
Correct |
234 ms |
10332 KB |
Output is correct |
16 |
Correct |
74 ms |
8660 KB |
Output is correct |
17 |
Correct |
78 ms |
8660 KB |
Output is correct |
18 |
Correct |
80 ms |
8660 KB |
Output is correct |
19 |
Correct |
79 ms |
8716 KB |
Output is correct |
20 |
Correct |
84 ms |
8704 KB |
Output is correct |
21 |
Correct |
838 ms |
11680 KB |
Output is correct |
22 |
Correct |
860 ms |
11604 KB |
Output is correct |
23 |
Correct |
796 ms |
11656 KB |
Output is correct |
24 |
Correct |
822 ms |
11704 KB |
Output is correct |
25 |
Correct |
836 ms |
11632 KB |
Output is correct |
26 |
Correct |
1120 ms |
11488 KB |
Output is correct |
27 |
Correct |
1111 ms |
11512 KB |
Output is correct |
28 |
Correct |
1092 ms |
11636 KB |
Output is correct |
29 |
Correct |
1109 ms |
11600 KB |
Output is correct |
30 |
Correct |
368 ms |
11996 KB |
Output is correct |
31 |
Correct |
410 ms |
11980 KB |
Output is correct |
32 |
Correct |
371 ms |
12108 KB |
Output is correct |
33 |
Correct |
373 ms |
12056 KB |
Output is correct |
34 |
Correct |
367 ms |
12028 KB |
Output is correct |
35 |
Correct |
236 ms |
10316 KB |
Output is correct |
36 |
Correct |
237 ms |
10316 KB |
Output is correct |
37 |
Correct |
240 ms |
10348 KB |
Output is correct |
38 |
Correct |
252 ms |
10340 KB |
Output is correct |
39 |
Correct |
21 ms |
8480 KB |
Output is correct |
40 |
Correct |
19 ms |
8440 KB |
Output is correct |
41 |
Correct |
21 ms |
8404 KB |
Output is correct |
42 |
Correct |
23 ms |
8520 KB |
Output is correct |
43 |
Correct |
23 ms |
8532 KB |
Output is correct |
44 |
Correct |
31 ms |
8512 KB |
Output is correct |
45 |
Correct |
26 ms |
8404 KB |
Output is correct |
46 |
Correct |
28 ms |
8404 KB |
Output is correct |
47 |
Correct |
25 ms |
8404 KB |
Output is correct |
48 |
Correct |
29 ms |
8516 KB |
Output is correct |
49 |
Correct |
23 ms |
8448 KB |
Output is correct |
50 |
Correct |
30 ms |
8404 KB |
Output is correct |
51 |
Correct |
23 ms |
8404 KB |
Output is correct |
52 |
Correct |
22 ms |
8404 KB |
Output is correct |
53 |
Correct |
23 ms |
8532 KB |
Output is correct |
54 |
Correct |
15 ms |
8404 KB |
Output is correct |
55 |
Correct |
19 ms |
8404 KB |
Output is correct |
56 |
Correct |
17 ms |
8404 KB |
Output is correct |
57 |
Correct |
17 ms |
8532 KB |
Output is correct |
58 |
Correct |
15 ms |
8404 KB |
Output is correct |
59 |
Correct |
1097 ms |
12236 KB |
Output is correct |
60 |
Correct |
1127 ms |
12276 KB |
Output is correct |
61 |
Correct |
1040 ms |
12344 KB |
Output is correct |
62 |
Correct |
1165 ms |
12268 KB |
Output is correct |
63 |
Correct |
1109 ms |
12348 KB |
Output is correct |
64 |
Correct |
1603 ms |
12348 KB |
Output is correct |
65 |
Correct |
1561 ms |
12352 KB |
Output is correct |
66 |
Correct |
1569 ms |
12364 KB |
Output is correct |
67 |
Correct |
1477 ms |
12356 KB |
Output is correct |
68 |
Correct |
1494 ms |
12348 KB |
Output is correct |
69 |
Correct |
1090 ms |
12404 KB |
Output is correct |
70 |
Correct |
1112 ms |
12344 KB |
Output is correct |
71 |
Correct |
1119 ms |
12356 KB |
Output is correct |
72 |
Correct |
1127 ms |
12348 KB |
Output is correct |
73 |
Correct |
1142 ms |
12236 KB |
Output is correct |
74 |
Correct |
148 ms |
8660 KB |
Output is correct |
75 |
Correct |
146 ms |
8660 KB |
Output is correct |
76 |
Correct |
149 ms |
8660 KB |
Output is correct |
77 |
Correct |
148 ms |
8744 KB |
Output is correct |
78 |
Correct |
148 ms |
8660 KB |
Output is correct |