TLE95求助
查看原帖
TLE95求助
428358
Grisses楼主2024/9/20 22:16

拉插。

rt,最后一个包的前五个过不了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
int n,a[505],b[505],tot,t[1005],dp[2][1005][505],ed[2][1005],c[1005],inc[1005];
int fpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=(ll)res*a%mod;
		a=(ll)a*a%mod;
		b>>=1;
	}
	return res;
}
int q[505],p[505];
signed main()
{
	c[0]=1;
	for(int i=1;i<=1000;i++)c[i]=(ll)c[i-1]*i%mod;
	inc[1000]=fpow(c[1000],mod-2);
	for(int i=999;i>=0;i--)inc[i]=(ll)inc[i+1]*(i+1)%mod;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]),t[++tot]=a[i],t[++tot]=b[i];
	tot++;
	sort(t+1,t+tot+1);
	tot=unique(t+1,t+tot+1)-t-1;
//	cerr<<tot<<endl;
	t[tot+1]=mod;
//	for(int i=1;i<=tot;i++)cerr<<t[i]<<' ';cerr<<endl;
	for(int j=1;j<=tot;j++){
		ed[0][j]=1;
		for(int k=0;k<min(503,t[j+1]-t[j]);k++){
			dp[0][j][k]=1;
		}
	}
	for(int i=1;i<=n;i++){
		int op=i&1,oop=1-op;
		for(int j=1;j<=tot;j++){
		    int ll=min(503,t[j+1]-t[j]);
			for(int k=0;k<ll;k++){
				dp[op][j][k]=dp[oop][j][k]-(k?dp[oop][j][k-1]:(j==1?0:ed[oop][j-1]))+mod;
				if(dp[op][j][k]>=mod)dp[op][j][k]-=mod;
				if(a[i]<=t[j]+k&&t[j]+k<=b[i]){
//					cerr<<"=="<<i<<" "<<j<<" "<<k<<" "<<t[j]+k<<endl;
					if(k)dp[op][j][k]=(dp[op][j][k]+dp[oop][j][k-1]);
					else if(j!=1){
						dp[op][j][k]=(dp[op][j][k]+ed[oop][j-1]);
					}
					if(dp[op][j][k]>=mod)dp[op][j][k]-=mod;
				}
			}
			for(int k=0;k<ll;k++){
				dp[op][j][k]=(dp[op][j][k]+(k?dp[op][j][k-1]:(j==1?0:ed[op][j-1])));
				if(dp[op][j][k]>=mod)dp[op][j][k]-=mod;
//				cerr<<i<<' '<<t[j]+k<<' '<<dp[op][j][k]<<endl;
			}
			if(t[j+1]-t[j]>503){
				ed[op][j]=0;
				int pos=t[j+1]-t[j]-1;
				p[0]=(pos);
				q[502]=(pos-502);
				for(int i=1;i<=502;i++)p[i]=(ll)p[i-1]*(pos-i)%mod;
				for(int i=501;i>=0;i--)q[i]=(ll)q[i+1]*(pos-i)%mod;
				for(int x=0;x<503;x++){
					int cnt=dp[op][j][x];
					cnt=(ll)cnt*(x==0?1:p[x-1])%mod*(x==502?1:q[x+1])%mod*inc[x]%mod*(x%2?mod-1:1)%mod*inc[502-x]%mod;
					ed[op][j]=(ed[op][j]+cnt);
					if(ed[op][j]>=mod)ed[op][j]-=mod;
				}
			}
			else ed[op][j]=dp[op][j][t[j+1]-t[j]-1];
//			cerr<<ed[op][j]<<endl;
//			if(j==2)return 0;
		}
	}
	printf("%d",(dp[n&1][tot][0]-1+mod)%mod);
	return 0;
}
/*

5
6000333 6000333
1111222 1111222
1000000 1000000
4000 4000
1 1

*/
2024/9/20 22:16
加载中...