#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 5007
using namespace std;
struct group{
int from,to;
}s[2*MAXN];
const long long mod=1e9+7;
int n,a[MAXN],b[MAXN],m;
vector<int> pos;
bool in(group g,int id){
return g.from>=a[id] and g.to<=b[id];
}
long long dp[2][MAXN][507][2];
long long invfact[MAXN];
long long cc[507][MAXN];
long long comb(int x,int g){
return cc[x][g];
}
long long power(long long a,int b){
if(b==0)return 1;
if(b==1)return a;
if(b==2)return (a*a)%mod;
if(b%2==0)return power(power(a,b/2),2);
return (a*power(power(a,b/2),2))%mod;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
pos.push_back(a[i]);
pos.push_back(b[i]);
}
invfact[0]=1;
for(long long i=1;i<=n;i++)invfact[i]=(invfact[i-1]*i)%mod;
for(long long i=1;i<=n;i++)invfact[i]=power(invfact[i],mod-2);
sort(pos.begin(),pos.end());
for(int i=0;i<pos.size()-1;i++){
if(pos[i]!=pos[i+1]){
m++; s[m]={pos[i],pos[i]};
if(pos[i]+1<=pos[i+1]-1){
m++; s[m]={pos[i]+1,pos[i+1]-1};
}
}
}
m++;
s[m]={pos.back(),pos.back()};
for(int i=1;i<=m;i++){
cc[0][i]=1;
for(int f=1;f<=min(n,s[i].to-s[i].from+1);f++){
cc[f][i]=(long long) cc[f-1][i]*(s[i].to-s[i].from+1-(f-1));
cc[f][i]%=mod;
}
for(int f=1;f<=min(n,s[i].to-s[i].from+1);f++){
cc[f][i]*=invfact[f];
cc[f][i]%=mod;
}
}
for(int i=0;i<=m;i++){
for(int c=0;c<=n;c++){
if(i==0)dp[0][i][c][1]=1;
else dp[0][i][c][1]=comb(c,i);
}
}
for(int i=1;i<=n;i++){
dp[i%2][0][0][1]=1;
for(int f=1;f<=m;f++){
for(int c=0;c<=n;c++){
for(int t=0;t<2;t++){
dp[i%2][f][c][t]=dp[1-i%2][f][c][0];
if(t==1)dp[i%2][f][c][t]+=dp[i%2][f-1][0][1]*comb(c,f);
if(in(s[f],i) and c+1<=s[f].to-s[f].from+1){
dp[i%2][f][c][t]+=dp[1-i%2][f][c+1][1];
}
dp[i%2][f][c][t]%=mod;
}
}
}
}
cout<<(dp[n%2][m][0][1]-1+mod)%mod<<"\n";
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=0;i<pos.size()-1;i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
744 ms |
22104 KB |
Output is correct |
2 |
Correct |
755 ms |
22320 KB |
Output is correct |
3 |
Correct |
719 ms |
22316 KB |
Output is correct |
4 |
Correct |
718 ms |
22108 KB |
Output is correct |
5 |
Correct |
706 ms |
22316 KB |
Output is correct |
6 |
Correct |
720 ms |
22316 KB |
Output is correct |
7 |
Correct |
714 ms |
22108 KB |
Output is correct |
8 |
Correct |
706 ms |
22308 KB |
Output is correct |
9 |
Correct |
704 ms |
22316 KB |
Output is correct |
10 |
Correct |
708 ms |
22356 KB |
Output is correct |
11 |
Correct |
717 ms |
22108 KB |
Output is correct |
12 |
Correct |
740 ms |
22104 KB |
Output is correct |
13 |
Correct |
701 ms |
22104 KB |
Output is correct |
14 |
Correct |
731 ms |
22316 KB |
Output is correct |
15 |
Correct |
736 ms |
22312 KB |
Output is correct |
16 |
Correct |
123 ms |
5980 KB |
Output is correct |
17 |
Correct |
133 ms |
6284 KB |
Output is correct |
18 |
Correct |
126 ms |
5980 KB |
Output is correct |
19 |
Correct |
132 ms |
6232 KB |
Output is correct |
20 |
Correct |
127 ms |
5928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
744 ms |
22104 KB |
Output is correct |
2 |
Correct |
755 ms |
22320 KB |
Output is correct |
3 |
Correct |
719 ms |
22316 KB |
Output is correct |
4 |
Correct |
718 ms |
22108 KB |
Output is correct |
5 |
Correct |
706 ms |
22316 KB |
Output is correct |
6 |
Correct |
720 ms |
22316 KB |
Output is correct |
7 |
Correct |
714 ms |
22108 KB |
Output is correct |
8 |
Correct |
706 ms |
22308 KB |
Output is correct |
9 |
Correct |
704 ms |
22316 KB |
Output is correct |
10 |
Correct |
708 ms |
22356 KB |
Output is correct |
11 |
Correct |
717 ms |
22108 KB |
Output is correct |
12 |
Correct |
740 ms |
22104 KB |
Output is correct |
13 |
Correct |
701 ms |
22104 KB |
Output is correct |
14 |
Correct |
731 ms |
22316 KB |
Output is correct |
15 |
Correct |
736 ms |
22312 KB |
Output is correct |
16 |
Correct |
123 ms |
5980 KB |
Output is correct |
17 |
Correct |
133 ms |
6284 KB |
Output is correct |
18 |
Correct |
126 ms |
5980 KB |
Output is correct |
19 |
Correct |
132 ms |
6232 KB |
Output is correct |
20 |
Correct |
127 ms |
5928 KB |
Output is correct |
21 |
Correct |
1290 ms |
29276 KB |
Output is correct |
22 |
Correct |
1312 ms |
29684 KB |
Output is correct |
23 |
Correct |
1315 ms |
29248 KB |
Output is correct |
24 |
Correct |
1291 ms |
29276 KB |
Output is correct |
25 |
Correct |
1240 ms |
28764 KB |
Output is correct |
26 |
Correct |
1227 ms |
27668 KB |
Output is correct |
27 |
Correct |
1286 ms |
27740 KB |
Output is correct |
28 |
Correct |
1212 ms |
27812 KB |
Output is correct |
29 |
Correct |
1211 ms |
27996 KB |
Output is correct |
30 |
Correct |
1911 ms |
39260 KB |
Output is correct |
31 |
Correct |
1806 ms |
39000 KB |
Output is correct |
32 |
Correct |
1850 ms |
38992 KB |
Output is correct |
33 |
Correct |
1876 ms |
39168 KB |
Output is correct |
34 |
Correct |
1840 ms |
38748 KB |
Output is correct |
35 |
Correct |
728 ms |
22316 KB |
Output is correct |
36 |
Correct |
719 ms |
22108 KB |
Output is correct |
37 |
Correct |
719 ms |
22312 KB |
Output is correct |
38 |
Correct |
710 ms |
22272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5468 KB |
Output is correct |
2 |
Correct |
15 ms |
5676 KB |
Output is correct |
3 |
Correct |
16 ms |
5468 KB |
Output is correct |
4 |
Correct |
14 ms |
5468 KB |
Output is correct |
5 |
Correct |
15 ms |
5468 KB |
Output is correct |
6 |
Correct |
15 ms |
5676 KB |
Output is correct |
7 |
Correct |
16 ms |
5468 KB |
Output is correct |
8 |
Correct |
15 ms |
5676 KB |
Output is correct |
9 |
Correct |
15 ms |
5464 KB |
Output is correct |
10 |
Correct |
15 ms |
5468 KB |
Output is correct |
11 |
Correct |
16 ms |
5680 KB |
Output is correct |
12 |
Correct |
15 ms |
5680 KB |
Output is correct |
13 |
Correct |
15 ms |
5468 KB |
Output is correct |
14 |
Correct |
15 ms |
5468 KB |
Output is correct |
15 |
Correct |
15 ms |
5716 KB |
Output is correct |
16 |
Correct |
7 ms |
2904 KB |
Output is correct |
17 |
Correct |
7 ms |
2908 KB |
Output is correct |
18 |
Correct |
6 ms |
2908 KB |
Output is correct |
19 |
Correct |
6 ms |
2908 KB |
Output is correct |
20 |
Correct |
7 ms |
2928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
744 ms |
22104 KB |
Output is correct |
2 |
Correct |
755 ms |
22320 KB |
Output is correct |
3 |
Correct |
719 ms |
22316 KB |
Output is correct |
4 |
Correct |
718 ms |
22108 KB |
Output is correct |
5 |
Correct |
706 ms |
22316 KB |
Output is correct |
6 |
Correct |
720 ms |
22316 KB |
Output is correct |
7 |
Correct |
714 ms |
22108 KB |
Output is correct |
8 |
Correct |
706 ms |
22308 KB |
Output is correct |
9 |
Correct |
704 ms |
22316 KB |
Output is correct |
10 |
Correct |
708 ms |
22356 KB |
Output is correct |
11 |
Correct |
717 ms |
22108 KB |
Output is correct |
12 |
Correct |
740 ms |
22104 KB |
Output is correct |
13 |
Correct |
701 ms |
22104 KB |
Output is correct |
14 |
Correct |
731 ms |
22316 KB |
Output is correct |
15 |
Correct |
736 ms |
22312 KB |
Output is correct |
16 |
Correct |
123 ms |
5980 KB |
Output is correct |
17 |
Correct |
133 ms |
6284 KB |
Output is correct |
18 |
Correct |
126 ms |
5980 KB |
Output is correct |
19 |
Correct |
132 ms |
6232 KB |
Output is correct |
20 |
Correct |
127 ms |
5928 KB |
Output is correct |
21 |
Correct |
1290 ms |
29276 KB |
Output is correct |
22 |
Correct |
1312 ms |
29684 KB |
Output is correct |
23 |
Correct |
1315 ms |
29248 KB |
Output is correct |
24 |
Correct |
1291 ms |
29276 KB |
Output is correct |
25 |
Correct |
1240 ms |
28764 KB |
Output is correct |
26 |
Correct |
1227 ms |
27668 KB |
Output is correct |
27 |
Correct |
1286 ms |
27740 KB |
Output is correct |
28 |
Correct |
1212 ms |
27812 KB |
Output is correct |
29 |
Correct |
1211 ms |
27996 KB |
Output is correct |
30 |
Correct |
1911 ms |
39260 KB |
Output is correct |
31 |
Correct |
1806 ms |
39000 KB |
Output is correct |
32 |
Correct |
1850 ms |
38992 KB |
Output is correct |
33 |
Correct |
1876 ms |
39168 KB |
Output is correct |
34 |
Correct |
1840 ms |
38748 KB |
Output is correct |
35 |
Correct |
728 ms |
22316 KB |
Output is correct |
36 |
Correct |
719 ms |
22108 KB |
Output is correct |
37 |
Correct |
719 ms |
22312 KB |
Output is correct |
38 |
Correct |
710 ms |
22272 KB |
Output is correct |
39 |
Correct |
16 ms |
5468 KB |
Output is correct |
40 |
Correct |
15 ms |
5676 KB |
Output is correct |
41 |
Correct |
16 ms |
5468 KB |
Output is correct |
42 |
Correct |
14 ms |
5468 KB |
Output is correct |
43 |
Correct |
15 ms |
5468 KB |
Output is correct |
44 |
Correct |
15 ms |
5676 KB |
Output is correct |
45 |
Correct |
16 ms |
5468 KB |
Output is correct |
46 |
Correct |
15 ms |
5676 KB |
Output is correct |
47 |
Correct |
15 ms |
5464 KB |
Output is correct |
48 |
Correct |
15 ms |
5468 KB |
Output is correct |
49 |
Correct |
16 ms |
5680 KB |
Output is correct |
50 |
Correct |
15 ms |
5680 KB |
Output is correct |
51 |
Correct |
15 ms |
5468 KB |
Output is correct |
52 |
Correct |
15 ms |
5468 KB |
Output is correct |
53 |
Correct |
15 ms |
5716 KB |
Output is correct |
54 |
Correct |
7 ms |
2904 KB |
Output is correct |
55 |
Correct |
7 ms |
2908 KB |
Output is correct |
56 |
Correct |
6 ms |
2908 KB |
Output is correct |
57 |
Correct |
6 ms |
2908 KB |
Output is correct |
58 |
Correct |
7 ms |
2928 KB |
Output is correct |
59 |
Correct |
1783 ms |
41856 KB |
Output is correct |
60 |
Correct |
1780 ms |
42072 KB |
Output is correct |
61 |
Correct |
1800 ms |
41976 KB |
Output is correct |
62 |
Correct |
1718 ms |
42072 KB |
Output is correct |
63 |
Correct |
1801 ms |
42080 KB |
Output is correct |
64 |
Correct |
1833 ms |
42072 KB |
Output is correct |
65 |
Correct |
1833 ms |
42072 KB |
Output is correct |
66 |
Correct |
1848 ms |
42076 KB |
Output is correct |
67 |
Correct |
1772 ms |
42076 KB |
Output is correct |
68 |
Correct |
1793 ms |
42076 KB |
Output is correct |
69 |
Correct |
1898 ms |
42064 KB |
Output is correct |
70 |
Correct |
1941 ms |
42064 KB |
Output is correct |
71 |
Correct |
1793 ms |
42076 KB |
Output is correct |
72 |
Correct |
1829 ms |
42076 KB |
Output is correct |
73 |
Correct |
1860 ms |
42076 KB |
Output is correct |
74 |
Correct |
151 ms |
6492 KB |
Output is correct |
75 |
Correct |
145 ms |
6368 KB |
Output is correct |
76 |
Correct |
146 ms |
6492 KB |
Output is correct |
77 |
Correct |
146 ms |
6492 KB |
Output is correct |
78 |
Correct |
150 ms |
6488 KB |
Output is correct |