#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=5e2+10;
const ll mod=1e9+7;
struct st{
int a,b,dl;
};
st pw[N*2];
ll dp[N][N*2];
ll dp2[N*2][N];
ll dp3[N*2][N];
pair<int,int>w[N];
ll sil[N],sp[N][N*2];
ll d1[N][N],d2[N*2][N];
ll power(ll n,ll k){
ll res=1;
while(k>0){
if(k&1) (res*=n)%=mod;
k>>=1;
(n*=n)%=mod;
}
return res;
}
int dwu(ll n,ll k){
if(k>n) return 0;
if(n<=500 and k<=500) return (sil[n]*(power((sil[k]*sil[n-k])%mod,mod-2)))%mod;
ll curr=1;
for(int i=n;i>=n-k+1;i--) (curr*=(ll)i)%=mod;
return (curr*power(sil[k],mod-2))%mod;
}
void solve(){
int n;
cin>>n;
vi v;
for(int i=1;i<=n;i++){
cin>>w[i].fi>>w[i].se;
v.push_back(w[i].fi),v.push_back(w[i].se+1);
}
sort(v.begin(),v.end());
int l=0;
for(int i=0;i<v.size()-1;i++){
if(v[i]==v[i+1]) continue;
st pom;
pom.a=v[i],pom.b=v[i+1]-1,pom.dl=(pom.b-pom.a+1);
pw[++l]=pom;
}
sil[0]=1;
for(int i=1;i<=500;i++) (sil[i]=sil[i-1]*(ll)i)%=mod;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++) d1[i][j]=dwu(i,j);
for(int i=1;i<=l;i++)
for(int j=1;j<=n;j++) d2[i][j]=dwu(pw[i].dl,j);
for(int i=1;i<=l;i++){
for(int j=1;j<=n;j++){
dp2[i][j]=pw[i].dl;
for(int k=min(j,pw[i].dl);k>=2;k--) (dp2[i][j]+=d1[j-1][k-1]*d2[i][k])%=mod;
}
}
for(int i=1;i<=l;i++){
for(int j=2;j<=n;j++){
if(pw[i].dl==1) continue;
dp3[i][j]=(ll)(pw[i].dl*(pw[i].dl-1LL))/2;
dp3[i][j]%=mod;
for(int k=min(j,pw[i].dl);k>=3;k--) (dp3[i][j]+=d1[j-2][k-2]*d2[i][k])%=mod;
}
}
ll res=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=l;j++){
if(pw[j].b<w[i].fi or pw[j].a>w[i].se) continue;
dp[i][j]=pw[j].dl;
int il=1;
for(int k=i-1;k>=1;k--){
(dp[i][j]+=sp[k][j-1]*dp2[j][il])%=mod;
if(pw[j].a>=w[k].fi and pw[j].b<=w[k].se){
il++;
dp[i][j]+=dp3[j][il];
}
}
dp[i][j]%=mod;
res+=dp[i][j];
}
for(int j=1;j<=l;j++) sp[i][j]=(sp[i][j-1]+dp[i][j])%mod;
}
cout<<res%mod<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
boat.cpp: In function 'void solve()':
boat.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0;i<v.size()-1;i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
802 ms |
20188 KB |
Output is correct |
2 |
Correct |
815 ms |
20228 KB |
Output is correct |
3 |
Correct |
805 ms |
20252 KB |
Output is correct |
4 |
Correct |
812 ms |
20184 KB |
Output is correct |
5 |
Correct |
812 ms |
20184 KB |
Output is correct |
6 |
Correct |
796 ms |
20280 KB |
Output is correct |
7 |
Correct |
827 ms |
20160 KB |
Output is correct |
8 |
Correct |
809 ms |
20252 KB |
Output is correct |
9 |
Correct |
819 ms |
20380 KB |
Output is correct |
10 |
Correct |
853 ms |
20208 KB |
Output is correct |
11 |
Correct |
812 ms |
20276 KB |
Output is correct |
12 |
Correct |
808 ms |
20228 KB |
Output is correct |
13 |
Correct |
813 ms |
20348 KB |
Output is correct |
14 |
Correct |
808 ms |
20272 KB |
Output is correct |
15 |
Correct |
817 ms |
20244 KB |
Output is correct |
16 |
Correct |
163 ms |
9132 KB |
Output is correct |
17 |
Correct |
172 ms |
9344 KB |
Output is correct |
18 |
Correct |
180 ms |
9228 KB |
Output is correct |
19 |
Correct |
172 ms |
9344 KB |
Output is correct |
20 |
Correct |
164 ms |
9164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
802 ms |
20188 KB |
Output is correct |
2 |
Correct |
815 ms |
20228 KB |
Output is correct |
3 |
Correct |
805 ms |
20252 KB |
Output is correct |
4 |
Correct |
812 ms |
20184 KB |
Output is correct |
5 |
Correct |
812 ms |
20184 KB |
Output is correct |
6 |
Correct |
796 ms |
20280 KB |
Output is correct |
7 |
Correct |
827 ms |
20160 KB |
Output is correct |
8 |
Correct |
809 ms |
20252 KB |
Output is correct |
9 |
Correct |
819 ms |
20380 KB |
Output is correct |
10 |
Correct |
853 ms |
20208 KB |
Output is correct |
11 |
Correct |
812 ms |
20276 KB |
Output is correct |
12 |
Correct |
808 ms |
20228 KB |
Output is correct |
13 |
Correct |
813 ms |
20348 KB |
Output is correct |
14 |
Correct |
808 ms |
20272 KB |
Output is correct |
15 |
Correct |
817 ms |
20244 KB |
Output is correct |
16 |
Correct |
163 ms |
9132 KB |
Output is correct |
17 |
Correct |
172 ms |
9344 KB |
Output is correct |
18 |
Correct |
180 ms |
9228 KB |
Output is correct |
19 |
Correct |
172 ms |
9344 KB |
Output is correct |
20 |
Correct |
164 ms |
9164 KB |
Output is correct |
21 |
Correct |
236 ms |
20124 KB |
Output is correct |
22 |
Correct |
239 ms |
20340 KB |
Output is correct |
23 |
Correct |
211 ms |
19848 KB |
Output is correct |
24 |
Correct |
233 ms |
19984 KB |
Output is correct |
25 |
Correct |
231 ms |
20204 KB |
Output is correct |
26 |
Correct |
292 ms |
20292 KB |
Output is correct |
27 |
Correct |
300 ms |
20420 KB |
Output is correct |
28 |
Correct |
299 ms |
20308 KB |
Output is correct |
29 |
Correct |
304 ms |
20228 KB |
Output is correct |
30 |
Correct |
766 ms |
20264 KB |
Output is correct |
31 |
Correct |
747 ms |
20260 KB |
Output is correct |
32 |
Correct |
753 ms |
20284 KB |
Output is correct |
33 |
Correct |
743 ms |
20332 KB |
Output is correct |
34 |
Correct |
761 ms |
20204 KB |
Output is correct |
35 |
Correct |
735 ms |
20144 KB |
Output is correct |
36 |
Correct |
726 ms |
20164 KB |
Output is correct |
37 |
Correct |
754 ms |
20164 KB |
Output is correct |
38 |
Correct |
734 ms |
20080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
4172 KB |
Output is correct |
2 |
Correct |
18 ms |
4172 KB |
Output is correct |
3 |
Correct |
19 ms |
4108 KB |
Output is correct |
4 |
Correct |
19 ms |
4096 KB |
Output is correct |
5 |
Correct |
20 ms |
4188 KB |
Output is correct |
6 |
Correct |
20 ms |
4144 KB |
Output is correct |
7 |
Correct |
19 ms |
4172 KB |
Output is correct |
8 |
Correct |
21 ms |
4172 KB |
Output is correct |
9 |
Correct |
21 ms |
4172 KB |
Output is correct |
10 |
Correct |
21 ms |
4116 KB |
Output is correct |
11 |
Correct |
18 ms |
4076 KB |
Output is correct |
12 |
Correct |
18 ms |
4044 KB |
Output is correct |
13 |
Correct |
18 ms |
4156 KB |
Output is correct |
14 |
Correct |
18 ms |
4112 KB |
Output is correct |
15 |
Correct |
18 ms |
4164 KB |
Output is correct |
16 |
Correct |
10 ms |
2936 KB |
Output is correct |
17 |
Correct |
12 ms |
2912 KB |
Output is correct |
18 |
Correct |
13 ms |
2892 KB |
Output is correct |
19 |
Correct |
10 ms |
2964 KB |
Output is correct |
20 |
Correct |
9 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
802 ms |
20188 KB |
Output is correct |
2 |
Correct |
815 ms |
20228 KB |
Output is correct |
3 |
Correct |
805 ms |
20252 KB |
Output is correct |
4 |
Correct |
812 ms |
20184 KB |
Output is correct |
5 |
Correct |
812 ms |
20184 KB |
Output is correct |
6 |
Correct |
796 ms |
20280 KB |
Output is correct |
7 |
Correct |
827 ms |
20160 KB |
Output is correct |
8 |
Correct |
809 ms |
20252 KB |
Output is correct |
9 |
Correct |
819 ms |
20380 KB |
Output is correct |
10 |
Correct |
853 ms |
20208 KB |
Output is correct |
11 |
Correct |
812 ms |
20276 KB |
Output is correct |
12 |
Correct |
808 ms |
20228 KB |
Output is correct |
13 |
Correct |
813 ms |
20348 KB |
Output is correct |
14 |
Correct |
808 ms |
20272 KB |
Output is correct |
15 |
Correct |
817 ms |
20244 KB |
Output is correct |
16 |
Correct |
163 ms |
9132 KB |
Output is correct |
17 |
Correct |
172 ms |
9344 KB |
Output is correct |
18 |
Correct |
180 ms |
9228 KB |
Output is correct |
19 |
Correct |
172 ms |
9344 KB |
Output is correct |
20 |
Correct |
164 ms |
9164 KB |
Output is correct |
21 |
Correct |
236 ms |
20124 KB |
Output is correct |
22 |
Correct |
239 ms |
20340 KB |
Output is correct |
23 |
Correct |
211 ms |
19848 KB |
Output is correct |
24 |
Correct |
233 ms |
19984 KB |
Output is correct |
25 |
Correct |
231 ms |
20204 KB |
Output is correct |
26 |
Correct |
292 ms |
20292 KB |
Output is correct |
27 |
Correct |
300 ms |
20420 KB |
Output is correct |
28 |
Correct |
299 ms |
20308 KB |
Output is correct |
29 |
Correct |
304 ms |
20228 KB |
Output is correct |
30 |
Correct |
766 ms |
20264 KB |
Output is correct |
31 |
Correct |
747 ms |
20260 KB |
Output is correct |
32 |
Correct |
753 ms |
20284 KB |
Output is correct |
33 |
Correct |
743 ms |
20332 KB |
Output is correct |
34 |
Correct |
761 ms |
20204 KB |
Output is correct |
35 |
Correct |
735 ms |
20144 KB |
Output is correct |
36 |
Correct |
726 ms |
20164 KB |
Output is correct |
37 |
Correct |
754 ms |
20164 KB |
Output is correct |
38 |
Correct |
734 ms |
20080 KB |
Output is correct |
39 |
Correct |
18 ms |
4172 KB |
Output is correct |
40 |
Correct |
18 ms |
4172 KB |
Output is correct |
41 |
Correct |
19 ms |
4108 KB |
Output is correct |
42 |
Correct |
19 ms |
4096 KB |
Output is correct |
43 |
Correct |
20 ms |
4188 KB |
Output is correct |
44 |
Correct |
20 ms |
4144 KB |
Output is correct |
45 |
Correct |
19 ms |
4172 KB |
Output is correct |
46 |
Correct |
21 ms |
4172 KB |
Output is correct |
47 |
Correct |
21 ms |
4172 KB |
Output is correct |
48 |
Correct |
21 ms |
4116 KB |
Output is correct |
49 |
Correct |
18 ms |
4076 KB |
Output is correct |
50 |
Correct |
18 ms |
4044 KB |
Output is correct |
51 |
Correct |
18 ms |
4156 KB |
Output is correct |
52 |
Correct |
18 ms |
4112 KB |
Output is correct |
53 |
Correct |
18 ms |
4164 KB |
Output is correct |
54 |
Correct |
10 ms |
2936 KB |
Output is correct |
55 |
Correct |
12 ms |
2912 KB |
Output is correct |
56 |
Correct |
13 ms |
2892 KB |
Output is correct |
57 |
Correct |
10 ms |
2964 KB |
Output is correct |
58 |
Correct |
9 ms |
2896 KB |
Output is correct |
59 |
Correct |
1821 ms |
21460 KB |
Output is correct |
60 |
Correct |
1814 ms |
21396 KB |
Output is correct |
61 |
Correct |
1787 ms |
21248 KB |
Output is correct |
62 |
Correct |
1805 ms |
21412 KB |
Output is correct |
63 |
Correct |
1757 ms |
21300 KB |
Output is correct |
64 |
Correct |
1882 ms |
22332 KB |
Output is correct |
65 |
Correct |
1848 ms |
22124 KB |
Output is correct |
66 |
Correct |
1878 ms |
22272 KB |
Output is correct |
67 |
Correct |
1911 ms |
22232 KB |
Output is correct |
68 |
Correct |
1861 ms |
22152 KB |
Output is correct |
69 |
Correct |
1751 ms |
21292 KB |
Output is correct |
70 |
Correct |
1708 ms |
21504 KB |
Output is correct |
71 |
Correct |
1722 ms |
21332 KB |
Output is correct |
72 |
Correct |
1734 ms |
21456 KB |
Output is correct |
73 |
Correct |
1740 ms |
21340 KB |
Output is correct |
74 |
Correct |
222 ms |
9416 KB |
Output is correct |
75 |
Correct |
227 ms |
9460 KB |
Output is correct |
76 |
Correct |
218 ms |
9416 KB |
Output is correct |
77 |
Correct |
228 ms |
9456 KB |
Output is correct |
78 |
Correct |
228 ms |
9388 KB |
Output is correct |