#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 520;
const int M = 1e9 + 7;
int a[N], b[N];
int d[N * 2];
int dp[2][N * 2][N];
int n, m;
int pm(int a, int b){
if(b == 0) return 1;
return pm(a * a % M, b / 2) * (b % 2 ? a : 1) % M;
}
int mi(int a){
return pm(a, M - 2);
}
int inv[N];
int32_t main(){
cin >> n;
for(int i = 0; i <= N; i++) inv[i] = mi(i);
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
}
vector<int> sp;
sp.push_back(0);
sp.push_back(1);
for(int i = 0; i < n; i++){
sp.push_back(a[i]);
sp.push_back(b[i] + 1);
}
sort(sp.begin(), sp.end());
sp.resize(unique(sp.begin(), sp.end()) - sp.begin());
m = sp.size();
{
map<int, int> M;
for(int i = 0; i < m; i++) M[sp[i]] = i;
for(int i = 0; i < n; i++) a[i] = M[a[i]], b[i] = M[b[i] + 1];
}
for(int i = 0; i < m - 1; i++){
d[i] = sp[i + 1] - sp[i];
}
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
for(int k = 1; k <= n; k++){
dp[(i + 1) % 2][j][k] = dp[i % 2][j][k];
}
}
int s[m + 1] = {0};
for(int j = 0; j < m; j++){
for(int k = 0; k <= n; k++){
s[j + 1] += dp[i % 2][j][k];
}
s[j] %= M;
s[j + 1] += s[j];
}
for(int j = a[i]; j < b[i]; j++){
dp[(i + 1) % 2][j][1] += (s[j] + 1) * d[j] % M;
for(int k = 1; k < n; k++){
dp[(i + 1) % 2][j][k + 1] += dp[i % 2][j][k] * (d[j] - k) % M * inv[k + 1] % M;
}
}
for(int j = 0; j < m; j++){
for(int k = 1; k <= n; k++){
dp[(i + 1) % 2][j][k] %= M;
}
}
}
for(int j = 0; j < m; j++){
for(int k = 1; k <= n; k++){
ans += dp[n % 2][j][k];
}
}
ans %= M;
cout << ans << endl;
}
Compilation message
boat.cpp: In function 'int32_t main()':
boat.cpp:27:37: warning: iteration 520 invokes undefined behavior [-Waggressive-loop-optimizations]
27 | for(int i = 0; i <= N; i++) inv[i] = mi(i);
| ~~~~~~~^~~~~~~
boat.cpp:27:19: note: within this loop
27 | for(int i = 0; i <= N; i++) inv[i] = mi(i);
| ~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
580 ms |
8548 KB |
Output is correct |
2 |
Correct |
605 ms |
8556 KB |
Output is correct |
3 |
Correct |
594 ms |
8560 KB |
Output is correct |
4 |
Correct |
597 ms |
8560 KB |
Output is correct |
5 |
Correct |
616 ms |
8560 KB |
Output is correct |
6 |
Correct |
612 ms |
8556 KB |
Output is correct |
7 |
Correct |
608 ms |
8556 KB |
Output is correct |
8 |
Correct |
593 ms |
8652 KB |
Output is correct |
9 |
Correct |
608 ms |
8560 KB |
Output is correct |
10 |
Correct |
611 ms |
8652 KB |
Output is correct |
11 |
Correct |
579 ms |
8564 KB |
Output is correct |
12 |
Correct |
629 ms |
8564 KB |
Output is correct |
13 |
Correct |
651 ms |
8556 KB |
Output is correct |
14 |
Correct |
605 ms |
8652 KB |
Output is correct |
15 |
Correct |
589 ms |
8532 KB |
Output is correct |
16 |
Correct |
79 ms |
1748 KB |
Output is correct |
17 |
Correct |
85 ms |
1920 KB |
Output is correct |
18 |
Correct |
109 ms |
1852 KB |
Output is correct |
19 |
Correct |
86 ms |
1920 KB |
Output is correct |
20 |
Correct |
82 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
580 ms |
8548 KB |
Output is correct |
2 |
Correct |
605 ms |
8556 KB |
Output is correct |
3 |
Correct |
594 ms |
8560 KB |
Output is correct |
4 |
Correct |
597 ms |
8560 KB |
Output is correct |
5 |
Correct |
616 ms |
8560 KB |
Output is correct |
6 |
Correct |
612 ms |
8556 KB |
Output is correct |
7 |
Correct |
608 ms |
8556 KB |
Output is correct |
8 |
Correct |
593 ms |
8652 KB |
Output is correct |
9 |
Correct |
608 ms |
8560 KB |
Output is correct |
10 |
Correct |
611 ms |
8652 KB |
Output is correct |
11 |
Correct |
579 ms |
8564 KB |
Output is correct |
12 |
Correct |
629 ms |
8564 KB |
Output is correct |
13 |
Correct |
651 ms |
8556 KB |
Output is correct |
14 |
Correct |
605 ms |
8652 KB |
Output is correct |
15 |
Correct |
589 ms |
8532 KB |
Output is correct |
16 |
Correct |
79 ms |
1748 KB |
Output is correct |
17 |
Correct |
85 ms |
1920 KB |
Output is correct |
18 |
Correct |
109 ms |
1852 KB |
Output is correct |
19 |
Correct |
86 ms |
1920 KB |
Output is correct |
20 |
Correct |
82 ms |
1876 KB |
Output is correct |
21 |
Correct |
771 ms |
7828 KB |
Output is correct |
22 |
Correct |
784 ms |
7920 KB |
Output is correct |
23 |
Correct |
734 ms |
7788 KB |
Output is correct |
24 |
Correct |
800 ms |
7732 KB |
Output is correct |
25 |
Correct |
780 ms |
7764 KB |
Output is correct |
26 |
Correct |
865 ms |
7572 KB |
Output is correct |
27 |
Correct |
934 ms |
7696 KB |
Output is correct |
28 |
Correct |
917 ms |
7560 KB |
Output is correct |
29 |
Correct |
844 ms |
7524 KB |
Output is correct |
30 |
Correct |
600 ms |
8532 KB |
Output is correct |
31 |
Correct |
629 ms |
8652 KB |
Output is correct |
32 |
Correct |
574 ms |
8532 KB |
Output is correct |
33 |
Correct |
621 ms |
8532 KB |
Output is correct |
34 |
Correct |
620 ms |
8532 KB |
Output is correct |
35 |
Correct |
599 ms |
8544 KB |
Output is correct |
36 |
Correct |
632 ms |
8540 KB |
Output is correct |
37 |
Correct |
562 ms |
8532 KB |
Output is correct |
38 |
Correct |
592 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1876 KB |
Output is correct |
2 |
Correct |
8 ms |
1992 KB |
Output is correct |
3 |
Correct |
8 ms |
1992 KB |
Output is correct |
4 |
Correct |
8 ms |
1876 KB |
Output is correct |
5 |
Correct |
8 ms |
1996 KB |
Output is correct |
6 |
Correct |
9 ms |
1876 KB |
Output is correct |
7 |
Correct |
9 ms |
1876 KB |
Output is correct |
8 |
Correct |
10 ms |
1876 KB |
Output is correct |
9 |
Correct |
10 ms |
1876 KB |
Output is correct |
10 |
Correct |
9 ms |
1876 KB |
Output is correct |
11 |
Correct |
8 ms |
1996 KB |
Output is correct |
12 |
Correct |
7 ms |
2000 KB |
Output is correct |
13 |
Correct |
8 ms |
1876 KB |
Output is correct |
14 |
Correct |
10 ms |
2000 KB |
Output is correct |
15 |
Correct |
8 ms |
1996 KB |
Output is correct |
16 |
Correct |
5 ms |
1200 KB |
Output is correct |
17 |
Correct |
5 ms |
1236 KB |
Output is correct |
18 |
Correct |
6 ms |
1236 KB |
Output is correct |
19 |
Correct |
5 ms |
1236 KB |
Output is correct |
20 |
Correct |
5 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
580 ms |
8548 KB |
Output is correct |
2 |
Correct |
605 ms |
8556 KB |
Output is correct |
3 |
Correct |
594 ms |
8560 KB |
Output is correct |
4 |
Correct |
597 ms |
8560 KB |
Output is correct |
5 |
Correct |
616 ms |
8560 KB |
Output is correct |
6 |
Correct |
612 ms |
8556 KB |
Output is correct |
7 |
Correct |
608 ms |
8556 KB |
Output is correct |
8 |
Correct |
593 ms |
8652 KB |
Output is correct |
9 |
Correct |
608 ms |
8560 KB |
Output is correct |
10 |
Correct |
611 ms |
8652 KB |
Output is correct |
11 |
Correct |
579 ms |
8564 KB |
Output is correct |
12 |
Correct |
629 ms |
8564 KB |
Output is correct |
13 |
Correct |
651 ms |
8556 KB |
Output is correct |
14 |
Correct |
605 ms |
8652 KB |
Output is correct |
15 |
Correct |
589 ms |
8532 KB |
Output is correct |
16 |
Correct |
79 ms |
1748 KB |
Output is correct |
17 |
Correct |
85 ms |
1920 KB |
Output is correct |
18 |
Correct |
109 ms |
1852 KB |
Output is correct |
19 |
Correct |
86 ms |
1920 KB |
Output is correct |
20 |
Correct |
82 ms |
1876 KB |
Output is correct |
21 |
Correct |
771 ms |
7828 KB |
Output is correct |
22 |
Correct |
784 ms |
7920 KB |
Output is correct |
23 |
Correct |
734 ms |
7788 KB |
Output is correct |
24 |
Correct |
800 ms |
7732 KB |
Output is correct |
25 |
Correct |
780 ms |
7764 KB |
Output is correct |
26 |
Correct |
865 ms |
7572 KB |
Output is correct |
27 |
Correct |
934 ms |
7696 KB |
Output is correct |
28 |
Correct |
917 ms |
7560 KB |
Output is correct |
29 |
Correct |
844 ms |
7524 KB |
Output is correct |
30 |
Correct |
600 ms |
8532 KB |
Output is correct |
31 |
Correct |
629 ms |
8652 KB |
Output is correct |
32 |
Correct |
574 ms |
8532 KB |
Output is correct |
33 |
Correct |
621 ms |
8532 KB |
Output is correct |
34 |
Correct |
620 ms |
8532 KB |
Output is correct |
35 |
Correct |
599 ms |
8544 KB |
Output is correct |
36 |
Correct |
632 ms |
8540 KB |
Output is correct |
37 |
Correct |
562 ms |
8532 KB |
Output is correct |
38 |
Correct |
592 ms |
8532 KB |
Output is correct |
39 |
Correct |
8 ms |
1876 KB |
Output is correct |
40 |
Correct |
8 ms |
1992 KB |
Output is correct |
41 |
Correct |
8 ms |
1992 KB |
Output is correct |
42 |
Correct |
8 ms |
1876 KB |
Output is correct |
43 |
Correct |
8 ms |
1996 KB |
Output is correct |
44 |
Correct |
9 ms |
1876 KB |
Output is correct |
45 |
Correct |
9 ms |
1876 KB |
Output is correct |
46 |
Correct |
10 ms |
1876 KB |
Output is correct |
47 |
Correct |
10 ms |
1876 KB |
Output is correct |
48 |
Correct |
9 ms |
1876 KB |
Output is correct |
49 |
Correct |
8 ms |
1996 KB |
Output is correct |
50 |
Correct |
7 ms |
2000 KB |
Output is correct |
51 |
Correct |
8 ms |
1876 KB |
Output is correct |
52 |
Correct |
10 ms |
2000 KB |
Output is correct |
53 |
Correct |
8 ms |
1996 KB |
Output is correct |
54 |
Correct |
5 ms |
1200 KB |
Output is correct |
55 |
Correct |
5 ms |
1236 KB |
Output is correct |
56 |
Correct |
6 ms |
1236 KB |
Output is correct |
57 |
Correct |
5 ms |
1236 KB |
Output is correct |
58 |
Correct |
5 ms |
1236 KB |
Output is correct |
59 |
Correct |
878 ms |
8560 KB |
Output is correct |
60 |
Correct |
885 ms |
8560 KB |
Output is correct |
61 |
Correct |
867 ms |
8560 KB |
Output is correct |
62 |
Correct |
894 ms |
8556 KB |
Output is correct |
63 |
Correct |
840 ms |
8556 KB |
Output is correct |
64 |
Correct |
1108 ms |
8656 KB |
Output is correct |
65 |
Correct |
1024 ms |
8552 KB |
Output is correct |
66 |
Correct |
1016 ms |
8652 KB |
Output is correct |
67 |
Correct |
1014 ms |
8560 KB |
Output is correct |
68 |
Correct |
998 ms |
8556 KB |
Output is correct |
69 |
Correct |
867 ms |
8572 KB |
Output is correct |
70 |
Correct |
856 ms |
8556 KB |
Output is correct |
71 |
Correct |
878 ms |
8628 KB |
Output is correct |
72 |
Correct |
850 ms |
8532 KB |
Output is correct |
73 |
Correct |
834 ms |
8564 KB |
Output is correct |
74 |
Correct |
136 ms |
1748 KB |
Output is correct |
75 |
Correct |
136 ms |
1892 KB |
Output is correct |
76 |
Correct |
142 ms |
1876 KB |
Output is correct |
77 |
Correct |
141 ms |
1856 KB |
Output is correct |
78 |
Correct |
134 ms |
1836 KB |
Output is correct |