HDU 5291 Candy Distribution
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
- $定义dp[i]表示两人之间相差i个糖果的情况数$
- 当前有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)\]
- $可以发现算出*dp[0]之后,算*dp[1] = *dp[0] + dp[1] + dp[3] - dp[0] - dp[-2]$
- $此时只要把[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的更多相关文章
- HDU 5291 Candy Distribution DP 差分 前缀和优化
Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...
- HDU 5291(Candy Distribution-差值dp)
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- AGC027 A - Candy Distribution Again
目录 题目链接 题解 代码 题目链接 AGC027 A - Candy Distribution Again 题解 贪心即可 代码 #include<cstdio> #include< ...
- Hdu 4465 Candy (快速排列组合+概率)
题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...
- HDU 4780 Candy Factory
Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...
- Candy Distribution
Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...
- hdu 1034 Candy Sharing Game
Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 4465 Candy(二次项概率)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465 参考博客:http://www.cnblogs.com/goagain/archive/2012 ...
- 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 ...
随机推荐
- bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏【dfs】
这个可以用异或高斯消元,但是我不会呀我用的暴搜 2的m次方枚举第一行的翻转情况,然后后面的就定了,因为对于一个j位置,如果i-1的j位置需要翻,那么一定要翻i的j,因为这是i-1的j最后翻的机会 按字 ...
- P3256 [JLOI2013]赛车
传送门 如果把速度看成斜率,起始位置看成截距,这就是一个水平可见直线了-- 不过这题里我实现方法借鉴了CQzhangyu大佬的,先按速度排序,然后维护一个单调栈,如果当前的人速度比栈顶大距离又比它远直 ...
- MongoDB的用户权限管理
1.创建用户并授权语法:db.createUser({user:"UserName",pwd:"Password",roles:[{role:"Rol ...
- HTML DOM getElementById() 方法
定义和用法 getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用. 语法 document.getElementById(id) 说明 HTML DOM 定义了多种查找元素 ...
- Enumerable.Union<TSource> 方法
功能:生成两个序列的并集(使用默认的相等比较器). 命名空间: System.Linq 程序集: System.Core.dll 备注:实现此方法时使用了延迟执行. 它直接返回一个对象,该对象存储了执 ...
- 第5章分布式系统模式 Singleton
上下文 在某些情况下,特定类型的数据需要提供给应用程序中的其他所有对象使用.在大多数情况下,这种类型的数据在系统中还是唯一的.例如,用户界面只能有一个所有应用程序必须访问的鼠标指针.同样,企业解决方案 ...
- [转] Redis在windows下安装过程
转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html) 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的win ...
- oracle 用sql语句管理数据库
基础sql语句 创建数据库 :create database database_name; 创建表:create table(字段名 字段类型 字段为空约束 ,字段名 字段类型 字段为空约束,,,, ...
- 360 你妈妈知道你跟Python存在兼容问题吗?
最近在用Python2.7.6版本开发的的过程中发现了一个问题 提示:UnicodeDecodeError:'ascii' codec can't decode bytes 0xb0 刚开始以为是编码 ...
- sp_Msforeachtable与sp_Msforeachdb详解
一.简要介绍: 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程.从mssql6.5开始,存放在SQL Server的MASTER数据 ...