#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
using namespace std;
const int MOD = (int) 1e9 + 7;
const int MAXN = 500;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1)
ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
int dp[MAXN + 1][2 * MAXN + 1];
int main() {
//ifstream cin("C.in");
//ofstream cout("C.out");
int i, j, n;
ios::sync_with_stdio(false);
cin >> n;
vector <int> a(n + 1), b(n + 1);
vector <int> nrm;
for(i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
b[i]++;
nrm.push_back(a[i]);
nrm.push_back(b[i]);
}
sort(nrm.begin(), nrm.end());
nrm.resize(unique(nrm.begin(), nrm.end()) - nrm.begin());
for(i = 1; i <= n; i++) {
a[i] = lower_bound(nrm.begin(), nrm.end(), a[i]) - nrm.begin();
b[i] = lower_bound(nrm.begin(), nrm.end(), b[i]) - nrm.begin();
}
vector <int> inv(n + 1);
for(i = 1; i <= n; i++) {
inv[i] = lgput(i, MOD - 2);
}
vector <int> len(2 * n);
for(i = 0; i < nrm.size(); i++) {
if(i + 1 < nrm.size()) {
len[i] = nrm[i + 1] - nrm[i];
}
dp[0][i] = 1;
}
for(i = 1; i <= n; i++) {
dp[i][0] = 1;
for(j = a[i] + 1; j <= b[i]; j++) {
dp[i][j] = (1LL * dp[i - 1][j - 1] * len[j - 1]) % MOD;
int coef = len[j - 1] - 1 + MOD;
int nr = 1;
for(int p = i - 2; p >= 0; p--) {
if(a[p + 1] < j && j <= b[p + 1]) {
nr++;
// comb(len + nr - 2, nr)
coef = (1LL * coef * (len[j - 1] + nr - 2)) % MOD;
coef = (1LL * coef * inv[nr]) % MOD;
dp[i][j] = (dp[i][j] + 1LL * coef * dp[p][j - 1]) % MOD;
}
}
}
for(j = 1; j < nrm.size(); j++) {
dp[i][j] = (1LL * dp[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
}
}
cout << (dp[n][nrm.size() - 1] - 1 + MOD) % MOD;
//cin.close();
//cout.close();
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:53:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < nrm.size(); i++) {
~~^~~~~~~~~~~~
boat.cpp:54:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i + 1 < nrm.size()) {
~~~~~~^~~~~~~~~~~~
boat.cpp:75:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 1; j < nrm.size(); j++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
10 ms |
2584 KB |
Output is correct |
3 |
Correct |
11 ms |
2584 KB |
Output is correct |
4 |
Correct |
9 ms |
2584 KB |
Output is correct |
5 |
Correct |
9 ms |
2584 KB |
Output is correct |
6 |
Correct |
10 ms |
2584 KB |
Output is correct |
7 |
Correct |
9 ms |
2584 KB |
Output is correct |
8 |
Correct |
9 ms |
2584 KB |
Output is correct |
9 |
Correct |
9 ms |
2584 KB |
Output is correct |
10 |
Correct |
9 ms |
2584 KB |
Output is correct |
11 |
Correct |
9 ms |
2584 KB |
Output is correct |
12 |
Correct |
9 ms |
2584 KB |
Output is correct |
13 |
Correct |
9 ms |
2584 KB |
Output is correct |
14 |
Correct |
9 ms |
2584 KB |
Output is correct |
15 |
Correct |
9 ms |
2584 KB |
Output is correct |
16 |
Correct |
5 ms |
2584 KB |
Output is correct |
17 |
Correct |
5 ms |
2584 KB |
Output is correct |
18 |
Correct |
5 ms |
2584 KB |
Output is correct |
19 |
Correct |
5 ms |
2584 KB |
Output is correct |
20 |
Correct |
6 ms |
2584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
10 ms |
2584 KB |
Output is correct |
3 |
Correct |
11 ms |
2584 KB |
Output is correct |
4 |
Correct |
9 ms |
2584 KB |
Output is correct |
5 |
Correct |
9 ms |
2584 KB |
Output is correct |
6 |
Correct |
10 ms |
2584 KB |
Output is correct |
7 |
Correct |
9 ms |
2584 KB |
Output is correct |
8 |
Correct |
9 ms |
2584 KB |
Output is correct |
9 |
Correct |
9 ms |
2584 KB |
Output is correct |
10 |
Correct |
9 ms |
2584 KB |
Output is correct |
11 |
Correct |
9 ms |
2584 KB |
Output is correct |
12 |
Correct |
9 ms |
2584 KB |
Output is correct |
13 |
Correct |
9 ms |
2584 KB |
Output is correct |
14 |
Correct |
9 ms |
2584 KB |
Output is correct |
15 |
Correct |
9 ms |
2584 KB |
Output is correct |
16 |
Correct |
5 ms |
2584 KB |
Output is correct |
17 |
Correct |
5 ms |
2584 KB |
Output is correct |
18 |
Correct |
5 ms |
2584 KB |
Output is correct |
19 |
Correct |
5 ms |
2584 KB |
Output is correct |
20 |
Correct |
6 ms |
2584 KB |
Output is correct |
21 |
Correct |
264 ms |
2584 KB |
Output is correct |
22 |
Correct |
240 ms |
2676 KB |
Output is correct |
23 |
Correct |
209 ms |
2676 KB |
Output is correct |
24 |
Correct |
242 ms |
2760 KB |
Output is correct |
25 |
Correct |
257 ms |
2772 KB |
Output is correct |
26 |
Correct |
533 ms |
2796 KB |
Output is correct |
27 |
Correct |
549 ms |
2800 KB |
Output is correct |
28 |
Correct |
527 ms |
2800 KB |
Output is correct |
29 |
Correct |
522 ms |
2800 KB |
Output is correct |
30 |
Correct |
9 ms |
2800 KB |
Output is correct |
31 |
Correct |
9 ms |
2884 KB |
Output is correct |
32 |
Correct |
10 ms |
2884 KB |
Output is correct |
33 |
Correct |
9 ms |
2884 KB |
Output is correct |
34 |
Correct |
9 ms |
2884 KB |
Output is correct |
35 |
Correct |
10 ms |
2884 KB |
Output is correct |
36 |
Correct |
10 ms |
2884 KB |
Output is correct |
37 |
Correct |
10 ms |
2884 KB |
Output is correct |
38 |
Correct |
10 ms |
2884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2884 KB |
Output is correct |
2 |
Correct |
4 ms |
2884 KB |
Output is correct |
3 |
Correct |
5 ms |
2884 KB |
Output is correct |
4 |
Correct |
5 ms |
2884 KB |
Output is correct |
5 |
Correct |
5 ms |
2884 KB |
Output is correct |
6 |
Correct |
7 ms |
2884 KB |
Output is correct |
7 |
Correct |
8 ms |
2884 KB |
Output is correct |
8 |
Correct |
7 ms |
2884 KB |
Output is correct |
9 |
Correct |
7 ms |
2884 KB |
Output is correct |
10 |
Correct |
8 ms |
2884 KB |
Output is correct |
11 |
Correct |
5 ms |
2884 KB |
Output is correct |
12 |
Correct |
4 ms |
2884 KB |
Output is correct |
13 |
Correct |
5 ms |
2884 KB |
Output is correct |
14 |
Correct |
4 ms |
2884 KB |
Output is correct |
15 |
Correct |
4 ms |
2884 KB |
Output is correct |
16 |
Correct |
4 ms |
2884 KB |
Output is correct |
17 |
Correct |
4 ms |
2884 KB |
Output is correct |
18 |
Correct |
4 ms |
2884 KB |
Output is correct |
19 |
Correct |
4 ms |
2884 KB |
Output is correct |
20 |
Correct |
4 ms |
2884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
10 ms |
2584 KB |
Output is correct |
3 |
Correct |
11 ms |
2584 KB |
Output is correct |
4 |
Correct |
9 ms |
2584 KB |
Output is correct |
5 |
Correct |
9 ms |
2584 KB |
Output is correct |
6 |
Correct |
10 ms |
2584 KB |
Output is correct |
7 |
Correct |
9 ms |
2584 KB |
Output is correct |
8 |
Correct |
9 ms |
2584 KB |
Output is correct |
9 |
Correct |
9 ms |
2584 KB |
Output is correct |
10 |
Correct |
9 ms |
2584 KB |
Output is correct |
11 |
Correct |
9 ms |
2584 KB |
Output is correct |
12 |
Correct |
9 ms |
2584 KB |
Output is correct |
13 |
Correct |
9 ms |
2584 KB |
Output is correct |
14 |
Correct |
9 ms |
2584 KB |
Output is correct |
15 |
Correct |
9 ms |
2584 KB |
Output is correct |
16 |
Correct |
5 ms |
2584 KB |
Output is correct |
17 |
Correct |
5 ms |
2584 KB |
Output is correct |
18 |
Correct |
5 ms |
2584 KB |
Output is correct |
19 |
Correct |
5 ms |
2584 KB |
Output is correct |
20 |
Correct |
6 ms |
2584 KB |
Output is correct |
21 |
Correct |
264 ms |
2584 KB |
Output is correct |
22 |
Correct |
240 ms |
2676 KB |
Output is correct |
23 |
Correct |
209 ms |
2676 KB |
Output is correct |
24 |
Correct |
242 ms |
2760 KB |
Output is correct |
25 |
Correct |
257 ms |
2772 KB |
Output is correct |
26 |
Correct |
533 ms |
2796 KB |
Output is correct |
27 |
Correct |
549 ms |
2800 KB |
Output is correct |
28 |
Correct |
527 ms |
2800 KB |
Output is correct |
29 |
Correct |
522 ms |
2800 KB |
Output is correct |
30 |
Correct |
9 ms |
2800 KB |
Output is correct |
31 |
Correct |
9 ms |
2884 KB |
Output is correct |
32 |
Correct |
10 ms |
2884 KB |
Output is correct |
33 |
Correct |
9 ms |
2884 KB |
Output is correct |
34 |
Correct |
9 ms |
2884 KB |
Output is correct |
35 |
Correct |
10 ms |
2884 KB |
Output is correct |
36 |
Correct |
10 ms |
2884 KB |
Output is correct |
37 |
Correct |
10 ms |
2884 KB |
Output is correct |
38 |
Correct |
10 ms |
2884 KB |
Output is correct |
39 |
Correct |
4 ms |
2884 KB |
Output is correct |
40 |
Correct |
4 ms |
2884 KB |
Output is correct |
41 |
Correct |
5 ms |
2884 KB |
Output is correct |
42 |
Correct |
5 ms |
2884 KB |
Output is correct |
43 |
Correct |
5 ms |
2884 KB |
Output is correct |
44 |
Correct |
7 ms |
2884 KB |
Output is correct |
45 |
Correct |
8 ms |
2884 KB |
Output is correct |
46 |
Correct |
7 ms |
2884 KB |
Output is correct |
47 |
Correct |
7 ms |
2884 KB |
Output is correct |
48 |
Correct |
8 ms |
2884 KB |
Output is correct |
49 |
Correct |
5 ms |
2884 KB |
Output is correct |
50 |
Correct |
4 ms |
2884 KB |
Output is correct |
51 |
Correct |
5 ms |
2884 KB |
Output is correct |
52 |
Correct |
4 ms |
2884 KB |
Output is correct |
53 |
Correct |
4 ms |
2884 KB |
Output is correct |
54 |
Correct |
4 ms |
2884 KB |
Output is correct |
55 |
Correct |
4 ms |
2884 KB |
Output is correct |
56 |
Correct |
4 ms |
2884 KB |
Output is correct |
57 |
Correct |
4 ms |
2884 KB |
Output is correct |
58 |
Correct |
4 ms |
2884 KB |
Output is correct |
59 |
Correct |
277 ms |
2940 KB |
Output is correct |
60 |
Correct |
244 ms |
3132 KB |
Output is correct |
61 |
Correct |
223 ms |
3132 KB |
Output is correct |
62 |
Correct |
265 ms |
3132 KB |
Output is correct |
63 |
Correct |
249 ms |
3132 KB |
Output is correct |
64 |
Correct |
608 ms |
3132 KB |
Output is correct |
65 |
Correct |
611 ms |
3188 KB |
Output is correct |
66 |
Correct |
608 ms |
3308 KB |
Output is correct |
67 |
Correct |
603 ms |
3308 KB |
Output is correct |
68 |
Correct |
602 ms |
3308 KB |
Output is correct |
69 |
Correct |
219 ms |
3308 KB |
Output is correct |
70 |
Correct |
239 ms |
3308 KB |
Output is correct |
71 |
Correct |
229 ms |
3308 KB |
Output is correct |
72 |
Correct |
236 ms |
3308 KB |
Output is correct |
73 |
Correct |
242 ms |
3308 KB |
Output is correct |
74 |
Correct |
58 ms |
3308 KB |
Output is correct |
75 |
Correct |
55 ms |
3344 KB |
Output is correct |
76 |
Correct |
59 ms |
3344 KB |
Output is correct |
77 |
Correct |
57 ms |
3344 KB |
Output is correct |
78 |
Correct |
61 ms |
3344 KB |
Output is correct |