Description

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

题意:用所有的砝码挂在天平的挂钩上,使天平平衡的挂法有多少种。
dp[i][j], 表示挂前i个砝码使其平衡系数(力矩和)为j的挂法, i∈[0,20], 当全部挂在最右端时, j = 15*20*25 = 7500, 最左端时-7500
为使其有意义,所以把j取值为[0, 7500*2], 7500时为平衡点
状态转移方程:dp[i][ j + w[i]*dis[k] ] += dp[i-1][j] k∈[1, c]
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxf = *;
int c, g;
int dis[];
int w[];
int dp[][maxf]; int main()
{
//freopen("1.txt", "r", stdin);
cin >> c >> g;
for (int i = ; i <= c; i++)
cin >> dis[i];
for (int i = ; i <= g; i++)
cin >> w[i]; memset(dp, , sizeof(dp));
dp[][] = ;
for (int i = ; i <= g; i++) {
for (int j = ; j <= maxf; j++) {
if (dp[i-][j]) {
for (int k = ; k <= c; k++) {
dp[i][j + w[i]*dis[k]] += dp[i-][j];
}
}
}
}
printf("%d\n", dp[g][]); return ;
}
 

[poj 1837] Balance dp的更多相关文章

  1. POJ 1837 -- Balance(DP)

     POJ 1837 -- Balance 转载:優YoU   http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...

  2. poj 1837 Balance(背包)

    题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. POJ 1837 Balance 01背包

    题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...

  4. POJ 1837 Balance 水题, DP 难度:0

    题目 http://poj.org/problem?id=1837 题意 单组数据,有一根杠杆,有R个钩子,其位置hi为整数且属于[-15,15],有C个重物,其质量wi为整数且属于[1,25],重物 ...

  5. POJ 1837 Balance(01背包变形, 枚举DP)

    Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...

  6. POJ 1837 Balance 【DP】

    题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...

  7. POJ 1837 Balance

    Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9240 Accepted: 5670 Description G ...

  8. poj 1837 Balance (0 1 背包)

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10326   Accepted: 6393 题意:给你n个挂 ...

  9. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

随机推荐

  1. Oracle使用hs odbc连接mssql2008

    1.创建odbc 2.在 product\11.2.0\dbhome_1\hs\admin\  下拷贝initdg4odbc,把名字改为initcrmsql(init+所建odbc的名称) HS_FD ...

  2. windows下安装配置nginx

    下载nginx-1.0.11.zip, 解压到到nginx目录下 D:\nginx\conf 修改conf下的nginx.conf文件, 默认是80端口,若该端口被占则可以修改 listen 8073 ...

  3. Sharepoint list webpart

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js&qu ...

  4. Atom插件及使用

    Atom比较好用的编辑工具之一,之前用过 HBuilder 和 Sublime Text ,个人感觉HBuider适合初级人们的人用. Atom好处之一是他的packages管理安装非常方便,你也可以 ...

  5. 源码安装postgres10 in centos6.4

    文件夹路径 程序目录 /usr/pgsql 数据目录/usr/local/pgdata 准备工作 获得源码 mkdir /opt/soft_bak cd /opt/soft_bak wget http ...

  6. 倍增模板orz

    #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #i ...

  7. Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N x M的矩阵,请你数一数其中有多少个3 x 3的子矩阵可以构成三阶幻方? 如果3 x 3的矩阵中每一行.每一列 ...

  8. 在Debug中使用断点调试程序

    我最近在学习汇编的程序,所以很多都需要动手写点代码去测试,如果是测试三五行代码的还比较简单,可以在debug中直接按T进行单步调试,但是到后来调试的代码越来越复杂,越来越长,如果再使用单步调试不知道要 ...

  9. 深入理解javascript中的立即执行函数

    这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包住业务代码,使用jquery时比较常见,需要的朋友可以 ...

  10. python2.7系统性能监控psutil模块

    系统环境:Centos7.4,系统自带python2.7.5 登录psutil官网,下载psutil的tar包:psutil-5.4.6.tar.gz,并使用命名sha256sum和官网的包进行核对, ...