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个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
随机推荐
- 【Explain】mysql之explain详解(分析索引的最佳使用)
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain 这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句 ...
- python之jsonpath的使用
import json import jsonpath import requests url="https://www.lagou.com/lbs/getAllCitySearchLabe ...
- 017 CPU冲高定位方法
1.通过top命令查看cpu占用高的进程ID; 2.通过top -Hp 进程ID 查看该进程下所有线程占用cpu的情况,拿出占用cpu最高的线程ID,换算成十六进制; 3.通过 jstack 进程ID ...
- centos-testlink安装使用手册
1.新建虚拟机设置 网卡必须选择ovirtmgmt模式 2.Centos6.3系统安装 说明: 1.CentOS 6.3系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.3-i386-bin ...
- PyQt:eg4
import sys from PyQt4 import QtCore from PyQt4 import QtGui class Form(QtGui.QDialog): def __init__( ...
- input只读属性 设置和移除 选择数字
设置只读属性 $('#stage').attr("readonly", "readonly"); 移除 只读属性 $("input").r ...
- curl 发送请求的时候报错
AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...
- 转 proc文件
/proc 是一个伪文件系统, 被用作内核数据结构的接口, 而不仅仅是解释说明/dev/kmem. /proc 里的大多数文件都是只读的, 但也可以通过写一些文件来改变内核变量. 下面对整个 /pro ...
- PostGreSQL数据库安装配置说明
windows 10 x64 pro 1703安装postgresql-9.6.3-2-windows-x64.exe数据库,步骤如下: 第一:下载数据库安装程序,下载地址为:https://www. ...
- mysql 导入数据到postgresql
创建PG的表脚本 DROP TABLE IF EXISTS "public"."t_resource_info"; CREATE TABLE "pub ...