Balance
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10773   Accepted: 6685

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

Source

给出每一个钩子的位置。和砝码的重量,问将砝码所有挂上时,有几种平衡的挂法?
这里有一个天平平衡的概念,转化为天平的左右的差,假设差是0那么天平平衡,所以。dp[i][j]代表当挂第i个砝码时差为j的种类。

避免反复选择,所以要使用二维的。
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 8000
int cc[30] , gg[30] ;
int dp[30][maxn<<1] ;
int main()
{
int c , g , i , j , k , max1 = 0 , m = 0 ;
memset(dp,0,sizeof(dp));
dp[0][maxn] = 1 ;
scanf("%d %d", &c, &g);
for(i = 0 ; i < c ; i++)
{
scanf("%d", &cc[i]);
if( abs(cc[i]) > max1 )
max1 = abs(cc[i]) ;
}
for(i = 1 ; i <= g ; i++)
{
scanf("%d", &gg[i]);
m += max1*gg[i] ;
}
max1 = m ;
for(i = 1 ; i <= g ; i++)
{
for(j = 0 ; j < c ; j++)
{
if( cc[j] > 0 )
{
m = gg[i]*cc[j] ;
for(k = maxn + max1 ; k >= m ; k--)
dp[i][k] += dp[i-1][k-m] ;
}
else
{
m = gg[i]*cc[j] ;
for(k = m ; k <= maxn+max1 ; k++)
dp[i][k] += dp[i-1][k-m] ;
}
}
}
printf("%d\n", dp[g][maxn]);
return 0;
}

poj1837--Balance(dp:天平问题)的更多相关文章

  1. POJ1837 Balance(DP)

    POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...

  2. POJ1837 Balance[分组背包]

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 Descript ...

  3. HDU 5616 Jam's balance(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...

  4. [poj 1837] Balance dp

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

  5. poj1837 Balance

    Balance  POJ - 1837 题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 其中可以把天枰看做一个以x轴0点 ...

  6. POJ 1837 -- Balance(DP)

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

  7. Codeforces Beta Round #17 C. Balance DP

    C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...

  8. POJ1837 Balance 背包

    题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 将每个砝码看作一组,组内各个物品 ...

  9. HDU 1709 The Balance( DP )

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. 【做题】ECFinal2018 J - Philosophical … Balance——dp

    原文链接 https://www.cnblogs.com/cly-none/p/ECFINAL2018J.html 题意:给出一个长度为\(n\)的字符串\(s\),要求给\(s\)的每个后缀\(s[ ...

随机推荐

  1. bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增

    3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MB Description 在Bytemountains有N座山峰,每座山峰 ...

  2. OpenGL和GLSL版本更迭

    前言 最近才发现,自己写的glsl和教程的glsl版本对不上,一直以为是xcode不允许使用太高版本,只能使用OpenGL 2.0的版本,却不知使用glfw可以使用到最新的OpenGL版本.

  3. codevs 1063 合并果子 STL 优先队列

    1063 合并果子 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codevs.cn/problem/1063/ Description 在一 ...

  4. centos 6.5安装VMware tools

    系统描述:win7旗舰版64位系统+VMware Workstation10+CentOS6.5(win7系统上安装了VMware Workstation10虚拟化软件,在该虚拟化软件上安装了Cent ...

  5. EJB (Enterprise Java Bean) 理解

    做开发有段时间了,一直似懂非懂的. http://blog.csdn.net/jojo52013145/article/details/5783677

  6. USB Mass Storage Class – Bulk Only Transport - Error Handling

    6.4 Device Error Handling The device may not be able to fully satisfy the host's request. At the poi ...

  7. codeforces 558B Amr and The Large Array-yy

    题意:有一个数组.如今要削减它的尺寸.数组中同样元素的个数的最大值为数组的魅力值,要求削减后魅力值不能降低,同一时候要尽可能的把尺寸减到最小 分析:水题,主要是不要想复杂了.还有就是沉下心来做 代码: ...

  8. NGINX + TOMCAT7 + MEMCACHED 实现SESSION 共享

    原文地址: http://my.oschina.net/u/1791256/blog/283064 TOMCAT7.0+ NGINX + MEMCACHED + memcached-session-m ...

  9. 【docker】docker部署spring boot项目在服务器上

    IDE:idea 工具:docker spring boot:2.0.1 ======================================== 简单记录一下流程,以供参考: 第一步:首先得 ...

  10. Oracle rac架构和原理

        Oracle RAC Oracle Real Application Cluster (RAC,实时应用集群)用来在集群环境下实现多机共享数据库,以保证应用的高可用性:同时可以自动实现并行处理 ...