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. Python 35 进程间的通信(IPC机制)、生产者消费者模型

    一:进程间的通信(IPC):先进先出  管道:队列=管道+锁 from multiprocessing import Queue q=Queue(4) q.put(['first',],block=T ...

  2. 树莓派-基于raspistill实现定时拍照

    raspistill 经过上一篇<<树莓派-安装摄像头模块>>之后 raspistill 是树莓派基于摄像头拍照命令 比如我要截取一张宽1024px,高768px,旋转180度 ...

  3. angularJS之ng-bind与{{}}取值的区别

    1:{{ }} 是等页面加载完后,再取值. 2:ng-bind 它是在页面加载的时候,是不会显示{{name}}这种变量出来. 3:ng-bind 可以解决 ng 页面闪烁加载问题. 4:ng-bin ...

  4. Beta冲刺-星期三

    这个作业属于哪个课程  <课程的链接>            这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 剩余任务预估,分配 ...

  5. HTML5标签构成

    一个HTML5文件是由一些列的元素和标签组成的.元素是HTML5文件的重要组成部分,例如title(文件标题).img(图像)及table(表格)等.元素名不区分大小写,而HTML5用标签来规定元素的 ...

  6. XML、集合、JSP综合练习

    一.利用DOM解析XML文件得到信息:存入泛型集合中在JSP页面循环打印读取的信息 a)         编写XML文件:添加测试节点数据 b)         建立web项目:在JSP页面中使用DO ...

  7. ubuntu 14.04安装x11VNC

    环境:Ubuntu 14.04, 1)安装x11vnc: sudo apt-get install x11vnc 2)设置VNC的连接密码: x11vnc -storepasswd Enter VNC ...

  8. day35-1 类的三大特性---继承,以及类的派生

    目录 类的继承 继承的特性 类的派生 类的组合 类的继承 继承是为了拿到父类的所有东西 继承的特性 减少代码的冗余 Python中父类和子类的对应关系是多对多 使用__bases__方法获取对象继承的 ...

  9. [luogu1627 CQOI2009] 中位数 (乱搞)

    传送门 Solution 好水的题(ーー;) Code //By Menteur_Hxy #include <map> #include <queue> #include &l ...

  10. 单个图片获取-爬取网易"数读"信息数据(暴涨的房租,正在摧毁中国年轻人的生活)

    参考链接:https://www.makcyun.top/web_scraping_withpython3.html 该网页其实有16张图片,但是因为页面数据中某处多个空白,导致参考链接中的方式只有1 ...