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

递推,注意到砝码一边的最大重量不超过7500,讲7500设为新的0点,枚举所有物品选择和节点选择,注意剪枝。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = * ; #define INF 0x3f3f3f3f //0 1 背包
/*
dp[i][MAXN]
*/
int dp[][MAXN];
int x[], w[];
int solve(int c, int g)
{
for (int i = ; i <= g; i++)
{
for (int v = ; v < MAXN; v++)
{
if (dp[i - ][v])
{
for (int j = ; j <= c; j++)
{
dp[i][v + w[i] * x[j]] += dp[i - ][v];
}
}
}
}
return dp[g][];
}
int main()
{
int c, g;
scanf("%d%d", &c, &g);
memset(dp, , sizeof(dp));
for (int i = ; i <= c; i++)
scanf("%d", &x[i]);
for (int j = ; j <= g; j++)
scanf("%d", &w[j]);
/*for (int i = 0; i < c; i++)
{
for (int j = 0; j < g; j++)
{
dp[0][x[i] * w[j] + 7500]++;
}
}*/
dp[][] = ;
printf("%d\n", solve(c, g));
return ;
}

Balance POJ - 1837 地推的更多相关文章

  1. Balance POJ - 1837

    Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...

  2. POJ 1837 -- Balance(DP)

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

  3. poj 1837 Balance(背包)

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

  4. POJ 1837 Balance 01背包

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

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

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

  6. POJ 1837 Balance

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

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

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

  8. [poj 1837] Balance dp

    Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...

  9. poj 1837 Balance (0 1 背包)

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

随机推荐

  1. Android开发学习——android与服务器端数据交互

    1.首先搭建服务器端. 使用MyEclipse开发工具 public class MyServlet extends HttpServlet { @Override protected void do ...

  2. canvas 保存状态

    1.保存和恢复绘图状态: 在绘制图形时,难免会重复使用某个样式,甚至有时会在不同颜色之间来回切换. 那么为了减少代码冗余,我们可以调用画布中的save()方法,来帮我们 保存一些样式和属性,这样我们就 ...

  3. 【开源】基于EF6+MVC5+API2+Easyui1.4.5+Easyui管理模板开发的管理系统

    经过近一步完善调整,现将本系统源码正式开放,定名为:EasyuiAdminFramework,另外EasyuiAdminTemplate及EasyuiFlatTheme也一并开源 项目主页:http: ...

  4. hihocoder offer收割编程练习赛12 C 矩形分割

    思路: 模拟,深搜. 实现: #include <iostream> #include <cstdio> #include <string> using names ...

  5. [POJ1509]Glass Beads 后缀自动机 最小循环串

    题目链接:http://poj.org/problem?id=1509 题目意思就是求循环字符串的最小表示. 我们用字符串S+S建立SAM,然后从root开始走n步,每次尽量选最小的. 由于 SAM ...

  6. Struts2------拦截器和标签库和注解开发

    一.解析Struts2源码中拦截器的执行 客户端请求Action,执行前端控制器,在前端控制器内部创建了Action的代理类,调用代理类的execute方法,在execute方法内部执行ActionI ...

  7. .NET 几种数据绑定控件的区别

    GridView 控件 GridView 控件以表的形式显示数据,并提供对列进行排序.分页.翻阅数据以及编辑或删除单个记录的功能. 特征:一行一条记录,就像新闻列表一样:带分页功能. DataList ...

  8. R in action读书笔记(21)第十六章 高级图形进阶(上)

    16.1 R 中的四种图形系统 基础图形函数可自动调用,而grid和lattice函数的调用必须要加载相应的包(如library(lattice)).要调用ggplot2函数需下载并安装该包(inst ...

  9. Farseer.net轻量级开源框架说明及链接索引

    Farseer.net是什么? 基于.net framework 4 开发的一系列解决方案. 完全开源在GitHub中托管.并发布到NuGet中. Farseer.Net由最初的关系数据库ORM框架后 ...

  10. webpack3整理(第二节/满三节)

    消除未使用的CSS:安装PurifyCSS-webpack插件 cnpm i purifycss-webpack purify-css -D const glob = require('glob'); ...