Submission #970794

#TimeUsernameProblemLanguageResultExecution timeMemory
970794vjudge1Boat (APIO16_boat)C++14
100 / 100
705 ms4696 KiB
#include<bits/stdc++.h> using namespace std; const int N=510, mod=1e9+7; int fact[N*2], invfact[N*2], inv[N*2]; int binpow(int x, int y){ int t=1; while (y){ if (y&1) t=1ll*t*x%mod; x=1ll*x*x%mod; y>>=1; } return t; } void precalc(){ fact[0]=1; for (int i=1; i<N*2; ++i) fact[i]=1ll*fact[i-1]*i%mod; invfact[N*2-1]=binpow(fact[N*2-1], mod-2); for (int i=N*2-2; i>=0; --i) invfact[i]=1ll*invfact[i+1]*(i+1)%mod; for (int i=1; i<N*2; ++i) inv[i]=1ll*invfact[i]*fact[i-1]%mod; } int C(int n, int k){ if (n<0 || k<0 || n<k) return 0; return 1ll*fact[n]*invfact[k]%mod*invfact[n-k]%mod; } int f[N*2][N], g[N*2][N], sf[N*2], sg[N*2], n, a[N], b[N]; vector<int> vals; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); precalc(); vals.push_back(-1); cin >> n; for (int i=1; i<=n; ++i) cin >> a[i] >> b[i], vals.push_back(a[i]), vals.push_back(++b[i]); sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin()); for (int i=1; i<=n; ++i){ a[i]=lower_bound(vals.begin(), vals.end(), a[i])-vals.begin(); b[i]=lower_bound(vals.begin(), vals.end(), b[i])-vals.begin(); } f[0][0]=1; for (int i=0; i<(int)vals.size(); ++i) sf[i]=1; for (int i=1; i<=n; ++i){ for (int j=a[i]; j<b[i]; ++j){ int len=vals[j+1]-vals[j]; for (int k=1; k<=i && k<=len; ++k){ g[j][k]=(g[j][k]+1ll*(k==1?sf[j-1]:f[j][k-1])*(len-k+1)%mod*inv[k])%mod; } } for (int j=0; j<(int)vals.size(); ++j) for (int k=0; k<=i; ++k){ g[j][k]=(g[j][k]+f[j][k])%mod; sg[j]=(sg[j]+g[j][k])%mod; } for (int j=1; j<(int)vals.size(); ++j) sg[j]=(sg[j]+sg[j-1])%mod; memcpy(f, g, sizeof f); memcpy(sf, sg, sizeof sf); memset(g, 0, sizeof g); memset(sg, 0, sizeof sg); } cout << (sf[(int)vals.size()-1]+mod-1)%mod << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...