【洛谷 P3643】【APIO2016】 划艇(DP)
看到满脑子都想的赛艇
考虑离散化成一堆区间
设表示前个,当前在第个区间,在第个区间的有个的方案数
然后在跳到下一个区间的时候再算个份分配在第个区间的方案
由于是递增的,所以系数为
那么有
前缀和优化即可做到
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=505;
int f[N*2][N],s[N*2];
int C[N*2][N],len[N*2];
int a[N],b[N],c[N*2],iv[N];
int n,m;
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read();
iv[0]=iv[1]=1;
for(int i=2;i<=n;i++)iv[i]=mul(mod-mod/i,iv[mod%i]);
for(int i=1;i<=n;i++)a[i]=read(),b[i]=read(),c[++m]=a[i],c[++m]=b[i]+1;
sort(c+1,c+m+1),m=unique(c+1,c+m+1)-c-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(c+1,c+m+1,a[i])-c,b[i]=upper_bound(c+1,c+m+1,b[i])-c-1;
m--;
for(int i=1;i<=m;i++){
len[i]=c[i+1]-c[i];
C[i][0]=1,C[i][1]=len[i];
for(int j=2;j<=n;j++)C[i][j]=mul(C[i][j-1],mul(len[i]-j+1,iv[j]));
}
for(int i=1;i<=n;i++){
for(int j=a[i];j<=b[i];j++){
for(int k=i;k>=2;k--)Add(f[j][k],f[j][k-1]);
Add(f[j][1],s[j-1]+1);
}
memset(s,0,sizeof(s));
for(int j=1;j<=m;j++)
for(int k=1;k<=i;k++)Add(s[j],mul(f[j][k],C[j][k]));
for(int j=1;j<=m;j++)Add(s[j],s[j-1]);
}
int res=0;
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
Add(res,mul(f[j][k],C[j][k]));//,cout<<j<<" "<<k<<" "<<f[j][k]*C[j][k]<<'\n';
cout<<res<<'\n';
return 0;
}