#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;
}
}
memset(pre,-1,sizeof pre);
for (int j = 0; j <= m; j++) dp[0][j] = 1;
for (int i = 1; i <= 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--) {
if (a[k] > xs[j] || b[k] <= xs[j-1] || (k!=i&&a[k]==xs[j])) continue;
++cnt;
if (!~pre[j][cnt][ree]) pre[j][cnt][ree] = calc(j,cnt,ree);
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 ("%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 |
335 ms |
9208 KB |
Output is correct |
2 |
Correct |
335 ms |
9464 KB |
Output is correct |
3 |
Correct |
336 ms |
9248 KB |
Output is correct |
4 |
Correct |
337 ms |
9464 KB |
Output is correct |
5 |
Correct |
337 ms |
9208 KB |
Output is correct |
6 |
Correct |
331 ms |
9312 KB |
Output is correct |
7 |
Correct |
335 ms |
9208 KB |
Output is correct |
8 |
Correct |
334 ms |
9336 KB |
Output is correct |
9 |
Correct |
335 ms |
9208 KB |
Output is correct |
10 |
Correct |
336 ms |
9208 KB |
Output is correct |
11 |
Correct |
335 ms |
9228 KB |
Output is correct |
12 |
Correct |
335 ms |
9208 KB |
Output is correct |
13 |
Correct |
334 ms |
9296 KB |
Output is correct |
14 |
Correct |
337 ms |
9336 KB |
Output is correct |
15 |
Correct |
334 ms |
9212 KB |
Output is correct |
16 |
Correct |
257 ms |
7800 KB |
Output is correct |
17 |
Correct |
256 ms |
7672 KB |
Output is correct |
18 |
Correct |
258 ms |
7764 KB |
Output is correct |
19 |
Correct |
256 ms |
7672 KB |
Output is correct |
20 |
Correct |
258 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
9208 KB |
Output is correct |
2 |
Correct |
335 ms |
9464 KB |
Output is correct |
3 |
Correct |
336 ms |
9248 KB |
Output is correct |
4 |
Correct |
337 ms |
9464 KB |
Output is correct |
5 |
Correct |
337 ms |
9208 KB |
Output is correct |
6 |
Correct |
331 ms |
9312 KB |
Output is correct |
7 |
Correct |
335 ms |
9208 KB |
Output is correct |
8 |
Correct |
334 ms |
9336 KB |
Output is correct |
9 |
Correct |
335 ms |
9208 KB |
Output is correct |
10 |
Correct |
336 ms |
9208 KB |
Output is correct |
11 |
Correct |
335 ms |
9228 KB |
Output is correct |
12 |
Correct |
335 ms |
9208 KB |
Output is correct |
13 |
Correct |
334 ms |
9296 KB |
Output is correct |
14 |
Correct |
337 ms |
9336 KB |
Output is correct |
15 |
Correct |
334 ms |
9212 KB |
Output is correct |
16 |
Correct |
257 ms |
7800 KB |
Output is correct |
17 |
Correct |
256 ms |
7672 KB |
Output is correct |
18 |
Correct |
258 ms |
7764 KB |
Output is correct |
19 |
Correct |
256 ms |
7672 KB |
Output is correct |
20 |
Correct |
258 ms |
7800 KB |
Output is correct |
21 |
Correct |
676 ms |
10892 KB |
Output is correct |
22 |
Correct |
675 ms |
10872 KB |
Output is correct |
23 |
Correct |
639 ms |
10872 KB |
Output is correct |
24 |
Correct |
666 ms |
11000 KB |
Output is correct |
25 |
Correct |
667 ms |
10872 KB |
Output is correct |
26 |
Correct |
1004 ms |
10868 KB |
Output is correct |
27 |
Correct |
1016 ms |
10844 KB |
Output is correct |
28 |
Correct |
1016 ms |
10872 KB |
Output is correct |
29 |
Correct |
979 ms |
10748 KB |
Output is correct |
30 |
Correct |
424 ms |
11264 KB |
Output is correct |
31 |
Correct |
424 ms |
11132 KB |
Output is correct |
32 |
Correct |
417 ms |
11000 KB |
Output is correct |
33 |
Correct |
418 ms |
11000 KB |
Output is correct |
34 |
Correct |
419 ms |
11128 KB |
Output is correct |
35 |
Correct |
333 ms |
9336 KB |
Output is correct |
36 |
Correct |
337 ms |
9336 KB |
Output is correct |
37 |
Correct |
336 ms |
9468 KB |
Output is correct |
38 |
Correct |
336 ms |
9292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
5760 KB |
Output is correct |
2 |
Correct |
14 ms |
5760 KB |
Output is correct |
3 |
Correct |
15 ms |
5680 KB |
Output is correct |
4 |
Correct |
18 ms |
5752 KB |
Output is correct |
5 |
Correct |
16 ms |
5760 KB |
Output is correct |
6 |
Correct |
20 ms |
5756 KB |
Output is correct |
7 |
Correct |
19 ms |
5760 KB |
Output is correct |
8 |
Correct |
18 ms |
5752 KB |
Output is correct |
9 |
Correct |
18 ms |
5760 KB |
Output is correct |
10 |
Correct |
18 ms |
5760 KB |
Output is correct |
11 |
Correct |
15 ms |
5760 KB |
Output is correct |
12 |
Correct |
15 ms |
5760 KB |
Output is correct |
13 |
Correct |
15 ms |
5760 KB |
Output is correct |
14 |
Correct |
18 ms |
5760 KB |
Output is correct |
15 |
Correct |
15 ms |
5760 KB |
Output is correct |
16 |
Correct |
10 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5248 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
10 ms |
5300 KB |
Output is correct |
20 |
Correct |
12 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
9208 KB |
Output is correct |
2 |
Correct |
335 ms |
9464 KB |
Output is correct |
3 |
Correct |
336 ms |
9248 KB |
Output is correct |
4 |
Correct |
337 ms |
9464 KB |
Output is correct |
5 |
Correct |
337 ms |
9208 KB |
Output is correct |
6 |
Correct |
331 ms |
9312 KB |
Output is correct |
7 |
Correct |
335 ms |
9208 KB |
Output is correct |
8 |
Correct |
334 ms |
9336 KB |
Output is correct |
9 |
Correct |
335 ms |
9208 KB |
Output is correct |
10 |
Correct |
336 ms |
9208 KB |
Output is correct |
11 |
Correct |
335 ms |
9228 KB |
Output is correct |
12 |
Correct |
335 ms |
9208 KB |
Output is correct |
13 |
Correct |
334 ms |
9296 KB |
Output is correct |
14 |
Correct |
337 ms |
9336 KB |
Output is correct |
15 |
Correct |
334 ms |
9212 KB |
Output is correct |
16 |
Correct |
257 ms |
7800 KB |
Output is correct |
17 |
Correct |
256 ms |
7672 KB |
Output is correct |
18 |
Correct |
258 ms |
7764 KB |
Output is correct |
19 |
Correct |
256 ms |
7672 KB |
Output is correct |
20 |
Correct |
258 ms |
7800 KB |
Output is correct |
21 |
Correct |
676 ms |
10892 KB |
Output is correct |
22 |
Correct |
675 ms |
10872 KB |
Output is correct |
23 |
Correct |
639 ms |
10872 KB |
Output is correct |
24 |
Correct |
666 ms |
11000 KB |
Output is correct |
25 |
Correct |
667 ms |
10872 KB |
Output is correct |
26 |
Correct |
1004 ms |
10868 KB |
Output is correct |
27 |
Correct |
1016 ms |
10844 KB |
Output is correct |
28 |
Correct |
1016 ms |
10872 KB |
Output is correct |
29 |
Correct |
979 ms |
10748 KB |
Output is correct |
30 |
Correct |
424 ms |
11264 KB |
Output is correct |
31 |
Correct |
424 ms |
11132 KB |
Output is correct |
32 |
Correct |
417 ms |
11000 KB |
Output is correct |
33 |
Correct |
418 ms |
11000 KB |
Output is correct |
34 |
Correct |
419 ms |
11128 KB |
Output is correct |
35 |
Correct |
333 ms |
9336 KB |
Output is correct |
36 |
Correct |
337 ms |
9336 KB |
Output is correct |
37 |
Correct |
336 ms |
9468 KB |
Output is correct |
38 |
Correct |
336 ms |
9292 KB |
Output is correct |
39 |
Correct |
14 ms |
5760 KB |
Output is correct |
40 |
Correct |
14 ms |
5760 KB |
Output is correct |
41 |
Correct |
15 ms |
5680 KB |
Output is correct |
42 |
Correct |
18 ms |
5752 KB |
Output is correct |
43 |
Correct |
16 ms |
5760 KB |
Output is correct |
44 |
Correct |
20 ms |
5756 KB |
Output is correct |
45 |
Correct |
19 ms |
5760 KB |
Output is correct |
46 |
Correct |
18 ms |
5752 KB |
Output is correct |
47 |
Correct |
18 ms |
5760 KB |
Output is correct |
48 |
Correct |
18 ms |
5760 KB |
Output is correct |
49 |
Correct |
15 ms |
5760 KB |
Output is correct |
50 |
Correct |
15 ms |
5760 KB |
Output is correct |
51 |
Correct |
15 ms |
5760 KB |
Output is correct |
52 |
Correct |
18 ms |
5760 KB |
Output is correct |
53 |
Correct |
15 ms |
5760 KB |
Output is correct |
54 |
Correct |
10 ms |
5248 KB |
Output is correct |
55 |
Correct |
9 ms |
5248 KB |
Output is correct |
56 |
Correct |
9 ms |
5248 KB |
Output is correct |
57 |
Correct |
10 ms |
5300 KB |
Output is correct |
58 |
Correct |
12 ms |
5248 KB |
Output is correct |
59 |
Correct |
703 ms |
11256 KB |
Output is correct |
60 |
Correct |
681 ms |
11388 KB |
Output is correct |
61 |
Correct |
666 ms |
11384 KB |
Output is correct |
62 |
Correct |
721 ms |
11256 KB |
Output is correct |
63 |
Correct |
695 ms |
11384 KB |
Output is correct |
64 |
Correct |
1090 ms |
11384 KB |
Output is correct |
65 |
Correct |
1102 ms |
11400 KB |
Output is correct |
66 |
Correct |
1102 ms |
11236 KB |
Output is correct |
67 |
Correct |
1094 ms |
11236 KB |
Output is correct |
68 |
Correct |
1133 ms |
11512 KB |
Output is correct |
69 |
Correct |
676 ms |
11404 KB |
Output is correct |
70 |
Correct |
676 ms |
11256 KB |
Output is correct |
71 |
Correct |
675 ms |
11256 KB |
Output is correct |
72 |
Correct |
685 ms |
11256 KB |
Output is correct |
73 |
Correct |
688 ms |
11256 KB |
Output is correct |
74 |
Correct |
293 ms |
7672 KB |
Output is correct |
75 |
Correct |
294 ms |
7796 KB |
Output is correct |
76 |
Correct |
292 ms |
7676 KB |
Output is correct |
77 |
Correct |
306 ms |
7800 KB |
Output is correct |
78 |
Correct |
297 ms |
7724 KB |
Output is correct |