#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 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()};
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:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=0;i<pos.size()-1;i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2032 ms |
16212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2032 ms |
16212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
892 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2032 ms |
16212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |