博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5291 Candy Distribution DP 差分 前缀和优化
阅读量:6905 次
发布时间:2019-06-27

本文共 2267 字,大约阅读时间需要 7 分钟。

Candy Distribution

题目连接:

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
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<
<

转载地址:http://tdldl.baihongyu.com/

你可能感兴趣的文章
服务器硬件监控之Check_openmanage
查看>>
获取免费Windows Store开发者账户方法
查看>>
程序员杂记系列
查看>>
参加“北向峰会”后对SOC之感言
查看>>
ASP.NET vNext MVC 6 电商网站开发实战
查看>>
马化腾IT领袖峰会力推,微信小程序即将迎来爆发拐点
查看>>
javascript js 判断页面是否加载完成
查看>>
Ural_1494. Monobilliards(栈)
查看>>
IBM_WebSpwhere_Portal WIN7安不上解决
查看>>
基于ArcGIS10.0和Oracle10g的空间数据管理平台十六(C#开发)-空间数据编辑(上)...
查看>>
Xml匹配为对象集合(两种不同的方式)
查看>>
sql server join
查看>>
翻译:Contoso 大学 - 6 – 更新关联数据
查看>>
无线AP不能连接太多设备
查看>>
LINQ - Restriction Operators
查看>>
Install RRDTool on Red Hat Enterprise Linux
查看>>
string 是值类型,还是引用类型(.net)
查看>>
group by的测试
查看>>
ASP.NET 学习笔记_04 Session、http、web开发原则、xss漏洞
查看>>
一个distinct问题引发的思考
查看>>