Candy Distribution

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

 

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
2015 Multi-University Training Contest 1

解题:动态规划+规律优化
 
  1. $定义dp[i]表示两人之间相差i个糖果的情况数$
  2. 当前有a个第i种糖果,那么我们有\[dp[j] = dp[j]\times (a/2 + 1) + dp[j-1]\times((a-1)/2+1)+dp[j+1]\times((a-1)/2+1)+\cdots + dp[j-a]\times ((a-a)/2 + 1) + dp[j+a]\times ((a-a)/2 + 1)\]
  3. $可以发现算出*dp[0]之后,算*dp[1]  = *dp[0] + dp[1] + dp[3] - dp[0] - dp[-2]$
  4. $此时只要把[j+1,j+1+a]的奇数位置的dp值加起来 - [j-a,j]偶数位置的dp值 + *dp[0] = *dp[1]$
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
const LL mod = ;
LL dp[maxn],sum[][maxn];
int bound[maxn],n;
int main(){
int kase;
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
int S = ;
for(int i = ; i <= n; ++i){
scanf("%d",bound + i);
S += bound[i];
}
if(S&) S |= ;
memset(dp,,sizeof dp);
memset(sum,,sizeof sum);
dp[S] = ;
for(int i = ,t = (S<<); i <= n; ++i){
sum[][] = dp[];
sum[][] = ;
for(int j = ; j <= t; ++j){
sum[][j] = sum[][j-];
sum[][j] = sum[][j-];
sum[j&][j] += dp[j];
sum[j&][j] %= mod;
}
LL ret = ;
for(int j = ; j <= bound[i]; ++j){
ret += (LL)dp[j]*(((bound[i] - j)>>) + );
ret %= mod;
}
for(int j = ,p = (bound[i]&^); j <= t; ++j){
dp[j] = ret;
int x = max(,j - bound[i] - );
ret += (sum[p][j + bound[i] + ] - sum[p][j]);
p ^= ;
ret -= sum[p][j] - sum[p][x];
ret %= mod;
}
}
printf("%I64d\n",(dp[S] + mod)%mod);
}
return ;
}

参考这位大大的博客

$dp[j-1]\times((a-1)/2+1)$就是表示先取第i种的一个给自己,剩下的两人均分,

但是,我们不一定要全部分,所以那个1就是表示剩下的不分了,为什么乘以$(a-1)/2$,因为两个人可以都分1,都分2,都分$(a-1)/2$,共$(a-1)/2$种

HDU 5291 Candy Distribution的更多相关文章

  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-差值dp)

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

  3. AGC027 A - Candy Distribution Again

    目录 题目链接 题解 代码 题目链接 AGC027 A - Candy Distribution Again 题解 贪心即可 代码 #include<cstdio> #include< ...

  4. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  5. HDU 4780 Candy Factory

    Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  6. Candy Distribution

    Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...

  7. hdu 1034 Candy Sharing Game

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. hdu 4465 Candy(二次项概率)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465 参考博客:http://www.cnblogs.com/goagain/archive/2012 ...

  9. hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)

    简单概率题,可以直接由剩余n个递推到剩余0个.现在考虑剩余x个概率为(1-p)的candy时,概率为C(2 * n - x, x) * pow(p, n + 1)  *pow(1 - p, n - x ...

随机推荐

  1. E20171121-sl

    contrast  n. 对比,对照; 差异; 对照物,对立面; [摄] 反差;

  2. 自动生成 html5 小页面

    StringBuilder htmltext = new StringBuilder();            try            {                //var readP ...

  3. [Usaco2013 Nov]No Change

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  4. 树形DP UVA 1292 Strategic game

    题目传送门 /* 题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择 树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值 */ /***** ...

  5. 二分搜索 HDOJ 2289 Cup

    题目传送门 /* 二分搜索:枚举高度,计算体积与给出的比较. */ #include <cstdio> #include <algorithm> #include <cs ...

  6. Linux命令(007) -- systemctl

    systemctl命令是系统服务管理指令,它实际上是将service和chkconfig两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd ...

  7. maxItemsInObjectGraph解释

    maxItemsInObjectGraph:一个整数,指定要序列化或反序列化的最大项数,可以限制对象图中要序列化的项数.默认的就是65535,当客户端与WebService之间传递的是对象要序列化的个 ...

  8. T-SQL编程以及常用函数

    1.索引添加索引,设计界面,在任何一列前右键--索引/键--点击进入添加某一列为索引 2.视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序, ...

  9. Spring.Net学习笔记(6)-方法注入

    一.开发环境 系统:win10 编译器:VS2013 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Logging.dll 三.开发过程 1.项目结构 2.编写Mobile ...

  10. Angular JS中自定义标签 属性绑定的解释

    看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来. 看了一个stackoverflow答案,才算明白,在此贴出翻译,以供大家参考. .csharpcode, .csh ...