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 差分 前缀和优化的更多相关文章

  1. HDU 5291 Candy Distribution

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

  2. HDU 5291(Candy Distribution-差值dp)

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

  3. 算法技巧讲解》关于对于递推形DP的前缀和优化

    这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...

  4. BZOJ 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛:dp【前缀和优化】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3398 题意: 约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡 ...

  5. BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...

  6. 洛谷 P3258 [JLOI2014]松鼠的新家 树链剖分+差分前缀和优化

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 说明 思路 AC代码 优化 优化后AC代码 总结 题面 题目链接 P3258 [JLOI2 ...

  7. BZOJ 1600 [Usaco2008 Oct]建造栅栏:dp【前缀和优化】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1600 题意: 给你一个长度为n的木板,让你把这个木板切割成四段(长度为整数),并且要求这四 ...

  8. HDU 5791 Two (DP)

    Two 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 Description Alice gets two sequences A and ...

  9. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

随机推荐

  1. Big O

    评价一个计算机算法的效率时用到的方法我们称之为Big O(Order of [大约是]). 有序二分查找算法: 比如我们通过二分查找算法查找容器里的10个数据. 那么,我们需要查找一个数据时正常应该是 ...

  2. Linux轻量级自动运维工具-Ansible浅析【转】

    转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...

  3. python魔法函数__dict__和__getattr__的妙用

    python魔法函数__dict__和__getattr__的妙用 __dict__ __dict__是用来存储对象属性的一个字典,其键为属性名,值为属性的值. 既然__dict__是个字典那么我们就 ...

  4. Linux删除除了今天以外的文件

    [背景] 开发到日志记录功能时,每天都会产生当天的一个日志,久而久之就会产生累积,想要查看的时候,tab键无法自动补全,还要自己额外输入. 比较麻烦. [命令] 经过查找和实验,找到了以下的方法: 1 ...

  5. ansible报错AttributeError: module 'urllib.request' has no attribute 'HTTPSHandler'

    报错内容: TASK [activemq : extract activemq tarball] *************************************************** ...

  6. 制作自动化系统安装U盘

    1.挂载CentOS6.6镜像 [root@test88 ~]# mkdir /application/tools -p [root@test88 ~]# cd /application/tools/ ...

  7. GitBash、EGit、SourceTree三个Git管理工具对比

    Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...

  8. Homestead在windows7 下的搭建

    遇到的问题有 1.Homestead 的版本问题,教程git版本是 v5,最新是v7的,如果用最新,就要求vagrant的版本是 2.0的: 2.启动homestead后,出现 No input fi ...

  9. 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)

    问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器

  10. Tutorial: Reverse debugging with GDB 7 (转载)

    Tutorial: Reverse debugging with GDB 7 Tutorial: Reverse debugging with GDB 7 by Jay Conrod posted o ...