HDU 5291(Candy Distribution-差值dp)

Candy Distribution

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 499    Accepted Submission(s): 189


Problem Description
WY has n kind of candy, number 1-N, The i-th kind of candy has ai. WY would like to give some of the candy to his teammate Ecry and lasten. To be fair, he hopes that Ecry’s candies are as many as lasten's in the end. How many kinds of methods are there?
 

Input
The first line contains an integer T<=11 which is the number of test cases.
Then T cases follow. Each case contains two lines. The first line contains one integer n(1<=n<=200). The second line contains n integers ai(1<=ai<=200)
 

Output
For each test case, output a single integer (the number of ways that WY can distribute candies to his teammates, modulo 10 9+7 ) in a single line.
 

Sample Input
  
  
2 1 2 2 1 2
 

Sample Output
  
  
2 4
Hint
Sample: a total of 4, (1) Ecry and lasten are not assigned to the candy; (2) Ecry and lasten each to a second kind of candy; (3) Ecry points to one of the first kind of candy, lasten points to a second type of candy; (4) Ecry points to a second type of candy, lasten points to one of the first kind of candy.
 

Author
FZUACM
 

Source
 

Recommend
We have carefully selected several similar problems for you:   5416  5415  5414  5413  5412 
 

令f[cur][j]为当前状态,表示截至第cur类糖,A比B多j个糖的方案

f[cur][j]=f[cur-1][j]*(a[i]/2)+f[cur-1][j±1]*(a[i]-1)/2+...+f[cur][j±a[i]]*1

从系数上看

a[i]=1:

f[cur-1][j]-101
f[cur][j]111

a[i]=2:

f[cur-1][j]-2-1012
f[cur][j]11211

a[i]=3:

f[cur-1][j]-3-2-10123
f[cur][j]1122211

我们奇偶分类讨论,很容易发现f[cur][j+1]-f[cur][j] = 某段区间奇(偶)数位区间和 - 某段区间()数位区间和






#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Forkstep(i,k,s,n) for(int i=k;i<=n;i+=s)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (200+10)
#define MAXSA (40000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b+llabs(a+b)/F*F+F)%F;}
//ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a+b+llabs(a+b)/F*F+F)%F;}
void sub(ll &a,ll b){a=(a-b+llabs(a-b)/F*F+F)%F;}
int n;
const int M = 40500;
int a[MAXN];
ll dp[2][MAXSA*2+10000],sum[2][MAXSA*2+10000];

int main()
{
//	freopen("hdu5291.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int T;cin>>T;
	while(T--) {
		MEM(dp) MEM(sum) MEM(a)
		cin>>n;
		For(i,n) scanf("%d",&a[i]);
		
		int cur=0,s=0,tot=0;
		
		dp[cur][M]=1;
		For(i,n)
		{
			if (a[i]==0) continue;
			int s=a[i];
			MEM(sum)
			Fork(k,1,M+tot+a[i]+a[i])
			{
				sum[k&1][k]=add(sum[ (k&1) ][k-1] ,dp[cur][k] );
				sum[(k&1)^1][k]=sum[(k&1)^1][k-1] ; 
			} 
						
			
			
			tot+=a[i];
			cur^=1; MEM(dp[cur])
			
			int t=M-tot;
			dp[cur][t]=1;
			
			if (s%2==0)
				Fork(k,M-tot+1,M+tot)
					{
						dp[cur][k]=dp[cur][k-1];
						upd(dp[cur][k], sum[k&1][k+s]-sum[k&1][k-1] );
						sub(dp[cur][k], sum[(k&1)^1][k-1]-sum[(k&1)^1][k-1-s-1]);
					}
			else 
				Fork(k,M-tot+1,M+tot)
					{
						dp[cur][k]=dp[cur][k-1];
						upd(dp[cur][k], sum[(k&1)^1][k+s]-sum[(k&1)^1][k-1] );
						sub(dp[cur][k], sum[k&1][k-1]-sum[k&1][k-1-s-1]);
					}
		} 
		
		
		
		printf("%lld\n",dp[cur][M]);
	}	
	
	
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值