#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long N, A[509], B[509], nr1[1009][1009], nr2[1009][1009], dp[1009][1009], inv[1009], mod = 1000000007; vector<long long>V;
long long rets[1009][1009];
long long modpow(long long a, long long b, long long m) {
long long p = 1, q = a;
for (int i = 0; i < 60; i++) {
if ((b / (1LL << i)) % 2 == 1) { p *= q; p %= m; }
q *= q; q %= m;
}
return p;
}
long long Div(long long a, long long b, long long m) {
return (a*modpow(b, m - 2, m)) % m;
}
void init() {
for (int i = 0; i <= 500; i++) {
for (int j = 0; j <= 500; j++) {
if (i == 0 || j == 0) nr1[i][j] = 1;
else nr1[i][j] = (nr1[i - 1][j] + nr1[i][j - 1]) % mod;
}
}
for (int i = 1; i <= 1000; i++) inv[i] = Div(1, i, mod);
for (int i = 0; i < V.size() - 1; i++) {
long long L = V[i + 1] - V[i], S = 1;
for (int j = 0; j <= 500; j++) {
if (L < j) break;
nr2[i][j] = S;
S *= (L - j); S %= mod;
S *= inv[j + 1]; S %= mod;
}
}
}
long long ncr(long long n, long long r) {
if (r < 0 || n < r) return 0;
return nr1[n - r][r];
}
long long solve(long long v, long long pos) {
if (rets[v][pos] >= 1) return rets[v][pos] - 1;
long long res = 0;
for (int i = 1; i <= v; i++) {
res += ncr(v - 1, i - 1)*nr2[pos][i];
if (res >= (7LL << 60)) res %= mod;
}
res %= mod;
rets[v][pos] = res + 1;
return res;
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i] >> B[i]; B[i]++;
V.push_back(A[i]);
V.push_back(B[i]);
}
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
for (int i = 1; i <= N; i++) {
A[i] = lower_bound(V.begin(), V.end(), A[i]) - V.begin();
B[i] = lower_bound(V.begin(), V.end(), B[i]) - V.begin();
}
init();
dp[0][1] = 1;
for (int i = 0; i < V.size() - 1; i++) {
for (int j = 1; j <= N + 1; j++) {
if (dp[i][j] == 0) continue;
dp[i + 1][j] += dp[i][j]; dp[i + 1][j] %= mod;
int G = 0;
for (int k = j + 1; k <= N + 1; k++) {
if (A[k - 1] <= i && i < B[k - 1]) {
G++;
dp[i + 1][k] += dp[i][j] * solve(G, i);
dp[i + 1][k] %= mod;
}
}
}
}
long long sum = 0;
for (int i = 2; i <= N + 1; i++) sum += dp[V.size() - 1][i];
cout << sum%mod << endl;
return 0;
}
Compilation message
boat.cpp: In function 'void init()':
boat.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < V.size() - 1; i++) {
~~^~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:70:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < V.size() - 1; i++) {
~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
18296 KB |
Output is correct |
2 |
Correct |
278 ms |
18296 KB |
Output is correct |
3 |
Correct |
264 ms |
18296 KB |
Output is correct |
4 |
Correct |
265 ms |
18348 KB |
Output is correct |
5 |
Correct |
278 ms |
18368 KB |
Output is correct |
6 |
Correct |
162 ms |
18368 KB |
Output is correct |
7 |
Correct |
183 ms |
18368 KB |
Output is correct |
8 |
Correct |
169 ms |
18368 KB |
Output is correct |
9 |
Correct |
170 ms |
18368 KB |
Output is correct |
10 |
Correct |
170 ms |
18368 KB |
Output is correct |
11 |
Correct |
167 ms |
18368 KB |
Output is correct |
12 |
Correct |
171 ms |
18424 KB |
Output is correct |
13 |
Correct |
191 ms |
18424 KB |
Output is correct |
14 |
Correct |
168 ms |
18424 KB |
Output is correct |
15 |
Correct |
172 ms |
18424 KB |
Output is correct |
16 |
Correct |
59 ms |
18424 KB |
Output is correct |
17 |
Correct |
62 ms |
18424 KB |
Output is correct |
18 |
Correct |
59 ms |
18424 KB |
Output is correct |
19 |
Correct |
60 ms |
18424 KB |
Output is correct |
20 |
Correct |
58 ms |
18424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
18296 KB |
Output is correct |
2 |
Correct |
278 ms |
18296 KB |
Output is correct |
3 |
Correct |
264 ms |
18296 KB |
Output is correct |
4 |
Correct |
265 ms |
18348 KB |
Output is correct |
5 |
Correct |
278 ms |
18368 KB |
Output is correct |
6 |
Correct |
162 ms |
18368 KB |
Output is correct |
7 |
Correct |
183 ms |
18368 KB |
Output is correct |
8 |
Correct |
169 ms |
18368 KB |
Output is correct |
9 |
Correct |
170 ms |
18368 KB |
Output is correct |
10 |
Correct |
170 ms |
18368 KB |
Output is correct |
11 |
Correct |
167 ms |
18368 KB |
Output is correct |
12 |
Correct |
171 ms |
18424 KB |
Output is correct |
13 |
Correct |
191 ms |
18424 KB |
Output is correct |
14 |
Correct |
168 ms |
18424 KB |
Output is correct |
15 |
Correct |
172 ms |
18424 KB |
Output is correct |
16 |
Correct |
59 ms |
18424 KB |
Output is correct |
17 |
Correct |
62 ms |
18424 KB |
Output is correct |
18 |
Correct |
59 ms |
18424 KB |
Output is correct |
19 |
Correct |
60 ms |
18424 KB |
Output is correct |
20 |
Correct |
58 ms |
18424 KB |
Output is correct |
21 |
Correct |
640 ms |
18424 KB |
Output is correct |
22 |
Correct |
604 ms |
18424 KB |
Output is correct |
23 |
Correct |
565 ms |
18424 KB |
Output is correct |
24 |
Correct |
598 ms |
18424 KB |
Output is correct |
25 |
Correct |
605 ms |
18424 KB |
Output is correct |
26 |
Correct |
1158 ms |
18424 KB |
Output is correct |
27 |
Correct |
1131 ms |
18580 KB |
Output is correct |
28 |
Correct |
1124 ms |
18580 KB |
Output is correct |
29 |
Correct |
1133 ms |
18580 KB |
Output is correct |
30 |
Correct |
173 ms |
18580 KB |
Output is correct |
31 |
Correct |
173 ms |
18580 KB |
Output is correct |
32 |
Correct |
170 ms |
18580 KB |
Output is correct |
33 |
Correct |
168 ms |
18580 KB |
Output is correct |
34 |
Correct |
171 ms |
18580 KB |
Output is correct |
35 |
Correct |
245 ms |
18580 KB |
Output is correct |
36 |
Correct |
251 ms |
18580 KB |
Output is correct |
37 |
Correct |
270 ms |
18580 KB |
Output is correct |
38 |
Correct |
258 ms |
18580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
18580 KB |
Output is correct |
2 |
Correct |
24 ms |
18580 KB |
Output is correct |
3 |
Correct |
23 ms |
18580 KB |
Output is correct |
4 |
Correct |
26 ms |
18580 KB |
Output is correct |
5 |
Correct |
25 ms |
18580 KB |
Output is correct |
6 |
Correct |
28 ms |
18580 KB |
Output is correct |
7 |
Correct |
28 ms |
18580 KB |
Output is correct |
8 |
Correct |
28 ms |
18580 KB |
Output is correct |
9 |
Correct |
28 ms |
18580 KB |
Output is correct |
10 |
Correct |
28 ms |
18580 KB |
Output is correct |
11 |
Correct |
24 ms |
18580 KB |
Output is correct |
12 |
Correct |
23 ms |
18580 KB |
Output is correct |
13 |
Correct |
24 ms |
18580 KB |
Output is correct |
14 |
Correct |
25 ms |
18580 KB |
Output is correct |
15 |
Correct |
24 ms |
18580 KB |
Output is correct |
16 |
Correct |
18 ms |
18580 KB |
Output is correct |
17 |
Correct |
18 ms |
18580 KB |
Output is correct |
18 |
Correct |
18 ms |
18580 KB |
Output is correct |
19 |
Correct |
18 ms |
18580 KB |
Output is correct |
20 |
Correct |
19 ms |
18580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
18296 KB |
Output is correct |
2 |
Correct |
278 ms |
18296 KB |
Output is correct |
3 |
Correct |
264 ms |
18296 KB |
Output is correct |
4 |
Correct |
265 ms |
18348 KB |
Output is correct |
5 |
Correct |
278 ms |
18368 KB |
Output is correct |
6 |
Correct |
162 ms |
18368 KB |
Output is correct |
7 |
Correct |
183 ms |
18368 KB |
Output is correct |
8 |
Correct |
169 ms |
18368 KB |
Output is correct |
9 |
Correct |
170 ms |
18368 KB |
Output is correct |
10 |
Correct |
170 ms |
18368 KB |
Output is correct |
11 |
Correct |
167 ms |
18368 KB |
Output is correct |
12 |
Correct |
171 ms |
18424 KB |
Output is correct |
13 |
Correct |
191 ms |
18424 KB |
Output is correct |
14 |
Correct |
168 ms |
18424 KB |
Output is correct |
15 |
Correct |
172 ms |
18424 KB |
Output is correct |
16 |
Correct |
59 ms |
18424 KB |
Output is correct |
17 |
Correct |
62 ms |
18424 KB |
Output is correct |
18 |
Correct |
59 ms |
18424 KB |
Output is correct |
19 |
Correct |
60 ms |
18424 KB |
Output is correct |
20 |
Correct |
58 ms |
18424 KB |
Output is correct |
21 |
Correct |
640 ms |
18424 KB |
Output is correct |
22 |
Correct |
604 ms |
18424 KB |
Output is correct |
23 |
Correct |
565 ms |
18424 KB |
Output is correct |
24 |
Correct |
598 ms |
18424 KB |
Output is correct |
25 |
Correct |
605 ms |
18424 KB |
Output is correct |
26 |
Correct |
1158 ms |
18424 KB |
Output is correct |
27 |
Correct |
1131 ms |
18580 KB |
Output is correct |
28 |
Correct |
1124 ms |
18580 KB |
Output is correct |
29 |
Correct |
1133 ms |
18580 KB |
Output is correct |
30 |
Correct |
173 ms |
18580 KB |
Output is correct |
31 |
Correct |
173 ms |
18580 KB |
Output is correct |
32 |
Correct |
170 ms |
18580 KB |
Output is correct |
33 |
Correct |
168 ms |
18580 KB |
Output is correct |
34 |
Correct |
171 ms |
18580 KB |
Output is correct |
35 |
Correct |
245 ms |
18580 KB |
Output is correct |
36 |
Correct |
251 ms |
18580 KB |
Output is correct |
37 |
Correct |
270 ms |
18580 KB |
Output is correct |
38 |
Correct |
258 ms |
18580 KB |
Output is correct |
39 |
Correct |
23 ms |
18580 KB |
Output is correct |
40 |
Correct |
24 ms |
18580 KB |
Output is correct |
41 |
Correct |
23 ms |
18580 KB |
Output is correct |
42 |
Correct |
26 ms |
18580 KB |
Output is correct |
43 |
Correct |
25 ms |
18580 KB |
Output is correct |
44 |
Correct |
28 ms |
18580 KB |
Output is correct |
45 |
Correct |
28 ms |
18580 KB |
Output is correct |
46 |
Correct |
28 ms |
18580 KB |
Output is correct |
47 |
Correct |
28 ms |
18580 KB |
Output is correct |
48 |
Correct |
28 ms |
18580 KB |
Output is correct |
49 |
Correct |
24 ms |
18580 KB |
Output is correct |
50 |
Correct |
23 ms |
18580 KB |
Output is correct |
51 |
Correct |
24 ms |
18580 KB |
Output is correct |
52 |
Correct |
25 ms |
18580 KB |
Output is correct |
53 |
Correct |
24 ms |
18580 KB |
Output is correct |
54 |
Correct |
18 ms |
18580 KB |
Output is correct |
55 |
Correct |
18 ms |
18580 KB |
Output is correct |
56 |
Correct |
18 ms |
18580 KB |
Output is correct |
57 |
Correct |
18 ms |
18580 KB |
Output is correct |
58 |
Correct |
19 ms |
18580 KB |
Output is correct |
59 |
Correct |
722 ms |
22432 KB |
Output is correct |
60 |
Correct |
698 ms |
22432 KB |
Output is correct |
61 |
Correct |
665 ms |
22432 KB |
Output is correct |
62 |
Correct |
761 ms |
22500 KB |
Output is correct |
63 |
Correct |
720 ms |
22524 KB |
Output is correct |
64 |
Correct |
1385 ms |
23604 KB |
Output is correct |
65 |
Correct |
1391 ms |
23604 KB |
Output is correct |
66 |
Correct |
1379 ms |
23680 KB |
Output is correct |
67 |
Correct |
1380 ms |
23680 KB |
Output is correct |
68 |
Correct |
1353 ms |
23724 KB |
Output is correct |
69 |
Correct |
781 ms |
23724 KB |
Output is correct |
70 |
Correct |
788 ms |
23724 KB |
Output is correct |
71 |
Correct |
741 ms |
23724 KB |
Output is correct |
72 |
Correct |
774 ms |
23724 KB |
Output is correct |
73 |
Correct |
769 ms |
23724 KB |
Output is correct |
74 |
Correct |
139 ms |
23724 KB |
Output is correct |
75 |
Correct |
136 ms |
23724 KB |
Output is correct |
76 |
Correct |
146 ms |
23724 KB |
Output is correct |
77 |
Correct |
141 ms |
23724 KB |
Output is correct |
78 |
Correct |
146 ms |
23724 KB |
Output is correct |