[BZOJ5215商店购物]

BZOJ5215

  • 背包很显然,再求一个组合数。复杂度是m4m^4m4
  • 前缀和优化一下,复杂度n3n^3n3。菜死了现场没想到优化

Coding

#include<bits/stdc++.h>
#define rint register int
#define ll long long
using namespace std;
const int N=2e7+10;
const int M=310;
const int mod=1e9+7;
int n,m,k,w[M];
int f[M][100010],sum[M][100010],jie[N*2],ans;
int read(){
	char ch=getchar();int num=0,f=1;
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){num=num*10+ch-'0';ch=getchar();}
	return num*f;
}
int power(int a,int b){
	int res=1%mod;
	for(;b;b>>=1){
		if(b&1) res=1LL*res*a%mod;
		a=1LL*a*a%mod;
	}
	return res;
}
int main(){
	//freopen("shopping.in","r",stdin);
	//freopen("shopping.out","w",stdout);
	n=read(),m=read(),k=read();
	jie[0]=1;
	for(rint i=1;i<=1e7;++i) jie[i]=1LL*jie[i-1]*i%mod;
	
	for(rint i=1;i<=m;++i) w[i]=read();
	f[0][0]=sum[0][0]=1;
	for(rint i=1;i<=90000;++i) sum[0][i]=1;
	for(rint i=1;i<=m;++i){
		for(rint j=0;j<=90000;++j){
			int l=j-w[i];
			if(l>0)
			f[i][j]=(1LL*f[i][j]+1LL*sum[i-1][j]-1LL*sum[i-1][l-1])%mod;
			else f[i][j]=(1LL*f[i][j]+1LL*sum[i-1][j])%mod;
			f[i][j]=(f[i][j]+mod)%mod;
			if(j>0)
			sum[i][j]=(1LL*sum[i][j-1]+1LL*f[i][j])%mod;
			else sum[i][j]=f[i][j];
		}
	}
	if(m==n)
	{
		cout<<f[m][k];
		return 0;
	}
	for(rint i=0;i<=90000;++i){
		if(f[m][i]){
			
			int c=1LL*jie[k-i+n-m-1]*power(jie[n-m-1],mod-2)%mod*power(jie[k-i],mod-2)%mod;
			ans=(ans+1LL*c*f[m][i]%mod)%mod;
		}
	}
	cout<<ans<<endl;
	return 0;
}
上一篇:线性基基本操作于模板


下一篇:洛谷P5282 【模板】快速阶乘算法(多项式多点求值+MTT)