Candy Distribution

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 499    Accepted Submission(s): 189

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
 
Recommend
We have carefully selected several similar problems for you:  5416 5415 5414 

pid=5413" target="_blank">5413 5412 

 

令f[cur][j]为当前状态,表示截至第cur类糖,A比B多j个糖的方案

f[cur][j]=f[cur-1][j]*(a[i]/2)+f[cur-1][j±1]*(a[i]-1)/2+...+f[cur][j±a[i]]*1

从系数上看

a[i]=1:

f[cur-1][j] -1 0 1
f[cur][j] 1 1 1

a[i]=2:

f[cur-1][j] -2 -1 0 1 2
f[cur][j] 1 1 2 1 1

a[i]=3:

f[cur-1][j] -3 -2 -1 0 1 2 3
f[cur][j] 1 1 2 2 2 1 1

我们奇偶分类讨论,非常easy发现f[cur][j+1]-f[cur][j] = 某段区间奇(偶)数位区间和 - 某段区间偶(奇)数位区间和

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Forkstep(i,k,s,n) for(int i=k;i<=n;i+=s)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (200+10)
#define MAXSA (40000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b+llabs(a+b)/F*F+F)%F;}
//ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a+b+llabs(a+b)/F*F+F)%F;}
void sub(ll &a,ll b){a=(a-b+llabs(a-b)/F*F+F)%F;}
int n;
const int M = 40500;
int a[MAXN];
ll dp[2][MAXSA*2+10000],sum[2][MAXSA*2+10000]; int main()
{
// freopen("hdu5291.in","r",stdin);
// freopen(".out","w",stdout); int T;cin>>T;
while(T--) {
MEM(dp) MEM(sum) MEM(a)
cin>>n;
For(i,n) scanf("%d",&a[i]); int cur=0,s=0,tot=0; dp[cur][M]=1;
For(i,n)
{
if (a[i]==0) continue;
int s=a[i];
MEM(sum)
Fork(k,1,M+tot+a[i]+a[i])
{
sum[k&1][k]=add(sum[ (k&1) ][k-1] ,dp[cur][k] );
sum[(k&1)^1][k]=sum[(k&1)^1][k-1] ;
} tot+=a[i];
cur^=1; MEM(dp[cur]) int t=M-tot;
dp[cur][t]=1; if (s%2==0)
Fork(k,M-tot+1,M+tot)
{
dp[cur][k]=dp[cur][k-1];
upd(dp[cur][k], sum[k&1][k+s]-sum[k&1][k-1] );
sub(dp[cur][k], sum[(k&1)^1][k-1]-sum[(k&1)^1][k-1-s-1]);
}
else
Fork(k,M-tot+1,M+tot)
{
dp[cur][k]=dp[cur][k-1];
upd(dp[cur][k], sum[(k&1)^1][k+s]-sum[(k&1)^1][k-1] );
sub(dp[cur][k], sum[k&1][k-1]-sum[k&1][k-1-s-1]);
}
} printf("%lld\n",dp[cur][M]);
} return 0;
}

HDU 5291(Candy Distribution-差值dp)的更多相关文章

  1. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  2. HDU 5291 Candy Distribution

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

  3. 【洛谷 P1651】 塔 (差值DP)

    题目链接 题意:\(n\)个木块放到两个塔里,每个木块可放可不放,使得两塔高度相同且高度最大,求最大高度. 这个差值\(DP\)的思维难度还是很大的,没想出来,我就打了一个\(dfs\)骗了好像\(2 ...

  4. P1282 多米诺骨牌 (差值DP+背包)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  5. HDU 5735 Born Slippy(拆值DP+位运算)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往 ...

  6. luogu- P1373 小a和uim之大逃离 DP 四维,其中一维记录差值

    P1373 小a和uim之大逃离: https://www.luogu.org/problemnew/show/P1373 题意: 在一个矩阵中,小A和小B轮流取数,小A可以从任意点先取,小B后取,最 ...

  7. hdu 5291 dp+优化 ****

    多校实在高能 题解链接 题意:有n中糖果,每种糖果有ai个.分给A,B两个人.两人的糖果要一样多,可以都是0,1......m个.同一种糖果没有区别. 问有几种分法. 定义dp[i]表示两人之间相差i ...

  8. HDU 3177 Crixalis's Equipment (贪心,差值)

    题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...

  9. 洛谷 P1373 小a和uim之大逃离 (差值型dp总结)

    这道题和多米诺骨牌那道题很像 ,都是涉及到差值的问题. 这道题是二维的,同时要取模. 这种题,因为当前的决策有后效性,会影响到差值,所以直接把 差值作为维度,然后计算答案的时候把差值为0的加起来就行了 ...

随机推荐

  1. C# 设置百分比保留2位数

    double a=50; double b=100; 没有小数部分Label1.Text = (a   /   (a+b)).ToString("0%")   两位小数Label2 ...

  2. POJ 2230 DFS

    题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条 ...

  3. [转]ORACLE EXECUTE IMMEDIATE 小结

    转自:http://www.cnblogs.com/huanghai223/archive/2011/06/29/2093660.html   EXECUTE IMMEDIATE 代替了以前Oracl ...

  4. 2B课程笔记分享_StudyJams_2017

    课程2B-创建交互式应用(下) 概述 课程2B的内容主要包括:使用变量来更新欲显示在屏幕上的内容,为按钮添加事件响应(联系XML属性与Java方法)逻辑等. 后续的课程会逐步深入地讲解使用Java开发 ...

  5. Deutsch lernen (11)

    1. anwesend a. 出席的,在场的 ~ abwesend Es waren gegen 50 Leute anwesend. 2. gespannt a. (心情)急切的,急于想知道的:紧张 ...

  6. 【sqli-labs】 less37 POST- Bypass MYSQL_real_escape_string (POST型绕过MYSQL_real_escape_string的注入)

    POST版本的less36 uname=1&passwd=1%df' or 1#

  7. Arduino DS18B20温度检测

    一.实物图 注:电阻选取4.7k欧 二.事例代码 注:先下载Onewire库到arduino libraries目录下,然后就有例子 #include <OneWire.h> // One ...

  8. POJ_2115_扩展欧几里德

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23673   Accepted: 6540 Descr ...

  9. PHP实现文字写入图片功能

    /** * PHP实现文字写入图片 */class wordsOnImg { public $config = null; /** * @param $config 传入参数 * @param $co ...

  10. cent OS官网上下载老版本系统镜像的正确打开方式

    当时的情况是这样的: 客户需要给服务器安装cent OS 7.3操作系统,我打开官网https://www.centos.org/,点击“GET CENTOS”——>“Minimal ISO”, ...