This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 507
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[MAXN][MAXN][2];
long long invfact[MAXN];
long long comb(int x,int g){
long long res=1,sz=s[g].to-s[g].from+1;
for(long long i=sz;i>=sz-x+1;i--){
res*=i; res%=mod;
}
res*=invfact[x]; res%=mod;
return res;
}
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));
}
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(int i=1;i<=n;i++){
invfact[i]=(invfact[i-1]*i)%mod;
}
for(int 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()};
dp[0][0][1]=dp[0][1][0]=dp[0][1][1]=1;
for(int i=1;i<=n;i++){
for(int f=1;f<=m;f++){
for(int t=0;t<2;t++){
dp[i][f][t]=dp[i-1][f][0];
if(t==1)dp[i][f][t]+=dp[i][f-1][1];
for(int k=i;k>=1 and i-k+1<=s[f].to-s[f].from+1;k--){
if(!in(s[f],k))break;
dp[i][f][t]+=(dp[k-1][f-1][1]*comb(i-k+1,f))%mod;
}
dp[i][f][t]%=mod;
}
}
}
cout<<dp[2][3][1]-1<<"\n";
return 0;
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<pos.size()-1;i++){
| ~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |