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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
const int MOD = 1e9 + 7;
int add(int a, int b)
{
a+=b;
while(a>=MOD) a-=MOD;
return a;
}
int mult(int a, int b)
{
return (a*1LL*b)%MOD;
}
const int N = 501;
int dp[N+1][2*N+1];
vi x;
int l[2*N+1];
int a[N+1];
int b[N+1];
int inv[N*3+1];
ll modpow(ll a, ll b)
{
ll r=1;
while(b)
{
if(b&1) r=mult(r,a);
a=mult(a,a);
b>>=1;
}
return r;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
x.pb(a[i]);
x.pb(b[i]+1);
}
for(int i=1;i<=N*3;i++) inv[i]=modpow(i,MOD-2);
sort(x.begin(),x.end());
x.erase(unique(x.begin(),x.end()),x.end());
for(int i=0;i<x.size();i++)
{
if(i==0) l[i]=x[i];
else l[i]=x[i]-x[i-1];
}
for(int i=1;i<=n;i++)
{
a[i]=upper_bound(x.begin(),x.end(),a[i])-x.begin();
b[i]=upper_bound(x.begin(),x.end(),b[i])-x.begin();
}
for(int i=0;i<x.size();i++) dp[0][i]=1;
for(int i=1;i<=n;i++)
{
//cerr<<i<<' '<<a[i]<<' '<<b[i]<<'\n';
for(int j=a[i];j<=b[i];j++)
{
dp[i][j]=add(dp[i][j],mult(dp[i-1][j-1],l[j]));
int cur=l[j]-1; int cnt=1;
for(int k=i-1;k>0;k--)
{
if(a[k]<=j&&j<=b[k])
{
cur=mult(cur,l[j]+cnt-1);
cur=mult(cur,inv[++cnt]);
dp[i][j]=add(dp[i][j],mult(dp[k-1][j-1],cur));
}
}
}
dp[i][0]=1;
for(int j=1;j<x.size();j++)
{
dp[i][j]=add(dp[i][j],dp[i-1][j]);
dp[i][j]=add(dp[i][j],dp[i][j-1]);
dp[i][j]=add(dp[i][j],MOD-dp[i-1][j-1]);
}
}
int ans=dp[n][int(x.size())-1];
ans=add(ans,MOD-1);
cout<<ans<<'\n';
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:72:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<x.size();i++)
^
boat.cpp:82:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<x.size();i++) dp[0][i]=1;
^
boat.cpp:101:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=1;j<x.size();j++)
^
# | 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... |