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. 洛谷 P4180 【模板】严格次小生成树[BJWC2010]【次小生成树】

    严格次小生成树模板 算法流程: 先用克鲁斯卡尔求最小生成树,然后给这个最小生成树树剖一下,维护边权转点权,维护最大值和严格次大值. 然后枚举没有被选入最小生成树的边,在最小生成树上查一下这条边的两端点 ...

  2. Linux环境下卸载、安装及配置MySQL5.1

    Linux环境下卸载原有MySQL5.1数据库,并重新安装MySQL数据库的示例记录. 一.卸载MySQL 查看主机中是否安装了MySQL数据库: [root@RD-viPORTAL- ~]# rpm ...

  3. java 键盘输入多种方法

    转! 分类: java学习2012-11-04 09:58 8427人阅读 评论(1) 收藏 举报 一.java不像C中拥有scanf这样功能强大的函数,大多是通过定义输入输出流对象.常用的类有Buf ...

  4. 项目需求会__前端er定位的思考~

    一.页面展示-----针对前端部分:后台的东西(功能.样式)不考虑! 二.动态效果------能不能实现! 三.接口数据------怎么传数据! 四.兼容性--------兼容到哪个版本浏览器! 五. ...

  5. mysql的简单优化【简单易学】

    1.选取最适用的字段属性: 表字段尽量设小,不要给数据库增加没必要的空间:如:值为'01'.'02',给char(2)即可: 2.使用连接(JOIN)来代替子查询(Sub-Queries): 使用jo ...

  6. swiper3初始化/swiper-init/用data实例化swiper/data-swiper

    Framework7直接用data属性实例化swiper用起来很爽,刚好最近又用到swiper插件,自己写一个 HTML <div class="swiper-container sw ...

  7. 百度人脸识别AI实践.doc

    0, 前言 百度开放了很多AI能力,其中人脸识别就是其中之一. 本文对百度人脸识别AI进行实践检验,看看其使用效果如何. 鉴于是最为基础的实践,基本都是在其接口范例代码修改而来. 百度人脸识别AI网站 ...

  8. JNDI链接SQLServer数据库步骤

    1.配置context.xml文件 在我们的WebRoot目录下,就是和WEB-INF同级的目录下,新建一个META-INF的目录(假如不存在),在该目录下创建一个context.xml文件,并且在c ...

  9. dubbo之服务降级

    向注册中心写入动态配置覆盖规则:(通过由监控中心或治理中心的页面完成) RegistryFactory registryFactory = ExtensionLoader.getExtensionLo ...

  10. 运用反射时报错java.lang.NoSuchMethodException,以解决,记录一下

    问题:想调用service类中的私有方法时, Method target=clz.getMethod("say", String.class);用Class的getMethod报错 ...