#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 1010;
class APIO16_boat {
void add(long &a, long b) {
a += b;
if(a >= MOD) a -= MOD;
}
long mul(long a, long b) { return a*b % MOD; }
long modPow(long b, long e) {
if(e == 0) return 1;
if(e == 1) return b;
return mul(modPow(mul(b, b), e >> 1), modPow(b, e & 1));
}
int n, a[N], b[N];
pair<int,int> segs[N];
vector<int> vals;
unordered_map<int, int> rev;
long ans, inv[N], C[N][N], f[N][N], dp[N][N];
public:
void solve(istream &cin, ostream &cout) {
cin >> n;
vals.push_back(0);
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
vals.push_back(a[i]);
vals.push_back(b[i]+1);
}
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
for(int i = 0; i < vals.size(); i++) rev[vals[i]] = i;
for(int i = 1; i <= n; i++) segs[i] = {rev[a[i]], rev[b[i]+1]};
inv[0] = 1;
for(int i = 1; i <= n; i++) inv[i] = modPow(i, MOD-2);
C[0][0] = 1;
for(int i = 1; i <= n; i++) {
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; j++) {
add(C[i][j], C[i-1][j-1]);
add(C[i][j], C[i-1][j]);
}
}
for(int j = 1; j < vals.size()-1; j++) {
f[j][0] = 1;
for(int cnt = 1; cnt <= min(vals[j+1] - vals[j], n); cnt++) {
f[j][cnt] = mul(f[j][cnt - 1], vals[j+1] - vals[j] - (cnt - 1));
f[j][cnt] = mul(f[j][cnt], inv[cnt]);
}
for(int cnt = n; cnt >= 1; cnt--) {
for(int x = 1; x <= min(cnt-1, n); x++) {
add(f[j][cnt], mul(f[j][x], C[cnt-1][x-1]));
}
}
}
for(int j = 0; j < vals.size(); j++) dp[0][j] = 1;
for(int i = 1; i <= n; i++) {
for(int j = segs[i].first; j < segs[i].second; j++) {
int cnt = 0;
for(int k = i-1; k >= 0; k--) {
cnt += segs[k+1].first <= j && j < segs[k+1].second;
add(dp[i][j], mul(dp[k][j-1], f[j][cnt]));
}
}
for(int j = 1; j < vals.size(); j++) add(dp[i][j], dp[i][j-1]);
add(ans, dp[i][vals.size() - 1]);
}
cout << ans << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
APIO16_boat *obj = new APIO16_boat();
obj->solve(in, out);
delete obj;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
Compilation message
boat.cpp: In member function 'void APIO16_boat::solve(std::istream&, std::ostream&)':
boat.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < vals.size(); i++) rev[vals[i]] = i;
~~^~~~~~~~~~~~~
boat.cpp:55:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < vals.size()-1; j++) {
~~^~~~~~~~~~~~~~~
boat.cpp:68:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < vals.size(); j++) dp[0][j] = 1;
~~^~~~~~~~~~~~~
boat.cpp:77:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < vals.size(); j++) add(dp[i][j], dp[i][j-1]);
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
24440 KB |
Output is correct |
2 |
Correct |
459 ms |
24700 KB |
Output is correct |
3 |
Correct |
448 ms |
24700 KB |
Output is correct |
4 |
Correct |
440 ms |
24700 KB |
Output is correct |
5 |
Correct |
472 ms |
24700 KB |
Output is correct |
6 |
Correct |
441 ms |
24736 KB |
Output is correct |
7 |
Correct |
459 ms |
24736 KB |
Output is correct |
8 |
Correct |
436 ms |
24736 KB |
Output is correct |
9 |
Correct |
458 ms |
24736 KB |
Output is correct |
10 |
Correct |
453 ms |
24972 KB |
Output is correct |
11 |
Correct |
455 ms |
24972 KB |
Output is correct |
12 |
Correct |
448 ms |
24972 KB |
Output is correct |
13 |
Correct |
458 ms |
24972 KB |
Output is correct |
14 |
Correct |
462 ms |
24972 KB |
Output is correct |
15 |
Correct |
474 ms |
24972 KB |
Output is correct |
16 |
Correct |
98 ms |
24972 KB |
Output is correct |
17 |
Correct |
102 ms |
24972 KB |
Output is correct |
18 |
Correct |
99 ms |
24972 KB |
Output is correct |
19 |
Correct |
105 ms |
25080 KB |
Output is correct |
20 |
Correct |
99 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
24440 KB |
Output is correct |
2 |
Correct |
459 ms |
24700 KB |
Output is correct |
3 |
Correct |
448 ms |
24700 KB |
Output is correct |
4 |
Correct |
440 ms |
24700 KB |
Output is correct |
5 |
Correct |
472 ms |
24700 KB |
Output is correct |
6 |
Correct |
441 ms |
24736 KB |
Output is correct |
7 |
Correct |
459 ms |
24736 KB |
Output is correct |
8 |
Correct |
436 ms |
24736 KB |
Output is correct |
9 |
Correct |
458 ms |
24736 KB |
Output is correct |
10 |
Correct |
453 ms |
24972 KB |
Output is correct |
11 |
Correct |
455 ms |
24972 KB |
Output is correct |
12 |
Correct |
448 ms |
24972 KB |
Output is correct |
13 |
Correct |
458 ms |
24972 KB |
Output is correct |
14 |
Correct |
462 ms |
24972 KB |
Output is correct |
15 |
Correct |
474 ms |
24972 KB |
Output is correct |
16 |
Correct |
98 ms |
24972 KB |
Output is correct |
17 |
Correct |
102 ms |
24972 KB |
Output is correct |
18 |
Correct |
99 ms |
24972 KB |
Output is correct |
19 |
Correct |
105 ms |
25080 KB |
Output is correct |
20 |
Correct |
99 ms |
25080 KB |
Output is correct |
21 |
Correct |
587 ms |
25080 KB |
Output is correct |
22 |
Correct |
612 ms |
25080 KB |
Output is correct |
23 |
Correct |
607 ms |
25080 KB |
Output is correct |
24 |
Correct |
624 ms |
25080 KB |
Output is correct |
25 |
Correct |
588 ms |
25080 KB |
Output is correct |
26 |
Correct |
674 ms |
25080 KB |
Output is correct |
27 |
Correct |
712 ms |
25080 KB |
Output is correct |
28 |
Correct |
673 ms |
25088 KB |
Output is correct |
29 |
Correct |
674 ms |
25096 KB |
Output is correct |
30 |
Correct |
439 ms |
25104 KB |
Output is correct |
31 |
Correct |
452 ms |
25104 KB |
Output is correct |
32 |
Correct |
450 ms |
25180 KB |
Output is correct |
33 |
Correct |
450 ms |
25184 KB |
Output is correct |
34 |
Correct |
458 ms |
25184 KB |
Output is correct |
35 |
Correct |
452 ms |
25208 KB |
Output is correct |
36 |
Correct |
442 ms |
25348 KB |
Output is correct |
37 |
Correct |
456 ms |
25348 KB |
Output is correct |
38 |
Correct |
445 ms |
25348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25348 KB |
Output is correct |
2 |
Correct |
26 ms |
25348 KB |
Output is correct |
3 |
Correct |
26 ms |
25348 KB |
Output is correct |
4 |
Correct |
26 ms |
25348 KB |
Output is correct |
5 |
Correct |
26 ms |
25348 KB |
Output is correct |
6 |
Correct |
27 ms |
25348 KB |
Output is correct |
7 |
Correct |
24 ms |
25348 KB |
Output is correct |
8 |
Correct |
27 ms |
25348 KB |
Output is correct |
9 |
Correct |
24 ms |
25348 KB |
Output is correct |
10 |
Correct |
24 ms |
25348 KB |
Output is correct |
11 |
Correct |
27 ms |
25348 KB |
Output is correct |
12 |
Correct |
25 ms |
25348 KB |
Output is correct |
13 |
Correct |
26 ms |
25348 KB |
Output is correct |
14 |
Correct |
26 ms |
25348 KB |
Output is correct |
15 |
Correct |
26 ms |
25348 KB |
Output is correct |
16 |
Correct |
24 ms |
25348 KB |
Output is correct |
17 |
Correct |
24 ms |
25348 KB |
Output is correct |
18 |
Correct |
24 ms |
25348 KB |
Output is correct |
19 |
Correct |
23 ms |
25348 KB |
Output is correct |
20 |
Correct |
25 ms |
25348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
24440 KB |
Output is correct |
2 |
Correct |
459 ms |
24700 KB |
Output is correct |
3 |
Correct |
448 ms |
24700 KB |
Output is correct |
4 |
Correct |
440 ms |
24700 KB |
Output is correct |
5 |
Correct |
472 ms |
24700 KB |
Output is correct |
6 |
Correct |
441 ms |
24736 KB |
Output is correct |
7 |
Correct |
459 ms |
24736 KB |
Output is correct |
8 |
Correct |
436 ms |
24736 KB |
Output is correct |
9 |
Correct |
458 ms |
24736 KB |
Output is correct |
10 |
Correct |
453 ms |
24972 KB |
Output is correct |
11 |
Correct |
455 ms |
24972 KB |
Output is correct |
12 |
Correct |
448 ms |
24972 KB |
Output is correct |
13 |
Correct |
458 ms |
24972 KB |
Output is correct |
14 |
Correct |
462 ms |
24972 KB |
Output is correct |
15 |
Correct |
474 ms |
24972 KB |
Output is correct |
16 |
Correct |
98 ms |
24972 KB |
Output is correct |
17 |
Correct |
102 ms |
24972 KB |
Output is correct |
18 |
Correct |
99 ms |
24972 KB |
Output is correct |
19 |
Correct |
105 ms |
25080 KB |
Output is correct |
20 |
Correct |
99 ms |
25080 KB |
Output is correct |
21 |
Correct |
587 ms |
25080 KB |
Output is correct |
22 |
Correct |
612 ms |
25080 KB |
Output is correct |
23 |
Correct |
607 ms |
25080 KB |
Output is correct |
24 |
Correct |
624 ms |
25080 KB |
Output is correct |
25 |
Correct |
588 ms |
25080 KB |
Output is correct |
26 |
Correct |
674 ms |
25080 KB |
Output is correct |
27 |
Correct |
712 ms |
25080 KB |
Output is correct |
28 |
Correct |
673 ms |
25088 KB |
Output is correct |
29 |
Correct |
674 ms |
25096 KB |
Output is correct |
30 |
Correct |
439 ms |
25104 KB |
Output is correct |
31 |
Correct |
452 ms |
25104 KB |
Output is correct |
32 |
Correct |
450 ms |
25180 KB |
Output is correct |
33 |
Correct |
450 ms |
25184 KB |
Output is correct |
34 |
Correct |
458 ms |
25184 KB |
Output is correct |
35 |
Correct |
452 ms |
25208 KB |
Output is correct |
36 |
Correct |
442 ms |
25348 KB |
Output is correct |
37 |
Correct |
456 ms |
25348 KB |
Output is correct |
38 |
Correct |
445 ms |
25348 KB |
Output is correct |
39 |
Correct |
26 ms |
25348 KB |
Output is correct |
40 |
Correct |
26 ms |
25348 KB |
Output is correct |
41 |
Correct |
26 ms |
25348 KB |
Output is correct |
42 |
Correct |
26 ms |
25348 KB |
Output is correct |
43 |
Correct |
26 ms |
25348 KB |
Output is correct |
44 |
Correct |
27 ms |
25348 KB |
Output is correct |
45 |
Correct |
24 ms |
25348 KB |
Output is correct |
46 |
Correct |
27 ms |
25348 KB |
Output is correct |
47 |
Correct |
24 ms |
25348 KB |
Output is correct |
48 |
Correct |
24 ms |
25348 KB |
Output is correct |
49 |
Correct |
27 ms |
25348 KB |
Output is correct |
50 |
Correct |
25 ms |
25348 KB |
Output is correct |
51 |
Correct |
26 ms |
25348 KB |
Output is correct |
52 |
Correct |
26 ms |
25348 KB |
Output is correct |
53 |
Correct |
26 ms |
25348 KB |
Output is correct |
54 |
Correct |
24 ms |
25348 KB |
Output is correct |
55 |
Correct |
24 ms |
25348 KB |
Output is correct |
56 |
Correct |
24 ms |
25348 KB |
Output is correct |
57 |
Correct |
23 ms |
25348 KB |
Output is correct |
58 |
Correct |
25 ms |
25348 KB |
Output is correct |
59 |
Correct |
690 ms |
25348 KB |
Output is correct |
60 |
Correct |
674 ms |
25348 KB |
Output is correct |
61 |
Correct |
648 ms |
25420 KB |
Output is correct |
62 |
Correct |
720 ms |
25420 KB |
Output is correct |
63 |
Correct |
703 ms |
25420 KB |
Output is correct |
64 |
Correct |
812 ms |
25420 KB |
Output is correct |
65 |
Correct |
825 ms |
25420 KB |
Output is correct |
66 |
Correct |
803 ms |
25420 KB |
Output is correct |
67 |
Correct |
790 ms |
25420 KB |
Output is correct |
68 |
Correct |
805 ms |
25420 KB |
Output is correct |
69 |
Correct |
611 ms |
25520 KB |
Output is correct |
70 |
Correct |
655 ms |
25520 KB |
Output is correct |
71 |
Correct |
618 ms |
25520 KB |
Output is correct |
72 |
Correct |
622 ms |
25520 KB |
Output is correct |
73 |
Correct |
661 ms |
25520 KB |
Output is correct |
74 |
Correct |
139 ms |
25520 KB |
Output is correct |
75 |
Correct |
140 ms |
25520 KB |
Output is correct |
76 |
Correct |
136 ms |
25520 KB |
Output is correct |
77 |
Correct |
154 ms |
25520 KB |
Output is correct |
78 |
Correct |
143 ms |
25520 KB |
Output is correct |