Gigel has a strange “balance” and he wants to poise it. Actually, the device is different from any other ordinary balance.
It orders two arms of negligible weight and each arm’s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.
It is guaranteed that will exist at least one solution for each test case at the evaluation.
Input
The input has the following structure:
• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20);
• the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: ‘-’ for the left arm and ‘+’ for the right arm);
• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights’ values.
Output
The output contains the number M representing the number of possibilities to poise the balance.
Sample Input
2 4
-2 3
3 4 5 8
Sample Output
2

题目大意:

有一个天平,两臂质量不计。现在有C个挂钩,坐标为负在左边,坐标为正在右边。
有G个重物,要求把所有重物全部使用。问两边平衡的情况有多少种。数据保证肯定有至少一个解。

大致思路:

一道典型的01背包问题,可以用二维数组来表示状态。dp[i][j],i表示装了i件,j代表天平的平衡状态。由于下标不能为负,所以将所有数据变为正数。极限情况为:15*20*25
状态转移方程: dp[i][j-arms[l]*weight[i]]+=dp[i-1][j]

代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int MAX=15*20*25;
int dp[25][2*MAX];
int main()
{int C,G;while(cin>>C>>G){int arms[25],weight[25];for(int i=1;i<=C;++i)cin>>arms[i];for(int i=1;i<=G;++i)cin>>weight[i];dp[0][MAX]=1;//边界条件for(int i=1;i<=G;++i){for(int j=2*MAX;j>=1;--j){for(int l=1;l<=C;++l){dp[i][j-weight[i]*arms[l]]+=dp[i-1][j];}}}cout<<dp[G][MAX]<<endl;}return 0;
}

POJ 1837 Blance (01背包)相关推荐

  1. poj 1837 blance (01背包)

    题意 : 有一个特俗的天平 c 个指定的位置 g个给定重量的砝码 问用上所有砝码 使天平平衡的方案有几种 思路: dp[i][j+c[k]*g[i]]+=dp[i-1][j]; #include< ...

  2. poj 1837(blance)

    该题为一道简单的01背包,本题解用滚动队列(叫栈也可)稍微进行了些优化,由于该背包出现了负数,我们用10000作为表示平衡点 View Code 1 #include<iostream> ...

  3. DP专题 4 | 骨头收集爱好者 - POJ 1458( 0-1背包)

    背包问题是DP里面变化比较多的问题,可以参考网上的<背包9讲>,另外还是阅读<算竞入门>和<算竞进阶>,讲的最全的肯定是背包9讲,基本上把所有变形都讲了一遍,但是把 ...

  4. poj 2063 Investment(01背包变形)

    http://poj.org/gotoproblem?pid=2063 (1)上限 m 一直上升的 n 次01背包问题,比一般的01背包多了一重循环: (2)本题出现了各种错误:1)刚开始,没注意 m ...

  5. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

  6. 【POJ - 3211】Washing Clothes (dp,0-1背包中点问题)

    题干: Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautif ...

  7. POJ 3624 Charm Bracelet 0-1背包

    传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...

  8. 动态规划 背包问题小结 0-1背包(采药 九度第101题) 完全背包(Piggy-Bank POJ 1384) 多重背包(珍惜现在,感恩生活 九度第103题)

    本小结介绍0-1背包.完全背包以及多重背包问题 记忆要点: 0-1背包:二维数组情况下,顺序遍历体积或者倒序均可以                降维情况下需倒序遍历体积 完全背包:数组降维+顺序遍历 ...

  9. POJ 1293 - Duty Free Shop 01背包记录所选物品

    裸的01背包.dp[x]只要是bool型记录当前空间是否可用.. 而为了找到用了哪些物品..dp[x]设置为int型..进行记录.. Program: #include<iostream> ...

最新文章

  1. bootstrap解析-栅格系统
  2. 怎么主动发起话题_约会的时候,多聊这4个话题,女人才会失去抵抗力
  3. 工控随笔_09_西门子_S7-200 Smart与V20 USS通信USS_RPM_R利用轮询的方式通讯异常
  4. python eval 入门_Python学习笔记整理3之输入输出、python eval函数
  5. 项目管理怎样游刃有余
  6. Android 一s个相对完整的自动升级功能实现代码
  7. GSON 循环引用的对象转为 JSON 造成栈溢出
  8. linux 应用网络连接失败的原因,PuTTY网络错误:软件导致连接中止
  9. iview table增加一行减少一行_OA协会领导一行赴深圳市办公设备租赁行业协会参观交流...
  10. 十次方项目登陆问题 token令牌解析,claims获取不到userid,求大神帮忙看下代码解决问题
  11. Python经典前端框架:Django,第一天【Django环境搭建】
  12. JAVA:实现 web 打印功能(简单易懂)
  13. php架构师之路2017半年精选40篇
  14. 【相机标定与三维重建原理及实现】学习笔记2——标定的算法与方法
  15. CS231n笔记-CNN网络结构
  16. Hibernate对象状态
  17. PHP能源管理系统ems,EMS能源管理系统
  18. 【TensorFlow】人脸识别OpenFace、Face-recognition、Insightface和FaceNet源码运行
  19. 卡兹莫机器人报价_第三次超级机器人大战Z:时狱篇 部分隐藏机体获得条件
  20. 一些常用的编程语言和他们的特点

热门文章

  1. 苹果xsmax有高通基带吗_苹果短期无望突破5G基带技术壁垒 依赖高通将持续到2023年...
  2. cuda opencl opengl opencv
  3. uniapp 开发APP 安卓手机中 在使用nvue时候 border的dashed和radius可能会有冲突的解决办法
  4. 【基于FPGA的运动目标实时跟踪检测】
  5. 善事利器:用思维导图建设你的脑回路
  6. java中关于^的一些应用
  7. 如何关闭苹果手机自动扣费_如果你用的是苹果手机,那这个功能必须关闭,否则每月会自动扣费...
  8. opencv学习_13 (trajkovic 角点检测)
  9. HTML----CSS
  10. 86版高中英语第一册目录