Submission #1106736

#TimeUsernameProblemLanguageResultExecution timeMemory
110673612345678Boat (APIO16_boat)C++17
100 / 100
765 ms18368 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int nx=505, mod=1e9+7; ll n, a[nx], b[nx], dp[nx][2*nx], qs[nx][2*nx], sz[2*nx], t, pv, ncr[nx][nx], choose[2*nx][nx], l[nx], r[nx], sm[2*nx][nx]; map<ll, ll> mp; ll invmod(ll a, ll x) { if (x==0) return 1; auto tmp=invmod(a, x/2); tmp=(tmp*tmp)%mod; if (x%2) return (tmp*a)%mod; else return tmp; } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n; for (int i=1; i<=n; i++) cin>>a[i]>>b[i], mp[--a[i]]=0, mp[b[i]]=0; for (auto &[x, y]:mp) y=t, sz[t]=x-pv, pv=x, t++; for (int i=1; i<=n; i++) l[i]=mp[a[i]], r[i]=mp[b[i]]; ncr[0][0]=1; for (int i=1; i<=n; i++) { for (int j=0; j<=i; j++) { ncr[i][j]=ncr[i-1][j]; if (j>0) ncr[i][j]=(ncr[i][j]+ncr[i-1][j-1])%mod; } } for (int i=1; i<t; i++) { ll tmp=1; choose[i][0]=tmp; for (int j=1; j<=min(sz[i], n); j++) tmp=(((tmp*(sz[i]-j+1))%mod)*invmod(j, mod-2))%mod, choose[i][j]=tmp; } for (int i=1; i<t; i++) for (int j=2; j<=n; j++) for (int x=0; x<=j-2; x++) sm[i][j]=(sm[i][j]+choose[i][x+2]*ncr[j-2][x])%mod; for (int i=1; i<=n; i++) { for (int j=1; j<t; j++) { if (l[i]<j&&j<=r[i]) { dp[i][j]=((qs[i-1][j-1]+1)*sz[j])%mod; ll cnt=1; for (int k=i-1; k>=1; k--) { if (l[k]<j&&j<=r[k]) { cnt++; if (cnt>=2) dp[i][j]=(dp[i][j]+sm[j][cnt]*(qs[k-1][j-1]+1))%mod; } } } //cout<<"dp "<<i<<' '<<j<<' '<<dp[i][j]<<'\n'; qs[i][j]=((dp[i][j]+qs[i-1][j]+qs[i][j-1]-qs[i-1][j-1])%mod+mod)%mod; } } cout<<qs[n][t-1]; } /* 2 1 3 1 3 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...