#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll n,c[1001],st[501],ed[501],cnt,comb[501];
ll dp[501][1001];
set<int> ss;
ll power(ll a, ll m) { //a^m;
ll ans=1;
while (m) {
if (m%2==1) ans=(ans*a)%mod;
a=(a*a)%mod;
m/=2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) {
cin>>st[i]>>ed[i]; --st[i];
ss.insert(st[i]);
ss.insert(ed[i]);
}
for (int i=1; i<=n; ++i) comb[i]=power(i,mod-2)%mod;
cnt=ss.size()-1;
int id=0;
for (auto it=ss.begin(); it!=ss.end(); ++it) c[id++]=*it;
for (int i=0; i<=cnt; ++i) dp[0][i]=1;
for (int i=1; i<=n; ++i) {
dp[i][0]=1; //don't send any boat
for (int j=1; j<=cnt; ++j) { //consider from the range from c[j-1]+1 to c[j]
dp[i][j]=dp[i][j-1];
ll idx=1,com=1;
for (auto k=i; k>0; --k) {
if (st[k]<c[j] && c[j]<=ed[k]) {
com=((com*(c[j]-c[j-1]+idx-1)%mod)*comb[idx])%mod;
dp[i][j]=(dp[i][j]+dp[k-1][j-1]*com)%mod;
++idx;
}
}
}
}
cout<<(dp[n][cnt]-1+mod)%mod;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
4172 KB |
Output is correct |
2 |
Correct |
235 ms |
4272 KB |
Output is correct |
3 |
Correct |
224 ms |
4324 KB |
Output is correct |
4 |
Correct |
213 ms |
4236 KB |
Output is correct |
5 |
Correct |
216 ms |
4308 KB |
Output is correct |
6 |
Correct |
133 ms |
4288 KB |
Output is correct |
7 |
Correct |
131 ms |
4268 KB |
Output is correct |
8 |
Correct |
140 ms |
4216 KB |
Output is correct |
9 |
Correct |
152 ms |
4288 KB |
Output is correct |
10 |
Correct |
133 ms |
4204 KB |
Output is correct |
11 |
Correct |
136 ms |
4276 KB |
Output is correct |
12 |
Correct |
140 ms |
4300 KB |
Output is correct |
13 |
Correct |
135 ms |
4228 KB |
Output is correct |
14 |
Correct |
123 ms |
4268 KB |
Output is correct |
15 |
Correct |
140 ms |
4368 KB |
Output is correct |
16 |
Correct |
46 ms |
3012 KB |
Output is correct |
17 |
Correct |
48 ms |
3048 KB |
Output is correct |
18 |
Correct |
47 ms |
2956 KB |
Output is correct |
19 |
Correct |
50 ms |
3020 KB |
Output is correct |
20 |
Correct |
48 ms |
2964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
4172 KB |
Output is correct |
2 |
Correct |
235 ms |
4272 KB |
Output is correct |
3 |
Correct |
224 ms |
4324 KB |
Output is correct |
4 |
Correct |
213 ms |
4236 KB |
Output is correct |
5 |
Correct |
216 ms |
4308 KB |
Output is correct |
6 |
Correct |
133 ms |
4288 KB |
Output is correct |
7 |
Correct |
131 ms |
4268 KB |
Output is correct |
8 |
Correct |
140 ms |
4216 KB |
Output is correct |
9 |
Correct |
152 ms |
4288 KB |
Output is correct |
10 |
Correct |
133 ms |
4204 KB |
Output is correct |
11 |
Correct |
136 ms |
4276 KB |
Output is correct |
12 |
Correct |
140 ms |
4300 KB |
Output is correct |
13 |
Correct |
135 ms |
4228 KB |
Output is correct |
14 |
Correct |
123 ms |
4268 KB |
Output is correct |
15 |
Correct |
140 ms |
4368 KB |
Output is correct |
16 |
Correct |
46 ms |
3012 KB |
Output is correct |
17 |
Correct |
48 ms |
3048 KB |
Output is correct |
18 |
Correct |
47 ms |
2956 KB |
Output is correct |
19 |
Correct |
50 ms |
3020 KB |
Output is correct |
20 |
Correct |
48 ms |
2964 KB |
Output is correct |
21 |
Correct |
452 ms |
4264 KB |
Output is correct |
22 |
Correct |
440 ms |
4360 KB |
Output is correct |
23 |
Correct |
416 ms |
4320 KB |
Output is correct |
24 |
Correct |
440 ms |
4376 KB |
Output is correct |
25 |
Correct |
456 ms |
4228 KB |
Output is correct |
26 |
Correct |
626 ms |
4468 KB |
Output is correct |
27 |
Correct |
640 ms |
4504 KB |
Output is correct |
28 |
Correct |
619 ms |
4284 KB |
Output is correct |
29 |
Correct |
617 ms |
4300 KB |
Output is correct |
30 |
Correct |
140 ms |
4260 KB |
Output is correct |
31 |
Correct |
138 ms |
4300 KB |
Output is correct |
32 |
Correct |
135 ms |
4196 KB |
Output is correct |
33 |
Correct |
135 ms |
4332 KB |
Output is correct |
34 |
Correct |
133 ms |
4240 KB |
Output is correct |
35 |
Correct |
238 ms |
4492 KB |
Output is correct |
36 |
Correct |
230 ms |
4196 KB |
Output is correct |
37 |
Correct |
227 ms |
4264 KB |
Output is correct |
38 |
Correct |
232 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
852 KB |
Output is correct |
2 |
Correct |
4 ms |
852 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
5 ms |
852 KB |
Output is correct |
5 |
Correct |
5 ms |
852 KB |
Output is correct |
6 |
Correct |
6 ms |
852 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
844 KB |
Output is correct |
9 |
Correct |
6 ms |
852 KB |
Output is correct |
10 |
Correct |
6 ms |
844 KB |
Output is correct |
11 |
Correct |
6 ms |
844 KB |
Output is correct |
12 |
Correct |
5 ms |
816 KB |
Output is correct |
13 |
Correct |
5 ms |
912 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
5 ms |
852 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
4172 KB |
Output is correct |
2 |
Correct |
235 ms |
4272 KB |
Output is correct |
3 |
Correct |
224 ms |
4324 KB |
Output is correct |
4 |
Correct |
213 ms |
4236 KB |
Output is correct |
5 |
Correct |
216 ms |
4308 KB |
Output is correct |
6 |
Correct |
133 ms |
4288 KB |
Output is correct |
7 |
Correct |
131 ms |
4268 KB |
Output is correct |
8 |
Correct |
140 ms |
4216 KB |
Output is correct |
9 |
Correct |
152 ms |
4288 KB |
Output is correct |
10 |
Correct |
133 ms |
4204 KB |
Output is correct |
11 |
Correct |
136 ms |
4276 KB |
Output is correct |
12 |
Correct |
140 ms |
4300 KB |
Output is correct |
13 |
Correct |
135 ms |
4228 KB |
Output is correct |
14 |
Correct |
123 ms |
4268 KB |
Output is correct |
15 |
Correct |
140 ms |
4368 KB |
Output is correct |
16 |
Correct |
46 ms |
3012 KB |
Output is correct |
17 |
Correct |
48 ms |
3048 KB |
Output is correct |
18 |
Correct |
47 ms |
2956 KB |
Output is correct |
19 |
Correct |
50 ms |
3020 KB |
Output is correct |
20 |
Correct |
48 ms |
2964 KB |
Output is correct |
21 |
Correct |
452 ms |
4264 KB |
Output is correct |
22 |
Correct |
440 ms |
4360 KB |
Output is correct |
23 |
Correct |
416 ms |
4320 KB |
Output is correct |
24 |
Correct |
440 ms |
4376 KB |
Output is correct |
25 |
Correct |
456 ms |
4228 KB |
Output is correct |
26 |
Correct |
626 ms |
4468 KB |
Output is correct |
27 |
Correct |
640 ms |
4504 KB |
Output is correct |
28 |
Correct |
619 ms |
4284 KB |
Output is correct |
29 |
Correct |
617 ms |
4300 KB |
Output is correct |
30 |
Correct |
140 ms |
4260 KB |
Output is correct |
31 |
Correct |
138 ms |
4300 KB |
Output is correct |
32 |
Correct |
135 ms |
4196 KB |
Output is correct |
33 |
Correct |
135 ms |
4332 KB |
Output is correct |
34 |
Correct |
133 ms |
4240 KB |
Output is correct |
35 |
Correct |
238 ms |
4492 KB |
Output is correct |
36 |
Correct |
230 ms |
4196 KB |
Output is correct |
37 |
Correct |
227 ms |
4264 KB |
Output is correct |
38 |
Correct |
232 ms |
4244 KB |
Output is correct |
39 |
Correct |
5 ms |
852 KB |
Output is correct |
40 |
Correct |
4 ms |
852 KB |
Output is correct |
41 |
Correct |
5 ms |
852 KB |
Output is correct |
42 |
Correct |
5 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
852 KB |
Output is correct |
44 |
Correct |
6 ms |
852 KB |
Output is correct |
45 |
Correct |
6 ms |
852 KB |
Output is correct |
46 |
Correct |
6 ms |
844 KB |
Output is correct |
47 |
Correct |
6 ms |
852 KB |
Output is correct |
48 |
Correct |
6 ms |
844 KB |
Output is correct |
49 |
Correct |
6 ms |
844 KB |
Output is correct |
50 |
Correct |
5 ms |
816 KB |
Output is correct |
51 |
Correct |
5 ms |
912 KB |
Output is correct |
52 |
Correct |
5 ms |
860 KB |
Output is correct |
53 |
Correct |
5 ms |
852 KB |
Output is correct |
54 |
Correct |
3 ms |
852 KB |
Output is correct |
55 |
Correct |
3 ms |
852 KB |
Output is correct |
56 |
Correct |
3 ms |
724 KB |
Output is correct |
57 |
Correct |
3 ms |
724 KB |
Output is correct |
58 |
Correct |
3 ms |
852 KB |
Output is correct |
59 |
Correct |
498 ms |
4296 KB |
Output is correct |
60 |
Correct |
475 ms |
4440 KB |
Output is correct |
61 |
Correct |
473 ms |
4376 KB |
Output is correct |
62 |
Correct |
502 ms |
4280 KB |
Output is correct |
63 |
Correct |
483 ms |
4296 KB |
Output is correct |
64 |
Correct |
713 ms |
4324 KB |
Output is correct |
65 |
Correct |
718 ms |
4208 KB |
Output is correct |
66 |
Correct |
722 ms |
4268 KB |
Output is correct |
67 |
Correct |
717 ms |
4348 KB |
Output is correct |
68 |
Correct |
716 ms |
4216 KB |
Output is correct |
69 |
Correct |
545 ms |
4296 KB |
Output is correct |
70 |
Correct |
548 ms |
4292 KB |
Output is correct |
71 |
Correct |
550 ms |
4248 KB |
Output is correct |
72 |
Correct |
566 ms |
4308 KB |
Output is correct |
73 |
Correct |
556 ms |
4192 KB |
Output is correct |
74 |
Correct |
100 ms |
3020 KB |
Output is correct |
75 |
Correct |
103 ms |
2948 KB |
Output is correct |
76 |
Correct |
102 ms |
3056 KB |
Output is correct |
77 |
Correct |
105 ms |
2988 KB |
Output is correct |
78 |
Correct |
103 ms |
2928 KB |
Output is correct |