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. PLSQL Developer使用技巧整理

      Shortcut: =============================================================================== Edit/Und ...

  2. 机器学习(十四)— kMeans算法

    参考文献:https://www.jianshu.com/p/5314834f9f8e # -*- coding: utf-8 -*- """ Created on Mo ...

  3. ES提高数据压缩的设置——单字段,去掉source和all

    curl -XPUT 'http://localhost:9200/hec_test3' -d ' { "mappings": { "hec_type3": { ...

  4. cssParser

    //cssParser.h #include<iostream> using namespace std;struct MyAttribute{ MyAttribute*  next; s ...

  5. js的trim方法(转)

    写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...

  6. 机器学习 Logistic Regression

    Logistic Regression 之前我们讨论过回归问题,并且讨论了线性回归模型.现在我们来看看分类问题,分类问题与回归问题类似,只不过输出变量一个是离散的,一个是连续的.我们先关注二分类问题, ...

  7. cocos2d-x 中 xml 文件读取

    实现类 CXmlParse 啥也不说了  直接上硬货  believe yourself   一看就明白 CXmlParse.h #ifndef __C_XML_PARSE__ #define __C ...

  8. 打包AAC码流到FLV文件

    AAC编码后数据打包到FLV很简单. 1. FLV音频Tag格式                              字节位置    意义0x08,                       ...

  9. WC2010 BZOJ1758 重建计划_长链剖分

    题目大意: 求长度$\in [L,U]$的路径的最大边权和平均值. 题解 首先二分就不用说了,分数规划大家都懂. 这题有非常显然的点分治做法,但还是借着这个题学一波长链剖分. 其长链剖分本身也没啥,就 ...

  10. javascript中eval()函数使用方法

    本教程主要重介绍eval()函数的语法与使用方法,及在后面我还补充了eval()解析json数据的相关例子,希望文章能帮助到各位深入理解eval()使用方法吧.   前几天说到js中尽量不要使用eva ...