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 109+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 

pid=5413" target="_blank">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] -1 0 1
f[cur][j] 1 1 1

a[i]=2:

f[cur-1][j] -2 -1 0 1 2
f[cur][j] 1 1 2 1 1

a[i]=3:

f[cur-1][j] -3 -2 -1 0 1 2 3
f[cur][j] 1 1 2 2 2 1 1

我们奇偶分类讨论,非常easy发现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;
}

HDU 5291(Candy Distribution-差值dp)的更多相关文章

  1. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  2. HDU 5291 Candy Distribution

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. 【洛谷 P1651】 塔 (差值DP)

    题目链接 题意:\(n\)个木块放到两个塔里,每个木块可放可不放,使得两塔高度相同且高度最大,求最大高度. 这个差值\(DP\)的思维难度还是很大的,没想出来,我就打了一个\(dfs\)骗了好像\(2 ...

  4. P1282 多米诺骨牌 (差值DP+背包)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  5. HDU 5735 Born Slippy(拆值DP+位运算)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往 ...

  6. luogu- P1373 小a和uim之大逃离 DP 四维,其中一维记录差值

    P1373 小a和uim之大逃离: https://www.luogu.org/problemnew/show/P1373 题意: 在一个矩阵中,小A和小B轮流取数,小A可以从任意点先取,小B后取,最 ...

  7. hdu 5291 dp+优化 ****

    多校实在高能 题解链接 题意:有n中糖果,每种糖果有ai个.分给A,B两个人.两人的糖果要一样多,可以都是0,1......m个.同一种糖果没有区别. 问有几种分法. 定义dp[i]表示两人之间相差i ...

  8. HDU 3177 Crixalis's Equipment (贪心,差值)

    题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...

  9. 洛谷 P1373 小a和uim之大逃离 (差值型dp总结)

    这道题和多米诺骨牌那道题很像 ,都是涉及到差值的问题. 这道题是二维的,同时要取模. 这种题,因为当前的决策有后效性,会影响到差值,所以直接把 差值作为维度,然后计算答案的时候把差值为0的加起来就行了 ...

随机推荐

  1. 给.Net Core添加Docker文件支持和运行

    1.添加一个Dockerfile文件,将其移到解决方案文件夹,模板如下: FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build WORKDIR /ap ...

  2. 【区间DP】释放囚犯

    貌似和石子合并差不多 可能是我见的题太少了,所以都差不多 OK 算法分析 首先不难看出这是一道区间DP,那么,按照本蒟蒻的意思 区间DP==三个循环 for(int len=2;len<=n;l ...

  3. Matlab数组创建

    只用C语言,不用Matlab这种魔咒还是要打破的.Matlab是科学计算的常用工具,既然以前没用过,现在开始学吧...... 1.   向量的创建 1)直接输入: 行向量:a=[1,2,3,4,5] ...

  4. 教材配套PPT初稿

    1-10章初稿,基本完整.有些粗糙,后面可能会稍作调整. 附更新情况如下: 1.增加了第10章内容: 2.第5章增加了一些内容: 3.第3章内容部分更新: 4.增加了第8-9章内容. 订正:更新了第8 ...

  5. Vim入门基础知识集锦

        1. 简介 Vim(Vi[Improved])编辑器是功能强大的跨平台文本文件编辑工具,继承自Unix系统的Vi编辑器,支持Linux/Mac OS X/Windows系统,利用它可以建立.修 ...

  6. Java中StringTokenizer类的使用

    StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数. 1.构造函数 public StringTokenizer(String str) public Str ...

  7. LOJ #6041. 「雅礼集训 2017 Day7」事情的相似度 LCT+SAM+线段树

    Code: #include<bits/stdc++.h> #define maxn 200003 using namespace std; void setIO(string s) { ...

  8. Python 字符串常用方法 day2

    1.去空格和换行符: s = ' a bc ' print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格 print(s.rstrip())#rstrip() ...

  9. eas之根据bostype查找实体

    select * from  T_PF_BOSOBJECT;所有实体都有一个唯一的类型ID:BOSTYPEID,8位字符串,可切换到“源代码“页签,查找bostype标签里的值.这个ID的用途主要有: ...

  10. swift UITableViewCell 中的单选控制样式

    我昨天在网上找了一晚上的资料,但是大多都是OC得语法,swift资料实在是太少了,使得我这个刚入门swift的彩笔好不吃力,后面一直各种翻阅资料,终于让我找到了 visibleCells 这个方法,直 ...