# bzoj 3027 [Ceoi2004] Sweet —— 生成函数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=1e7+5,mod=2004;
int n,a,b,s[xn],m;
int rd()
{
int ret=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
return f?ret:-ret;
}
int upt(int x){while(x>=mod)x-=mod; while(x<0)x+=mod; return x;}
int main()
{
n=rd(); a=rd(); b=rd(); m=rd();
for(int i=0;i<=m;i++)s[i]=i+1;
for(int i=m+1;i<=b;i++)s[i]=s[m];//
for(int i=2;i<=n;i++)
{
m=rd();
//mx=min(mx+m[i],b);
for(int j=b;j>=0;j--)//
if(j-m-1>=0)s[j]=upt(s[j]-s[j-m-1]);
for(int j=1;j<=b;j++)s[j]=upt(s[j]+s[j-1]);
}
int ans=s[b];
if(a)ans=upt(ans-s[a-1]);
printf("%d\n",ans);
return 0;
}
TLE

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=15,xm=1e7+5,mod=2004;
int n,a,b,m[xn];
ll fac,p,ans;
int rd()
{
int ret=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
return f?ret:-ret;
}
ll upt(ll x){while(x>=mod)x-=mod; while(x<0)x+=mod; return x;}
ll C(int n,int m)
{
if(n<m)return 0;//!
ll ret=1;
for(int i=n-m+1;i<=n;i++)ret=(ret*i)%p;
return ret;
}
void dfs(int x,int s,int t)
{
if(x==n+1)
{
ans+=s*(C(n+b-t,n)-C(n+a-t-1,n)); ans=ans%p;
return;
}
dfs(x+1,s,t);
dfs(x+1,-s,t+m[x]+1);
}
int main()
{
n=rd(); a=rd(); b=rd(); fac=1;
for(int i=1;i<=n;i++)m[i]=rd(),fac*=i;
p=(ll)fac*mod;
dfs(1,1,0);
/*
for(int y=0;y<=b;y++)
{
ll tmp=upt(C(n+b-y,n)-C(n+a-y-1,n));
ans=(ans+tmp*f[y])%p;
}
*/
if(ans<0)ans+=p;
printf("%lld\n",ans/fac);
return 0;
}

posted @ 2018-11-27 20:50  Zinn  阅读(206)  评论(0编辑  收藏  举报