# bzoj4332;vijos1955:JSOI2012 分零食

## 样例1

### 样例输入1

4 100
4
1
0
0

### 样例输出1

63

## 限制

#include<cstdio>
#define rec(x,y) rec[aa[y]+x]
using namespace std;

int n,m,a,b,c,p,l1,l2,l3,l0;
int rec[50000001],ans=0,aa[10001],f[255];
int main(){
scanf("%d%d%d%d%d%d",&m,&p,&n,&a,&b,&c);
register int i,j;
if (n>m) n=m;
aa[1]=0;
a%=p;b%=p;c%=p;
f[0]=c;
for (i=1;i<p;i++){
f[i]=f[i-1]+a*(2*i-1)+b;
while (f[i]>=p) f[i]-=p;
}
for (i=2;i<=m;i++) aa[i]=aa[i-1]+i-1;
for (i=1,j=1;i<=m;i++,j++,j-=j>=p?p:0)
if (i>p) rec(1,i)=rec(1,(j==0?p-1:j-1)+1);else rec(1,i)=f[j];
int k=((rec(1,2)-3*rec(1,1))+3*p)%p,y=(2*a-rec(1,2)+2*rec(1,1)+100*p)%p;
ans=rec(1,m);
for (i=2;i<=n;i++){
rec(i,i)=(rec(i-1,i-1)*f[1])%p;
int d=rec(i,i);
j=i+1;
d+=((k*rec(i-1,j-2))+(f[1]*rec(i-1,j-1)));
rec(i,j)=(2*rec(i,j-1)+d+p)%p;
j++;
l0=aa[j]+i;l1=aa[j-1]+i-1;l2=aa[j-2]+i-1;l3=aa[j-3]+i-1;
for (;j<=m;j++){
d+=((y*rec[l3])+(k*rec[l2])+(f[1]*rec[l1]));
rec[l0]=(2*rec[l1+1]-rec[l2+1]+d+p)%p;
l0+=j;l1+=j-1;l2+=j-2;l3+=j-3;
}
ans+=rec(i,m);
}
printf("%d\n",ans%p);
}

UPD at 2017.4.8

posted @ 2015-12-22 21:28  swm_sxt  阅读(433)  评论(0编辑  收藏  举报