#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int power(int n, int x) {
if (x == 0) {
return 1;
}
int res = power(n, x / 2);
res = (1ll * res * res) % MOD;
if (x & 1) {
res = (1ll * res * n) % MOD;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
cin >> N;
vector<int> A(N + 1);
vector<int> B(N + 1);
vector<int> X;
vector<int> L;
vector<int> inv(N + 1); // inv[i] = inverse of integer i
for (int i = 1; i <= N; i++) {
cin >> A[i] >> B[i];
X.emplace_back(A[i]);
X.emplace_back(B[i] + 1);
inv[i] = power(i, MOD - 2); // Fermatt's Little Theorem for modular multiplicative inverse
}
sort(begin(X), end(X));
X.resize(unique(begin(X), end(X)) - begin(X));
for (int i = 1; i <= N; i++) {
A[i] = upper_bound(begin(X), end(X), A[i]) - begin(X);
B[i] = upper_bound(begin(X), end(X), B[i]) - begin(X);
}
L.resize(X.size());
for (int i = 1; i < X.size(); i++) {
L[i] = X[i] - X[i - 1];
}
vector<vector<int>> DP(N + 1, vector<int>(X.size() + 1, 0));
vector<vector<int>> Choose(X.size() + 1, vector<int>(N + 1, 0));
for (int i = 1; i < X.size(); i++) {
Choose[i][0] = 1;
for (int j = 1; j <= N; j++) {
Choose[i][j] = (1ll * Choose[i][j - 1] * (L[i] + j - 1) % MOD) * inv[j] % MOD; // Choose j items from a total of L[i] items, each item can be chosen more than once
}
}
for (int i = 0; i < X.size(); i++) {
DP[0][i] = 1;
}
for (int i = 1; i <= N; i++) {
DP[i][0] = 1;
for (int j = 1; j < X.size(); j++) {
DP[i][j] = DP[i][j - 1];
for (int k = i, cnt = 0, choose = 0; k > 0; k--) {
if (A[k] <= j && j <= B[k]) {
// Choose[j][cnt] = Same item can be chosen more than once, but it is equal to ignoring duplicate items - thus it is strictly increasing
cnt++;
DP[i][j] = (1ll * DP[i][j] + (1ll * Choose[j][cnt] * DP[k - 1][j - 1]) % MOD) % MOD;
}
}
}
}
cout << (DP[N][X.size() - 1] - 1 + MOD) % MOD << "\n";
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:66:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j < X.size(); j++) {
~~^~~~~~~~~~
boat.cpp:68:38: warning: unused variable 'choose' [-Wunused-variable]
for (int k = i, cnt = 0, choose = 0; k > 0; k--) {
^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
443 ms |
4472 KB |
Output is correct |
2 |
Correct |
470 ms |
4472 KB |
Output is correct |
3 |
Correct |
454 ms |
4344 KB |
Output is correct |
4 |
Correct |
466 ms |
4344 KB |
Output is correct |
5 |
Correct |
457 ms |
4344 KB |
Output is correct |
6 |
Correct |
180 ms |
4472 KB |
Output is correct |
7 |
Correct |
173 ms |
4344 KB |
Output is correct |
8 |
Correct |
187 ms |
4344 KB |
Output is correct |
9 |
Correct |
185 ms |
4344 KB |
Output is correct |
10 |
Correct |
173 ms |
4344 KB |
Output is correct |
11 |
Correct |
179 ms |
4476 KB |
Output is correct |
12 |
Correct |
175 ms |
4344 KB |
Output is correct |
13 |
Correct |
179 ms |
4444 KB |
Output is correct |
14 |
Correct |
165 ms |
4344 KB |
Output is correct |
15 |
Correct |
184 ms |
4472 KB |
Output is correct |
16 |
Correct |
81 ms |
1144 KB |
Output is correct |
17 |
Correct |
90 ms |
1244 KB |
Output is correct |
18 |
Correct |
83 ms |
1148 KB |
Output is correct |
19 |
Correct |
80 ms |
1144 KB |
Output is correct |
20 |
Correct |
83 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
443 ms |
4472 KB |
Output is correct |
2 |
Correct |
470 ms |
4472 KB |
Output is correct |
3 |
Correct |
454 ms |
4344 KB |
Output is correct |
4 |
Correct |
466 ms |
4344 KB |
Output is correct |
5 |
Correct |
457 ms |
4344 KB |
Output is correct |
6 |
Correct |
180 ms |
4472 KB |
Output is correct |
7 |
Correct |
173 ms |
4344 KB |
Output is correct |
8 |
Correct |
187 ms |
4344 KB |
Output is correct |
9 |
Correct |
185 ms |
4344 KB |
Output is correct |
10 |
Correct |
173 ms |
4344 KB |
Output is correct |
11 |
Correct |
179 ms |
4476 KB |
Output is correct |
12 |
Correct |
175 ms |
4344 KB |
Output is correct |
13 |
Correct |
179 ms |
4444 KB |
Output is correct |
14 |
Correct |
165 ms |
4344 KB |
Output is correct |
15 |
Correct |
184 ms |
4472 KB |
Output is correct |
16 |
Correct |
81 ms |
1144 KB |
Output is correct |
17 |
Correct |
90 ms |
1244 KB |
Output is correct |
18 |
Correct |
83 ms |
1148 KB |
Output is correct |
19 |
Correct |
80 ms |
1144 KB |
Output is correct |
20 |
Correct |
83 ms |
1144 KB |
Output is correct |
21 |
Correct |
416 ms |
4188 KB |
Output is correct |
22 |
Correct |
457 ms |
4344 KB |
Output is correct |
23 |
Correct |
395 ms |
3960 KB |
Output is correct |
24 |
Correct |
430 ms |
4088 KB |
Output is correct |
25 |
Correct |
429 ms |
4088 KB |
Output is correct |
26 |
Correct |
521 ms |
3960 KB |
Output is correct |
27 |
Correct |
541 ms |
3964 KB |
Output is correct |
28 |
Correct |
530 ms |
3960 KB |
Output is correct |
29 |
Correct |
518 ms |
3960 KB |
Output is correct |
30 |
Correct |
192 ms |
4472 KB |
Output is correct |
31 |
Correct |
173 ms |
4344 KB |
Output is correct |
32 |
Correct |
183 ms |
4444 KB |
Output is correct |
33 |
Correct |
180 ms |
4348 KB |
Output is correct |
34 |
Correct |
180 ms |
4372 KB |
Output is correct |
35 |
Correct |
489 ms |
4472 KB |
Output is correct |
36 |
Correct |
470 ms |
4444 KB |
Output is correct |
37 |
Correct |
471 ms |
4344 KB |
Output is correct |
38 |
Correct |
472 ms |
4372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
548 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
504 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
7 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
508 KB |
Output is correct |
8 |
Correct |
7 ms |
504 KB |
Output is correct |
9 |
Correct |
7 ms |
504 KB |
Output is correct |
10 |
Correct |
7 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
7 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
443 ms |
4472 KB |
Output is correct |
2 |
Correct |
470 ms |
4472 KB |
Output is correct |
3 |
Correct |
454 ms |
4344 KB |
Output is correct |
4 |
Correct |
466 ms |
4344 KB |
Output is correct |
5 |
Correct |
457 ms |
4344 KB |
Output is correct |
6 |
Correct |
180 ms |
4472 KB |
Output is correct |
7 |
Correct |
173 ms |
4344 KB |
Output is correct |
8 |
Correct |
187 ms |
4344 KB |
Output is correct |
9 |
Correct |
185 ms |
4344 KB |
Output is correct |
10 |
Correct |
173 ms |
4344 KB |
Output is correct |
11 |
Correct |
179 ms |
4476 KB |
Output is correct |
12 |
Correct |
175 ms |
4344 KB |
Output is correct |
13 |
Correct |
179 ms |
4444 KB |
Output is correct |
14 |
Correct |
165 ms |
4344 KB |
Output is correct |
15 |
Correct |
184 ms |
4472 KB |
Output is correct |
16 |
Correct |
81 ms |
1144 KB |
Output is correct |
17 |
Correct |
90 ms |
1244 KB |
Output is correct |
18 |
Correct |
83 ms |
1148 KB |
Output is correct |
19 |
Correct |
80 ms |
1144 KB |
Output is correct |
20 |
Correct |
83 ms |
1144 KB |
Output is correct |
21 |
Correct |
416 ms |
4188 KB |
Output is correct |
22 |
Correct |
457 ms |
4344 KB |
Output is correct |
23 |
Correct |
395 ms |
3960 KB |
Output is correct |
24 |
Correct |
430 ms |
4088 KB |
Output is correct |
25 |
Correct |
429 ms |
4088 KB |
Output is correct |
26 |
Correct |
521 ms |
3960 KB |
Output is correct |
27 |
Correct |
541 ms |
3964 KB |
Output is correct |
28 |
Correct |
530 ms |
3960 KB |
Output is correct |
29 |
Correct |
518 ms |
3960 KB |
Output is correct |
30 |
Correct |
192 ms |
4472 KB |
Output is correct |
31 |
Correct |
173 ms |
4344 KB |
Output is correct |
32 |
Correct |
183 ms |
4444 KB |
Output is correct |
33 |
Correct |
180 ms |
4348 KB |
Output is correct |
34 |
Correct |
180 ms |
4372 KB |
Output is correct |
35 |
Correct |
489 ms |
4472 KB |
Output is correct |
36 |
Correct |
470 ms |
4444 KB |
Output is correct |
37 |
Correct |
471 ms |
4344 KB |
Output is correct |
38 |
Correct |
472 ms |
4372 KB |
Output is correct |
39 |
Correct |
6 ms |
548 KB |
Output is correct |
40 |
Correct |
6 ms |
504 KB |
Output is correct |
41 |
Correct |
6 ms |
504 KB |
Output is correct |
42 |
Correct |
6 ms |
504 KB |
Output is correct |
43 |
Correct |
6 ms |
504 KB |
Output is correct |
44 |
Correct |
7 ms |
504 KB |
Output is correct |
45 |
Correct |
7 ms |
508 KB |
Output is correct |
46 |
Correct |
7 ms |
504 KB |
Output is correct |
47 |
Correct |
7 ms |
504 KB |
Output is correct |
48 |
Correct |
7 ms |
504 KB |
Output is correct |
49 |
Correct |
7 ms |
504 KB |
Output is correct |
50 |
Correct |
6 ms |
504 KB |
Output is correct |
51 |
Correct |
6 ms |
504 KB |
Output is correct |
52 |
Correct |
7 ms |
504 KB |
Output is correct |
53 |
Correct |
6 ms |
504 KB |
Output is correct |
54 |
Correct |
5 ms |
380 KB |
Output is correct |
55 |
Correct |
5 ms |
504 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
474 ms |
4396 KB |
Output is correct |
60 |
Correct |
459 ms |
4388 KB |
Output is correct |
61 |
Correct |
440 ms |
4344 KB |
Output is correct |
62 |
Correct |
484 ms |
4384 KB |
Output is correct |
63 |
Correct |
476 ms |
4396 KB |
Output is correct |
64 |
Correct |
609 ms |
4388 KB |
Output is correct |
65 |
Correct |
625 ms |
4344 KB |
Output is correct |
66 |
Correct |
615 ms |
4404 KB |
Output is correct |
67 |
Correct |
598 ms |
4388 KB |
Output is correct |
68 |
Correct |
635 ms |
4344 KB |
Output is correct |
69 |
Correct |
470 ms |
4384 KB |
Output is correct |
70 |
Correct |
483 ms |
4344 KB |
Output is correct |
71 |
Correct |
492 ms |
4388 KB |
Output is correct |
72 |
Correct |
503 ms |
4472 KB |
Output is correct |
73 |
Correct |
490 ms |
4344 KB |
Output is correct |
74 |
Correct |
107 ms |
1144 KB |
Output is correct |
75 |
Correct |
110 ms |
1148 KB |
Output is correct |
76 |
Correct |
108 ms |
1272 KB |
Output is correct |
77 |
Correct |
107 ms |
1160 KB |
Output is correct |
78 |
Correct |
105 ms |
1016 KB |
Output is correct |