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
  1. 2
  2. 1
  3. 2
  4. 2
  5. 1 2
 
Sample Output
  1. 2
  2. 4
  3. Hint
  4. 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.
  5.  
 
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] = 某段区间奇(偶)数位区间和 - 某段区间偶(奇)数位区间和

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<functional>
  6. #include<iostream>
  7. #include<cmath>
  8. #include<cctype>
  9. #include<ctime>
  10. using namespace std;
  11. #define For(i,n) for(int i=1;i<=n;i++)
  12. #define Fork(i,k,n) for(int i=k;i<=n;i++)
  13. #define Forkstep(i,k,s,n) for(int i=k;i<=n;i+=s)
  14. #define Rep(i,n) for(int i=0;i<n;i++)
  15. #define ForD(i,n) for(int i=n;i;i--)
  16. #define RepD(i,n) for(int i=n;i>=0;i--)
  17. #define Forp(x) for(int p=pre[x];p;p=next[p])
  18. #define Forpiter(x) for(int &p=iter[x];p;p=next[p])
  19. #define Lson (x<<1)
  20. #define Rson ((x<<1)+1)
  21. #define MEM(a) memset(a,0,sizeof(a));
  22. #define MEMI(a) memset(a,127,sizeof(a));
  23. #define MEMi(a) memset(a,128,sizeof(a));
  24. #define INF (2139062143)
  25. #define F (1000000007)
  26. #define MAXN (200+10)
  27. #define MAXSA (40000+10)
  28. typedef long long ll;
  29. ll mul(ll a,ll b){return (a*b)%F;}
  30. ll add(ll a,ll b){return (a+b+llabs(a+b)/F*F+F)%F;}
  31. //ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
  32. void upd(ll &a,ll b){a=(a+b+llabs(a+b)/F*F+F)%F;}
  33. void sub(ll &a,ll b){a=(a-b+llabs(a-b)/F*F+F)%F;}
  34. int n;
  35. const int M = 40500;
  36. int a[MAXN];
  37. ll dp[2][MAXSA*2+10000],sum[2][MAXSA*2+10000];
  38.  
  39. int main()
  40. {
  41. // freopen("hdu5291.in","r",stdin);
  42. // freopen(".out","w",stdout);
  43.  
  44. int T;cin>>T;
  45. while(T--) {
  46. MEM(dp) MEM(sum) MEM(a)
  47. cin>>n;
  48. For(i,n) scanf("%d",&a[i]);
  49.  
  50. int cur=0,s=0,tot=0;
  51.  
  52. dp[cur][M]=1;
  53. For(i,n)
  54. {
  55. if (a[i]==0) continue;
  56. int s=a[i];
  57. MEM(sum)
  58. Fork(k,1,M+tot+a[i]+a[i])
  59. {
  60. sum[k&1][k]=add(sum[ (k&1) ][k-1] ,dp[cur][k] );
  61. sum[(k&1)^1][k]=sum[(k&1)^1][k-1] ;
  62. }
  63.  
  64. tot+=a[i];
  65. cur^=1; MEM(dp[cur])
  66.  
  67. int t=M-tot;
  68. dp[cur][t]=1;
  69.  
  70. if (s%2==0)
  71. Fork(k,M-tot+1,M+tot)
  72. {
  73. dp[cur][k]=dp[cur][k-1];
  74. upd(dp[cur][k], sum[k&1][k+s]-sum[k&1][k-1] );
  75. sub(dp[cur][k], sum[(k&1)^1][k-1]-sum[(k&1)^1][k-1-s-1]);
  76. }
  77. else
  78. Fork(k,M-tot+1,M+tot)
  79. {
  80. dp[cur][k]=dp[cur][k-1];
  81. upd(dp[cur][k], sum[(k&1)^1][k+s]-sum[(k&1)^1][k-1] );
  82. sub(dp[cur][k], sum[k&1][k-1]-sum[k&1][k-1-s-1]);
  83. }
  84. }
  85.  
  86. printf("%lld\n",dp[cur][M]);
  87. }
  88.  
  89. return 0;
  90. }

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. idea导入ssm项目启动tomcat报错404

    用idea写ssm项目,基于之前一直在用spring boot  对于idea如何运行ssm花费了一番功夫 启动Tom act一直在报404 我搜了网上各种解决办法都不行,花费一天多的时间解决不了 就 ...

  2. POJ 1172 DFS

    (感谢wzc学长的幻灯片) 单组数据 注意从必经点能到标记过的点则此点不是分裂点. //By: Sirius_Ren #include <cstdio> #include <queu ...

  3. 使用MALTAB标定实践记录

    记录一下整个相机的标定矫正过程,希望能够帮助到刚学习标定的童鞋们- 首先下载matlab calibration toolbox,百度搜索第一条就是了(http://www.vision.caltec ...

  4. Java_Web之状态管理

    回顾及作业点评: (1)JSP如何处理客户端的请求? 使用response对象处理响应 (2)请描述转发与重定向有何区别? 转发是在服务器端发挥作用,通过forward方法将提交信息在多个页面间进行传 ...

  5. Linux命令小记

    以下说法都是基于普通用户的角度,如果是root,可能会有不同. (1)rm -r或-R选项:递归删除目录及其内容(子目录.文件) rm默认无法删除目录,如果删除空目录,可以使用-d选项.如果目录非空, ...

  6. react基础篇四

    列表 & Keys 渲染多个组件 你可以通过使用{}在JSX内构建一个元素集合 下面,我们使用Javascript中的map()方法遍历numbers数组.对数组中的每个元素返回<li& ...

  7. [Jxoi2012]奇怪的道路 题解(From luoguBlog)

    题面 状压好题 1<= n <= 30, 0 <= m <= 30, 1 <= K <= 8 这美妙的范围非状压莫属 理所当然地,0和1代表度的奇偶 dp[i][j ...

  8. wx 小程序开发---开发者工具使用

    1:右侧详情界面 合法域名 都要在需要在小程序平台 配置合法域名 这样你的小程序请求的网址 才能通. 1.2如果自己的域名没有配置https 可以勾选为 不校验合法域名即可 (小程序官方规定 网址必须 ...

  9. 明明引用了jquery,js还是报错

    先引jquery,不然加载上一个js的时候jquery还没有加载 <script src="js/jquery-1.9.1.js" type="text/javas ...

  10. Labview学习笔记(二)

    一.编程基础 LABVIEW程序成为虚拟.仪器程序,简称VI,一个最基本的VI包括三个部分:前面板.程序框图和图标/连接端口. 1.前面板 在前面板窗口中,可以添加输入控件和显示控件,同时,可以用快捷 ...