#include <cstdio>
#include <algorithm>
#include <vector>
//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout
#define MOD 1000000007
inline void precalcCombinari(std::vector < std::vector < int > > &comb) {
for (int i = 0; i < (int)comb.size(); i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++)
comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
}
}
inline bool getValues(int n, int a, int b, std::vector < int > &A, std::vector < int > &B, std::vector < bool > &avem) {
bool ans = 0;
for (int i = 1; i <= n; i++) {
avem[i] = A[i] <= a && B[i] >= b - 1;
ans |= avem[i];
}
return ans;
}
inline int lgput(int a, int n) {
int r = 1;
while (n) {
if (n % 2)
r = 1LL * r * a % MOD;
a = 1LL * a * a % MOD;
n /= 2;
}
return r;
}
inline void calcCombinari(std::vector < int > &comb, int n) {
comb[0] = 1;
for (int i = 1; i < (int)comb.size(); i++)
comb[i] = 1LL * comb[i - 1] * (n - i + 1) % MOD * lgput(i, MOD - 2) % MOD;
}
inline void precalc(std::vector < int > &r, int tmax, int l, std::vector < std::vector < int > > &comb) {
std::vector < int > combL(std::min(tmax + 1, l + 1));
calcCombinari(combL, l);
for (int i = 0; i <= tmax; i++) {
r[i] = 0;
for (int j = 1; j <= i && j <= l; j++)
r[i] = (r[i] + 1LL * comb[i - 1][j - 1] * combL[j]) % MOD;
}
}
inline void solve(std::vector < std::vector < int > > &dp, int n, int l, int &w, int &q, std::vector < bool > &avem, std::vector < std::vector < int > > &comb) {
w = 1 - w;
q = 1 - q;
int tmax = 0;
for (int i = 1; i <= n; i++)
tmax += avem[i];
std::vector < int > r(tmax + 1);
precalc(r, tmax, l, comb);
for (int i = 0; i <= n; i++)
dp[w][i] = 0;
for (int i = 0; i <= n; i++) {
dp[w][i] = (dp[w][i] + dp[q][i]) % MOD;
int t = 0;
for (int j = i + 1; j <= n; j++) {
if (avem[j]) {
t++;
dp[w][j] = (dp[w][j] + 1LL * dp[q][i] * r[t]) % MOD;
}
}
}
}
int main() {
int n;
fscanf(fin, "%d", &n);
std::vector < std::vector < int > > comb(n, std::vector < int > (n, 0));
precalcCombinari(comb);
std::vector < int > poz, A(n + 1), B(n + 1);
for (int i = 1; i <= n; i++) {
fscanf(fin, "%d%d", &A[i], &B[i]);
poz.push_back(A[i]);
poz.push_back(B[i] + 1);
}
std::sort(poz.begin(), poz.end());
poz.resize(std::distance(poz.begin(), std::unique(poz.begin(), poz.end())));
int w = 0, q = 1;
std::vector < std::vector < int > > dp(2, std::vector < int > (n + 1, 0));
dp[0][0] = 1;
std::vector < bool > avem(n + 1);
for (int i = 0; i < (int)poz.size() - 1; i++)
if (getValues(n, poz[i], poz[i + 1], A, B, avem))
solve(dp, n, poz[i + 1] - poz[i], w, q, avem, comb);
int ans = 0;
for (int i = 1; i <= n; i++)
ans = (ans + dp[w][i]) % MOD;
fprintf(fout, "%d\n", ans);
fclose(fin);
fclose(fout);
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:84:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d", &n);
~~~~~~^~~~~~~~~~~~~~~
boat.cpp:91:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d", &A[i], &B[i]);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
1280 KB |
Output is correct |
2 |
Correct |
87 ms |
1400 KB |
Output is correct |
3 |
Correct |
91 ms |
1532 KB |
Output is correct |
4 |
Correct |
81 ms |
1400 KB |
Output is correct |
5 |
Correct |
96 ms |
1380 KB |
Output is correct |
6 |
Correct |
89 ms |
1372 KB |
Output is correct |
7 |
Correct |
90 ms |
1404 KB |
Output is correct |
8 |
Correct |
122 ms |
1292 KB |
Output is correct |
9 |
Correct |
95 ms |
1280 KB |
Output is correct |
10 |
Correct |
109 ms |
1408 KB |
Output is correct |
11 |
Correct |
94 ms |
1372 KB |
Output is correct |
12 |
Correct |
84 ms |
1500 KB |
Output is correct |
13 |
Correct |
91 ms |
1408 KB |
Output is correct |
14 |
Correct |
101 ms |
1364 KB |
Output is correct |
15 |
Correct |
88 ms |
1280 KB |
Output is correct |
16 |
Correct |
26 ms |
1280 KB |
Output is correct |
17 |
Correct |
20 ms |
1280 KB |
Output is correct |
18 |
Correct |
21 ms |
1280 KB |
Output is correct |
19 |
Correct |
18 ms |
1280 KB |
Output is correct |
20 |
Correct |
23 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
1280 KB |
Output is correct |
2 |
Correct |
87 ms |
1400 KB |
Output is correct |
3 |
Correct |
91 ms |
1532 KB |
Output is correct |
4 |
Correct |
81 ms |
1400 KB |
Output is correct |
5 |
Correct |
96 ms |
1380 KB |
Output is correct |
6 |
Correct |
89 ms |
1372 KB |
Output is correct |
7 |
Correct |
90 ms |
1404 KB |
Output is correct |
8 |
Correct |
122 ms |
1292 KB |
Output is correct |
9 |
Correct |
95 ms |
1280 KB |
Output is correct |
10 |
Correct |
109 ms |
1408 KB |
Output is correct |
11 |
Correct |
94 ms |
1372 KB |
Output is correct |
12 |
Correct |
84 ms |
1500 KB |
Output is correct |
13 |
Correct |
91 ms |
1408 KB |
Output is correct |
14 |
Correct |
101 ms |
1364 KB |
Output is correct |
15 |
Correct |
88 ms |
1280 KB |
Output is correct |
16 |
Correct |
26 ms |
1280 KB |
Output is correct |
17 |
Correct |
20 ms |
1280 KB |
Output is correct |
18 |
Correct |
21 ms |
1280 KB |
Output is correct |
19 |
Correct |
18 ms |
1280 KB |
Output is correct |
20 |
Correct |
23 ms |
1408 KB |
Output is correct |
21 |
Correct |
292 ms |
1280 KB |
Output is correct |
22 |
Correct |
272 ms |
1400 KB |
Output is correct |
23 |
Correct |
248 ms |
1528 KB |
Output is correct |
24 |
Correct |
266 ms |
1380 KB |
Output is correct |
25 |
Correct |
260 ms |
1452 KB |
Output is correct |
26 |
Correct |
261 ms |
1400 KB |
Output is correct |
27 |
Correct |
317 ms |
1280 KB |
Output is correct |
28 |
Correct |
304 ms |
1280 KB |
Output is correct |
29 |
Correct |
241 ms |
1404 KB |
Output is correct |
30 |
Correct |
171 ms |
1408 KB |
Output is correct |
31 |
Correct |
153 ms |
1272 KB |
Output is correct |
32 |
Correct |
183 ms |
1368 KB |
Output is correct |
33 |
Correct |
224 ms |
1400 KB |
Output is correct |
34 |
Correct |
170 ms |
1280 KB |
Output is correct |
35 |
Correct |
194 ms |
1372 KB |
Output is correct |
36 |
Correct |
185 ms |
1372 KB |
Output is correct |
37 |
Correct |
171 ms |
1280 KB |
Output is correct |
38 |
Correct |
165 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
356 KB |
Output is correct |
2 |
Correct |
8 ms |
384 KB |
Output is correct |
3 |
Correct |
13 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
8 ms |
384 KB |
Output is correct |
6 |
Correct |
9 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
384 KB |
Output is correct |
9 |
Correct |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
8 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
384 KB |
Output is correct |
14 |
Correct |
7 ms |
384 KB |
Output is correct |
15 |
Correct |
8 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
1280 KB |
Output is correct |
2 |
Correct |
87 ms |
1400 KB |
Output is correct |
3 |
Correct |
91 ms |
1532 KB |
Output is correct |
4 |
Correct |
81 ms |
1400 KB |
Output is correct |
5 |
Correct |
96 ms |
1380 KB |
Output is correct |
6 |
Correct |
89 ms |
1372 KB |
Output is correct |
7 |
Correct |
90 ms |
1404 KB |
Output is correct |
8 |
Correct |
122 ms |
1292 KB |
Output is correct |
9 |
Correct |
95 ms |
1280 KB |
Output is correct |
10 |
Correct |
109 ms |
1408 KB |
Output is correct |
11 |
Correct |
94 ms |
1372 KB |
Output is correct |
12 |
Correct |
84 ms |
1500 KB |
Output is correct |
13 |
Correct |
91 ms |
1408 KB |
Output is correct |
14 |
Correct |
101 ms |
1364 KB |
Output is correct |
15 |
Correct |
88 ms |
1280 KB |
Output is correct |
16 |
Correct |
26 ms |
1280 KB |
Output is correct |
17 |
Correct |
20 ms |
1280 KB |
Output is correct |
18 |
Correct |
21 ms |
1280 KB |
Output is correct |
19 |
Correct |
18 ms |
1280 KB |
Output is correct |
20 |
Correct |
23 ms |
1408 KB |
Output is correct |
21 |
Correct |
292 ms |
1280 KB |
Output is correct |
22 |
Correct |
272 ms |
1400 KB |
Output is correct |
23 |
Correct |
248 ms |
1528 KB |
Output is correct |
24 |
Correct |
266 ms |
1380 KB |
Output is correct |
25 |
Correct |
260 ms |
1452 KB |
Output is correct |
26 |
Correct |
261 ms |
1400 KB |
Output is correct |
27 |
Correct |
317 ms |
1280 KB |
Output is correct |
28 |
Correct |
304 ms |
1280 KB |
Output is correct |
29 |
Correct |
241 ms |
1404 KB |
Output is correct |
30 |
Correct |
171 ms |
1408 KB |
Output is correct |
31 |
Correct |
153 ms |
1272 KB |
Output is correct |
32 |
Correct |
183 ms |
1368 KB |
Output is correct |
33 |
Correct |
224 ms |
1400 KB |
Output is correct |
34 |
Correct |
170 ms |
1280 KB |
Output is correct |
35 |
Correct |
194 ms |
1372 KB |
Output is correct |
36 |
Correct |
185 ms |
1372 KB |
Output is correct |
37 |
Correct |
171 ms |
1280 KB |
Output is correct |
38 |
Correct |
165 ms |
1500 KB |
Output is correct |
39 |
Correct |
8 ms |
356 KB |
Output is correct |
40 |
Correct |
8 ms |
384 KB |
Output is correct |
41 |
Correct |
13 ms |
384 KB |
Output is correct |
42 |
Correct |
8 ms |
384 KB |
Output is correct |
43 |
Correct |
8 ms |
384 KB |
Output is correct |
44 |
Correct |
9 ms |
384 KB |
Output is correct |
45 |
Correct |
10 ms |
384 KB |
Output is correct |
46 |
Correct |
9 ms |
384 KB |
Output is correct |
47 |
Correct |
9 ms |
384 KB |
Output is correct |
48 |
Correct |
9 ms |
384 KB |
Output is correct |
49 |
Correct |
6 ms |
512 KB |
Output is correct |
50 |
Correct |
8 ms |
384 KB |
Output is correct |
51 |
Correct |
8 ms |
384 KB |
Output is correct |
52 |
Correct |
7 ms |
384 KB |
Output is correct |
53 |
Correct |
8 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
512 KB |
Output is correct |
56 |
Correct |
4 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
5 ms |
384 KB |
Output is correct |
59 |
Correct |
403 ms |
1400 KB |
Output is correct |
60 |
Correct |
358 ms |
1500 KB |
Output is correct |
61 |
Correct |
412 ms |
1528 KB |
Output is correct |
62 |
Correct |
451 ms |
1408 KB |
Output is correct |
63 |
Correct |
396 ms |
1408 KB |
Output is correct |
64 |
Correct |
605 ms |
1408 KB |
Output is correct |
65 |
Correct |
555 ms |
1528 KB |
Output is correct |
66 |
Correct |
519 ms |
1280 KB |
Output is correct |
67 |
Correct |
524 ms |
1404 KB |
Output is correct |
68 |
Correct |
588 ms |
1500 KB |
Output is correct |
69 |
Correct |
360 ms |
1500 KB |
Output is correct |
70 |
Correct |
386 ms |
1400 KB |
Output is correct |
71 |
Correct |
376 ms |
1280 KB |
Output is correct |
72 |
Correct |
385 ms |
1368 KB |
Output is correct |
73 |
Correct |
396 ms |
1364 KB |
Output is correct |
74 |
Correct |
71 ms |
1280 KB |
Output is correct |
75 |
Correct |
63 ms |
1400 KB |
Output is correct |
76 |
Correct |
86 ms |
1404 KB |
Output is correct |
77 |
Correct |
63 ms |
1280 KB |
Output is correct |
78 |
Correct |
73 ms |
1408 KB |
Output is correct |