#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int MN = 505, LOG = 18, MOD = 1e9 + 7;
int dp[MN][MN<<1],pre[MN<<1][MN][2],a[MN],b[MN];
int ch[MN][MN], ch2[MN<<1][MN][2], iv[MN<<1];
int inv (int base) {
int res = 1, exp = MOD - 2;
while (exp) {
if (exp&1) res = (ll)res * base % MOD;
base = (ll)base * base % MOD;
exp>>=1;
}
return res;
}
int choose (int n, int k) {
assert(n >= 0 && k >= 0);
int res = 1;
for (int i = n; i > n-k; i--) res = (ll)res * i % MOD;
for (int i = 2; i <= k; i++) res = (ll)res * iv[i] % MOD;
return res;
}
int calc (int j, int x, bool ree) {
int ret = ch2[j][x-ree][ree];
for (int zeros = 1; zeros <= x-2; zeros++) ret = (ret + (ll)ch2[j][x-zeros-ree][ree] * ch[x-2][zeros]) % MOD;
return ret;
}
int main () {
int n;
scanf ("%d",&n);
for (int i = 0; i <= n*2; i++) iv[i] = inv(i);
for (int i = 0; i <= n; i++) for (int j = 0; j <= i; j++) ch[i][j] = choose(i,j);
vector<int> xs = {-1};
for (int i = 1; i <= n; i++) {
scanf ("%d %d",&a[i],&b[i]);
xs.push_back(a[i]); xs.push_back(b[i]);
}
sort(xs.begin(),xs.end()); xs.erase(unique(xs.begin(),xs.end()),xs.end());
int m = xs.size() - 1;
for (int j = 1; j <= m; j++) {
ch2[j][0][0] = ch2[j][0][1] = 1;
for (int i = 1; i <= n; i++) {
ch2[j][i][0] = (ll)ch2[j][i-1][0] * (xs[j]-xs[j-1]-i+1) % MOD * inv(i) % MOD;
ch2[j][i][1] = (ll)ch2[j][i-1][1] * (xs[j]-xs[j-1]-i) % MOD * inv(i) % MOD;
}
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
pre[j][i][0] = calc(j,i,0);
pre[j][i][1] = calc(j,i,1);
}
}
for (int j = 0; j <= m; j++) dp[0][j] = 1;
for (int i = 1; i <= n; i++) {
//printf ("i %d\n",i);
for (int j = 0; j <= m; j++){
if (j) dp[i][j] = (dp[i][j] + dp[i][j-1]) % MOD;
if (xs[j] < a[i] || (j&&b[i] <= xs[j-1])) continue;
bool ree = a[i]==xs[j]; int cnt = 0;
for (int k = i; k >= 1; k--) { //everything before k was <= xs[j-1]
if (a[k] > xs[j] || b[k] <= xs[j-1] || (k!=i&&a[k]==xs[j])) continue;
++cnt;
dp[i][j] = (dp[i][j] + (ll)dp[k-1][j-1] * pre[j][cnt][ree]) % MOD;
}
}
for (int j = 0; j <= m; j++) {
dp[i][j] = (dp[i][j] + dp[i-1][j]) % MOD;
//printf ("dp[%d][%d] = %d\n",i,j,dp[i][j]);
}
}
printf ("%d\n",(dp[n][m] + MOD - 1) % MOD);
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d",&n);
~~~~~~^~~~~~~~~
boat.cpp:36:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&a[i],&b[i]);
~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
938 ms |
7416 KB |
Output is correct |
2 |
Correct |
948 ms |
7476 KB |
Output is correct |
3 |
Correct |
937 ms |
7416 KB |
Output is correct |
4 |
Correct |
940 ms |
7544 KB |
Output is correct |
5 |
Correct |
946 ms |
7324 KB |
Output is correct |
6 |
Correct |
937 ms |
7468 KB |
Output is correct |
7 |
Correct |
939 ms |
7216 KB |
Output is correct |
8 |
Correct |
937 ms |
7288 KB |
Output is correct |
9 |
Correct |
937 ms |
7412 KB |
Output is correct |
10 |
Correct |
936 ms |
7260 KB |
Output is correct |
11 |
Correct |
933 ms |
7288 KB |
Output is correct |
12 |
Correct |
938 ms |
7288 KB |
Output is correct |
13 |
Correct |
938 ms |
7416 KB |
Output is correct |
14 |
Correct |
941 ms |
7288 KB |
Output is correct |
15 |
Correct |
941 ms |
7288 KB |
Output is correct |
16 |
Correct |
363 ms |
4040 KB |
Output is correct |
17 |
Correct |
373 ms |
4088 KB |
Output is correct |
18 |
Correct |
365 ms |
4088 KB |
Output is correct |
19 |
Correct |
371 ms |
4188 KB |
Output is correct |
20 |
Correct |
365 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
938 ms |
7416 KB |
Output is correct |
2 |
Correct |
948 ms |
7476 KB |
Output is correct |
3 |
Correct |
937 ms |
7416 KB |
Output is correct |
4 |
Correct |
940 ms |
7544 KB |
Output is correct |
5 |
Correct |
946 ms |
7324 KB |
Output is correct |
6 |
Correct |
937 ms |
7468 KB |
Output is correct |
7 |
Correct |
939 ms |
7216 KB |
Output is correct |
8 |
Correct |
937 ms |
7288 KB |
Output is correct |
9 |
Correct |
937 ms |
7412 KB |
Output is correct |
10 |
Correct |
936 ms |
7260 KB |
Output is correct |
11 |
Correct |
933 ms |
7288 KB |
Output is correct |
12 |
Correct |
938 ms |
7288 KB |
Output is correct |
13 |
Correct |
938 ms |
7416 KB |
Output is correct |
14 |
Correct |
941 ms |
7288 KB |
Output is correct |
15 |
Correct |
941 ms |
7288 KB |
Output is correct |
16 |
Correct |
363 ms |
4040 KB |
Output is correct |
17 |
Correct |
373 ms |
4088 KB |
Output is correct |
18 |
Correct |
365 ms |
4088 KB |
Output is correct |
19 |
Correct |
371 ms |
4188 KB |
Output is correct |
20 |
Correct |
365 ms |
4088 KB |
Output is correct |
21 |
Correct |
1672 ms |
10548 KB |
Output is correct |
22 |
Correct |
1665 ms |
10720 KB |
Output is correct |
23 |
Correct |
1638 ms |
10588 KB |
Output is correct |
24 |
Correct |
1653 ms |
10604 KB |
Output is correct |
25 |
Correct |
1644 ms |
10360 KB |
Output is correct |
26 |
Correct |
1781 ms |
10232 KB |
Output is correct |
27 |
Correct |
1807 ms |
10360 KB |
Output is correct |
28 |
Correct |
1784 ms |
10328 KB |
Output is correct |
29 |
Correct |
1785 ms |
10336 KB |
Output is correct |
30 |
Correct |
1584 ms |
11000 KB |
Output is correct |
31 |
Correct |
1574 ms |
10840 KB |
Output is correct |
32 |
Correct |
1581 ms |
10820 KB |
Output is correct |
33 |
Correct |
1611 ms |
11000 KB |
Output is correct |
34 |
Correct |
1558 ms |
10912 KB |
Output is correct |
35 |
Correct |
939 ms |
7388 KB |
Output is correct |
36 |
Correct |
939 ms |
7416 KB |
Output is correct |
37 |
Correct |
942 ms |
7416 KB |
Output is correct |
38 |
Correct |
941 ms |
7312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2552 KB |
Output is correct |
2 |
Correct |
21 ms |
2560 KB |
Output is correct |
3 |
Correct |
22 ms |
2552 KB |
Output is correct |
4 |
Correct |
25 ms |
2556 KB |
Output is correct |
5 |
Correct |
22 ms |
2560 KB |
Output is correct |
6 |
Correct |
24 ms |
2560 KB |
Output is correct |
7 |
Correct |
23 ms |
2552 KB |
Output is correct |
8 |
Correct |
23 ms |
2560 KB |
Output is correct |
9 |
Correct |
23 ms |
2560 KB |
Output is correct |
10 |
Correct |
23 ms |
2560 KB |
Output is correct |
11 |
Correct |
22 ms |
2560 KB |
Output is correct |
12 |
Correct |
21 ms |
2560 KB |
Output is correct |
13 |
Correct |
22 ms |
2536 KB |
Output is correct |
14 |
Correct |
21 ms |
2560 KB |
Output is correct |
15 |
Correct |
21 ms |
2552 KB |
Output is correct |
16 |
Correct |
11 ms |
1664 KB |
Output is correct |
17 |
Correct |
11 ms |
1664 KB |
Output is correct |
18 |
Correct |
11 ms |
1664 KB |
Output is correct |
19 |
Correct |
11 ms |
1664 KB |
Output is correct |
20 |
Correct |
11 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
938 ms |
7416 KB |
Output is correct |
2 |
Correct |
948 ms |
7476 KB |
Output is correct |
3 |
Correct |
937 ms |
7416 KB |
Output is correct |
4 |
Correct |
940 ms |
7544 KB |
Output is correct |
5 |
Correct |
946 ms |
7324 KB |
Output is correct |
6 |
Correct |
937 ms |
7468 KB |
Output is correct |
7 |
Correct |
939 ms |
7216 KB |
Output is correct |
8 |
Correct |
937 ms |
7288 KB |
Output is correct |
9 |
Correct |
937 ms |
7412 KB |
Output is correct |
10 |
Correct |
936 ms |
7260 KB |
Output is correct |
11 |
Correct |
933 ms |
7288 KB |
Output is correct |
12 |
Correct |
938 ms |
7288 KB |
Output is correct |
13 |
Correct |
938 ms |
7416 KB |
Output is correct |
14 |
Correct |
941 ms |
7288 KB |
Output is correct |
15 |
Correct |
941 ms |
7288 KB |
Output is correct |
16 |
Correct |
363 ms |
4040 KB |
Output is correct |
17 |
Correct |
373 ms |
4088 KB |
Output is correct |
18 |
Correct |
365 ms |
4088 KB |
Output is correct |
19 |
Correct |
371 ms |
4188 KB |
Output is correct |
20 |
Correct |
365 ms |
4088 KB |
Output is correct |
21 |
Correct |
1672 ms |
10548 KB |
Output is correct |
22 |
Correct |
1665 ms |
10720 KB |
Output is correct |
23 |
Correct |
1638 ms |
10588 KB |
Output is correct |
24 |
Correct |
1653 ms |
10604 KB |
Output is correct |
25 |
Correct |
1644 ms |
10360 KB |
Output is correct |
26 |
Correct |
1781 ms |
10232 KB |
Output is correct |
27 |
Correct |
1807 ms |
10360 KB |
Output is correct |
28 |
Correct |
1784 ms |
10328 KB |
Output is correct |
29 |
Correct |
1785 ms |
10336 KB |
Output is correct |
30 |
Correct |
1584 ms |
11000 KB |
Output is correct |
31 |
Correct |
1574 ms |
10840 KB |
Output is correct |
32 |
Correct |
1581 ms |
10820 KB |
Output is correct |
33 |
Correct |
1611 ms |
11000 KB |
Output is correct |
34 |
Correct |
1558 ms |
10912 KB |
Output is correct |
35 |
Correct |
939 ms |
7388 KB |
Output is correct |
36 |
Correct |
939 ms |
7416 KB |
Output is correct |
37 |
Correct |
942 ms |
7416 KB |
Output is correct |
38 |
Correct |
941 ms |
7312 KB |
Output is correct |
39 |
Correct |
23 ms |
2552 KB |
Output is correct |
40 |
Correct |
21 ms |
2560 KB |
Output is correct |
41 |
Correct |
22 ms |
2552 KB |
Output is correct |
42 |
Correct |
25 ms |
2556 KB |
Output is correct |
43 |
Correct |
22 ms |
2560 KB |
Output is correct |
44 |
Correct |
24 ms |
2560 KB |
Output is correct |
45 |
Correct |
23 ms |
2552 KB |
Output is correct |
46 |
Correct |
23 ms |
2560 KB |
Output is correct |
47 |
Correct |
23 ms |
2560 KB |
Output is correct |
48 |
Correct |
23 ms |
2560 KB |
Output is correct |
49 |
Correct |
22 ms |
2560 KB |
Output is correct |
50 |
Correct |
21 ms |
2560 KB |
Output is correct |
51 |
Correct |
22 ms |
2536 KB |
Output is correct |
52 |
Correct |
21 ms |
2560 KB |
Output is correct |
53 |
Correct |
21 ms |
2552 KB |
Output is correct |
54 |
Correct |
11 ms |
1664 KB |
Output is correct |
55 |
Correct |
11 ms |
1664 KB |
Output is correct |
56 |
Correct |
11 ms |
1664 KB |
Output is correct |
57 |
Correct |
11 ms |
1664 KB |
Output is correct |
58 |
Correct |
11 ms |
1664 KB |
Output is correct |
59 |
Correct |
1811 ms |
11512 KB |
Output is correct |
60 |
Correct |
1787 ms |
11276 KB |
Output is correct |
61 |
Correct |
1777 ms |
11384 KB |
Output is correct |
62 |
Correct |
1803 ms |
11376 KB |
Output is correct |
63 |
Correct |
1804 ms |
11368 KB |
Output is correct |
64 |
Correct |
1991 ms |
11384 KB |
Output is correct |
65 |
Correct |
1988 ms |
11556 KB |
Output is correct |
66 |
Correct |
1989 ms |
11216 KB |
Output is correct |
67 |
Correct |
1992 ms |
11364 KB |
Output is correct |
68 |
Correct |
1985 ms |
11316 KB |
Output is correct |
69 |
Correct |
1768 ms |
11408 KB |
Output is correct |
70 |
Correct |
1767 ms |
11384 KB |
Output is correct |
71 |
Correct |
1764 ms |
11516 KB |
Output is correct |
72 |
Correct |
1776 ms |
11384 KB |
Output is correct |
73 |
Correct |
1781 ms |
11640 KB |
Output is correct |
74 |
Correct |
403 ms |
4088 KB |
Output is correct |
75 |
Correct |
398 ms |
4344 KB |
Output is correct |
76 |
Correct |
401 ms |
4088 KB |
Output is correct |
77 |
Correct |
399 ms |
4216 KB |
Output is correct |
78 |
Correct |
403 ms |
4088 KB |
Output is correct |