HDU 5291 Candy Distribution DP 差分 前缀和优化
Candy Distribution
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5291
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
题意
有n种糖果,每种糖果ai个,然后你要把糖果分给A,B两个人,问你使得两个人糖果个数都相同的方案有多少种。
题解:
最简单的dp:
dp[i][j]表示考虑到第i种糖果,现在A比B多j个,那么dp[i][x-y+j]+=dp[i-1][j],如果x+y<=ai的话
当然这种dp肯定会tle的。
然后优化一下,发现dp[i][j]=dp[i][j-k]*((a[i]-k)/2+1),表示你先扔了K个给A,然后剩下的水果AB平分。
这个东西我们发现,是一个分奇数和偶数的等差数列的东西。
1 1 2 2 3 3 4 4 ..... n n n n-1 n-1 ..... 3 3 2 2 1 1 系数是这样的。
显然可以分奇偶前缀和+差分+递推就可以O(1)得到每一个dp[i][j]了。
然后这道题就可以搞了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 8e4+5;
const int mod = 1e9+7;
int dp[maxn],sum[2][maxn],a[205],tot;
void solve()
{
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
tot=0;
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),tot+=a[i];
if(tot%2==1)tot++;
int M=tot*2;
dp[tot]=1;
for(int i=1;i<=n;i++)
{
sum[0][0]=dp[0];
sum[1][0]=0;
for(int j=1;j<=M;j++)
{
sum[0][j]=sum[0][j-1];
sum[1][j]=sum[1][j-1];
if(j%2==1)sum[1][j]+=dp[j];
else sum[0][j]+=dp[j];
sum[0][j]%=mod;
sum[1][j]%=mod;
}
long long res = 0;
for(int j=0;j<=a[i];j++)
res = (res + 1ll*dp[j]*((a[i]-j)/2+1))%mod;
int o = (a[i]%2)^1;
for(int j=0;j<=M;j++)
{
dp[j]=res;
res+=sum[o][(j+a[i]+1)];
res%=mod;
res-=sum[o][j];
res%=mod;
o^=1;
res-=sum[o][j];
res%=mod;
res+=sum[o][max((j-a[i]-1),0)];
res%=mod;
}
}
dp[tot]=dp[tot]%mod;
dp[tot]=(dp[tot]+mod)%mod;
cout<<dp[tot]<<endl;
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}
HDU 5291 Candy Distribution DP 差分 前缀和优化的更多相关文章
- HDU 5291 Candy Distribution
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5291(Candy Distribution-差值dp)
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 算法技巧讲解》关于对于递推形DP的前缀和优化
这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...
- BZOJ 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛:dp【前缀和优化】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3398 题意: 约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡 ...
- BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...
- 洛谷 P3258 [JLOI2014]松鼠的新家 树链剖分+差分前缀和优化
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 说明 思路 AC代码 优化 优化后AC代码 总结 题面 题目链接 P3258 [JLOI2 ...
- BZOJ 1600 [Usaco2008 Oct]建造栅栏:dp【前缀和优化】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1600 题意: 给你一个长度为n的木板,让你把这个木板切割成四段(长度为整数),并且要求这四 ...
- HDU 5791 Two (DP)
Two 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 Description Alice gets two sequences A and ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
随机推荐
- java中常见异常汇总(根据自己遇到的异常不定时更新)
1.java.lang.ArrayIndexOutOfBoundsException:N(数组索引越界异常.如果访问数组元素时指定的索引值小于0,或者大于等于数组的长度,编译程序不会出现任何错误,但运 ...
- AssetBundle——外部加载资源Asset
几篇很不错的文章 AssetBundle创建到使用入门 全面理解Unity加载和内存管理 实用的创建AssetBundle的脚本 相关资源 相关的共享资源下载 本共享包括创建assetbund ...
- vsftpd.log内容的意义
vsftpd日志(xferlog格式)的含义 引用: Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a ...
- python之pandas&&DataFrame(二)
简单操作 Python-层次聚类-Hierarchical clustering >>> data = pd.Series(np.random.randn(10),index=[[' ...
- 洛谷P1789【Mc生存】插火把 题解
题目传送门 这道题目可以纯暴力: #include<bits/stdc++.h> //Minecraft 666 using namespace std; ][]; int n,m,k,a ...
- appium---【Mac】Appium-Doctor提示WARN:“ ios_webkit_debug_proxy cannot be found”解决方案
“ ios_webkit_debug_proxy cannot be found”报错截图如下: 解决方案: 打开terminal终端,分别输入执行结束,再次运行appium-doctor即可看到运行 ...
- 使用亚马逊云服务器EC2做深度学习(三)配置TensorFlow
这是<使用亚马逊云服务器EC2做深度学习>系列的第三篇文章. (一)申请竞价实例 (二)配置Jupyter Notebook服务器 (三)配置TensorFlow (四)配置好的系统 ...
- [BZOJ4942][Noi2017]整数 线段树+压位
用线段树来模拟加减法过程,维护连续一段中是否全为0/1. 因为数字很大,我们60位压一位来处理. #include<iostream> #include<cstring> #i ...
- [实战]MVC5+EF6+MySql企业网盘实战(26)——音乐列表
写在前面 本篇文章将实现,音乐列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网 ...
- webpack2.0构建vue2.0超详细精简版
原文地址:http://blog.csdn.net/dahuzix/article/details/55549387 npm init -y 初始化项目 安装各种依赖项 npm install --s ...