#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "Boat"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 502;
const int MOD = 1e9 + 7;
int n, a[N], b[N];
int nX, x[2 * N], inv[N], dp[2][N * 2][N], sumDp[N * 2][N];
void readInput(){
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i] >> b[i];
}
void compress(){
for(int i = 1; i <= n; i++){
x[++nX] = a[i];
x[++nX] = ++b[i];
}
sort(x + 1, x + 1 + nX);
nX = unique(x + 1, x + 1 + nX) - (x + 1);
for(int i = 1; i <= n; i++){
a[i] = lower_bound(x + 1, x + 1 + nX, a[i]) - x;
b[i] = lower_bound(x + 1, x + 1 + nX, b[i]) - x;
}
}
int powMod(int x, int k){
if (k == 0)
return 1;
int res = powMod(x, k >> 1);
res = (ll) res * res % MOD;
if (k & 1)
res = (ll) res * x % MOD;
return res;
}
void prepare(){
for(int i = 1; i <= n; i++)
b[i]--;
inv[0] = 1;
for(int i = 1; i <= n; i++)
inv[i] = powMod(i, MOD - 2);
}
void add(int &x, int y){
x += y;
x -= (x >= MOD) * MOD;
}
void solve(){
for(int i = 0; i < n; i++){
int id = i & 1;
for(int j = 1; j < nX; j++){
int len = x[j + 1] - x[j];
int maxK = min(n, len);
for(int k = 1; k <= maxK; k++)
dp[id ^ 1][j][k] = 0;
}
int sum = 1;
for(int j = 1; j < nX; j++){
int len = x[j + 1] - x[j];
int maxK = min(n, len);
if (a[i + 1] <= j && j <= b[i + 1])
add(dp[id ^ 1][j][1], (ll) sum * len % MOD);
for(int k = 1; k <= maxK; k++){
add(sum, sumDp[j][k]);
if (a[i + 1] <= j && j <= b[i + 1] && k < maxK)
add(dp[id ^ 1][j][k + 1], (ll) sumDp[j][k] * (len - k) % MOD * inv[k + 1] % MOD);
}
}
for(int j = 1; j < nX; j++){
int maxK = min(n, x[j + 1] - x[j]);
for(int k = 1; k <= maxK; k++)
add(sumDp[j][k], dp[id ^ 1][j][k]);
}
}
int res = 0;
for(int j = 1; j < nX; j++){
int maxK = min(n, x[j + 1] - x[j]);
for(int k = 1; k <= maxK; k++)
add(res, sumDp[j][k]);
}
cout << res;
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
readInput();
compress();
prepare();
solve();
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
101 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
102 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
453 ms |
6264 KB |
Output is correct |
2 |
Correct |
485 ms |
6264 KB |
Output is correct |
3 |
Correct |
451 ms |
6264 KB |
Output is correct |
4 |
Correct |
457 ms |
6264 KB |
Output is correct |
5 |
Correct |
452 ms |
6268 KB |
Output is correct |
6 |
Correct |
453 ms |
6264 KB |
Output is correct |
7 |
Correct |
451 ms |
6264 KB |
Output is correct |
8 |
Correct |
452 ms |
6264 KB |
Output is correct |
9 |
Correct |
446 ms |
6268 KB |
Output is correct |
10 |
Correct |
457 ms |
6260 KB |
Output is correct |
11 |
Correct |
494 ms |
6260 KB |
Output is correct |
12 |
Correct |
453 ms |
6264 KB |
Output is correct |
13 |
Correct |
454 ms |
6264 KB |
Output is correct |
14 |
Correct |
457 ms |
6264 KB |
Output is correct |
15 |
Correct |
470 ms |
6264 KB |
Output is correct |
16 |
Correct |
72 ms |
1408 KB |
Output is correct |
17 |
Correct |
79 ms |
1408 KB |
Output is correct |
18 |
Correct |
75 ms |
1440 KB |
Output is correct |
19 |
Correct |
81 ms |
1528 KB |
Output is correct |
20 |
Correct |
83 ms |
1464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
453 ms |
6264 KB |
Output is correct |
2 |
Correct |
485 ms |
6264 KB |
Output is correct |
3 |
Correct |
451 ms |
6264 KB |
Output is correct |
4 |
Correct |
457 ms |
6264 KB |
Output is correct |
5 |
Correct |
452 ms |
6268 KB |
Output is correct |
6 |
Correct |
453 ms |
6264 KB |
Output is correct |
7 |
Correct |
451 ms |
6264 KB |
Output is correct |
8 |
Correct |
452 ms |
6264 KB |
Output is correct |
9 |
Correct |
446 ms |
6268 KB |
Output is correct |
10 |
Correct |
457 ms |
6260 KB |
Output is correct |
11 |
Correct |
494 ms |
6260 KB |
Output is correct |
12 |
Correct |
453 ms |
6264 KB |
Output is correct |
13 |
Correct |
454 ms |
6264 KB |
Output is correct |
14 |
Correct |
457 ms |
6264 KB |
Output is correct |
15 |
Correct |
470 ms |
6264 KB |
Output is correct |
16 |
Correct |
72 ms |
1408 KB |
Output is correct |
17 |
Correct |
79 ms |
1408 KB |
Output is correct |
18 |
Correct |
75 ms |
1440 KB |
Output is correct |
19 |
Correct |
81 ms |
1528 KB |
Output is correct |
20 |
Correct |
83 ms |
1464 KB |
Output is correct |
21 |
Correct |
36 ms |
5760 KB |
Output is correct |
22 |
Correct |
44 ms |
5760 KB |
Output is correct |
23 |
Correct |
36 ms |
5632 KB |
Output is correct |
24 |
Correct |
37 ms |
5760 KB |
Output is correct |
25 |
Correct |
36 ms |
5632 KB |
Output is correct |
26 |
Correct |
30 ms |
5504 KB |
Output is correct |
27 |
Correct |
30 ms |
5632 KB |
Output is correct |
28 |
Correct |
30 ms |
5504 KB |
Output is correct |
29 |
Correct |
30 ms |
5504 KB |
Output is correct |
30 |
Correct |
423 ms |
6144 KB |
Output is correct |
31 |
Correct |
419 ms |
6392 KB |
Output is correct |
32 |
Correct |
425 ms |
6264 KB |
Output is correct |
33 |
Correct |
433 ms |
6392 KB |
Output is correct |
34 |
Correct |
418 ms |
6264 KB |
Output is correct |
35 |
Correct |
412 ms |
6264 KB |
Output is correct |
36 |
Correct |
414 ms |
6392 KB |
Output is correct |
37 |
Correct |
426 ms |
6372 KB |
Output is correct |
38 |
Correct |
415 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1536 KB |
Output is correct |
2 |
Correct |
12 ms |
1536 KB |
Output is correct |
3 |
Correct |
13 ms |
1536 KB |
Output is correct |
4 |
Correct |
12 ms |
1536 KB |
Output is correct |
5 |
Correct |
13 ms |
1536 KB |
Output is correct |
6 |
Correct |
15 ms |
1536 KB |
Output is correct |
7 |
Correct |
14 ms |
1536 KB |
Output is correct |
8 |
Correct |
15 ms |
1536 KB |
Output is correct |
9 |
Correct |
15 ms |
1536 KB |
Output is correct |
10 |
Correct |
15 ms |
1536 KB |
Output is correct |
11 |
Correct |
13 ms |
1536 KB |
Output is correct |
12 |
Correct |
12 ms |
1536 KB |
Output is correct |
13 |
Correct |
13 ms |
1536 KB |
Output is correct |
14 |
Correct |
13 ms |
1664 KB |
Output is correct |
15 |
Correct |
13 ms |
1664 KB |
Output is correct |
16 |
Correct |
6 ms |
1024 KB |
Output is correct |
17 |
Correct |
6 ms |
1024 KB |
Output is correct |
18 |
Correct |
6 ms |
1024 KB |
Output is correct |
19 |
Correct |
6 ms |
1024 KB |
Output is correct |
20 |
Correct |
6 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
453 ms |
6264 KB |
Output is correct |
2 |
Correct |
485 ms |
6264 KB |
Output is correct |
3 |
Correct |
451 ms |
6264 KB |
Output is correct |
4 |
Correct |
457 ms |
6264 KB |
Output is correct |
5 |
Correct |
452 ms |
6268 KB |
Output is correct |
6 |
Correct |
453 ms |
6264 KB |
Output is correct |
7 |
Correct |
451 ms |
6264 KB |
Output is correct |
8 |
Correct |
452 ms |
6264 KB |
Output is correct |
9 |
Correct |
446 ms |
6268 KB |
Output is correct |
10 |
Correct |
457 ms |
6260 KB |
Output is correct |
11 |
Correct |
494 ms |
6260 KB |
Output is correct |
12 |
Correct |
453 ms |
6264 KB |
Output is correct |
13 |
Correct |
454 ms |
6264 KB |
Output is correct |
14 |
Correct |
457 ms |
6264 KB |
Output is correct |
15 |
Correct |
470 ms |
6264 KB |
Output is correct |
16 |
Correct |
72 ms |
1408 KB |
Output is correct |
17 |
Correct |
79 ms |
1408 KB |
Output is correct |
18 |
Correct |
75 ms |
1440 KB |
Output is correct |
19 |
Correct |
81 ms |
1528 KB |
Output is correct |
20 |
Correct |
83 ms |
1464 KB |
Output is correct |
21 |
Correct |
36 ms |
5760 KB |
Output is correct |
22 |
Correct |
44 ms |
5760 KB |
Output is correct |
23 |
Correct |
36 ms |
5632 KB |
Output is correct |
24 |
Correct |
37 ms |
5760 KB |
Output is correct |
25 |
Correct |
36 ms |
5632 KB |
Output is correct |
26 |
Correct |
30 ms |
5504 KB |
Output is correct |
27 |
Correct |
30 ms |
5632 KB |
Output is correct |
28 |
Correct |
30 ms |
5504 KB |
Output is correct |
29 |
Correct |
30 ms |
5504 KB |
Output is correct |
30 |
Correct |
423 ms |
6144 KB |
Output is correct |
31 |
Correct |
419 ms |
6392 KB |
Output is correct |
32 |
Correct |
425 ms |
6264 KB |
Output is correct |
33 |
Correct |
433 ms |
6392 KB |
Output is correct |
34 |
Correct |
418 ms |
6264 KB |
Output is correct |
35 |
Correct |
412 ms |
6264 KB |
Output is correct |
36 |
Correct |
414 ms |
6392 KB |
Output is correct |
37 |
Correct |
426 ms |
6372 KB |
Output is correct |
38 |
Correct |
415 ms |
6264 KB |
Output is correct |
39 |
Correct |
13 ms |
1536 KB |
Output is correct |
40 |
Correct |
12 ms |
1536 KB |
Output is correct |
41 |
Correct |
13 ms |
1536 KB |
Output is correct |
42 |
Correct |
12 ms |
1536 KB |
Output is correct |
43 |
Correct |
13 ms |
1536 KB |
Output is correct |
44 |
Correct |
15 ms |
1536 KB |
Output is correct |
45 |
Correct |
14 ms |
1536 KB |
Output is correct |
46 |
Correct |
15 ms |
1536 KB |
Output is correct |
47 |
Correct |
15 ms |
1536 KB |
Output is correct |
48 |
Correct |
15 ms |
1536 KB |
Output is correct |
49 |
Correct |
13 ms |
1536 KB |
Output is correct |
50 |
Correct |
12 ms |
1536 KB |
Output is correct |
51 |
Correct |
13 ms |
1536 KB |
Output is correct |
52 |
Correct |
13 ms |
1664 KB |
Output is correct |
53 |
Correct |
13 ms |
1664 KB |
Output is correct |
54 |
Correct |
6 ms |
1024 KB |
Output is correct |
55 |
Correct |
6 ms |
1024 KB |
Output is correct |
56 |
Correct |
6 ms |
1024 KB |
Output is correct |
57 |
Correct |
6 ms |
1024 KB |
Output is correct |
58 |
Correct |
6 ms |
1024 KB |
Output is correct |
59 |
Correct |
1321 ms |
6284 KB |
Output is correct |
60 |
Correct |
1275 ms |
6288 KB |
Output is correct |
61 |
Correct |
1279 ms |
6392 KB |
Output is correct |
62 |
Correct |
1313 ms |
6288 KB |
Output is correct |
63 |
Correct |
1306 ms |
6272 KB |
Output is correct |
64 |
Correct |
1553 ms |
6392 KB |
Output is correct |
65 |
Correct |
1539 ms |
6392 KB |
Output is correct |
66 |
Correct |
1553 ms |
6272 KB |
Output is correct |
67 |
Correct |
1538 ms |
6392 KB |
Output is correct |
68 |
Correct |
1553 ms |
6272 KB |
Output is correct |
69 |
Correct |
1274 ms |
6392 KB |
Output is correct |
70 |
Correct |
1281 ms |
6392 KB |
Output is correct |
71 |
Correct |
1287 ms |
6392 KB |
Output is correct |
72 |
Correct |
1303 ms |
6392 KB |
Output is correct |
73 |
Correct |
1299 ms |
6392 KB |
Output is correct |
74 |
Correct |
136 ms |
1408 KB |
Output is correct |
75 |
Correct |
135 ms |
1408 KB |
Output is correct |
76 |
Correct |
138 ms |
1408 KB |
Output is correct |
77 |
Correct |
133 ms |
1408 KB |
Output is correct |
78 |
Correct |
136 ms |
1456 KB |
Output is correct |