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. VMWare虚拟机下CentOS 配置网络实现远程连接,提供Web访问

        最近使用VMWARE虚拟机当中redhat操作系统,感觉直接使用很不方便,于是就决定配置下redhat网络,通过本机远程工具SecureCRT来连接redhat使用.     环境说明:本机操 ...

  2. Codeforces Round #295 (Div. 2)B - Two Buttons BFS

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. vue 组件开发 props 验证

    使用props 在Vue中父组件向子组件中传送数据是通过props实现的,一个简单的使用props的例子: <!DOCTYPE html> <html> <head> ...

  4. Python知识(7)--最小二乘求解

    这里展示利用python实现的最小二乘的直接求解方法.其求解原理,请参考:最小二乘法拟合非线性函数及其Matlab/Excel 实现 1.一般曲线拟合 代码如下: # -*- coding:utf-8 ...

  5. 如何使用git工具向github提交代码

    大致分为以下几个步骤 安装git环境,工具使用msysgit github上的账号 首先在github上点击头像旁边的加号 add new ,选择new Repository,自己创建一个名字,假设取 ...

  6. 计算机音频基础-PCM简介

    我们在音频处理的时候经常会接触到PCM数据:它是模拟音频信号经模数转换(A/D变换)直接形成的二进制序列,该文件没有附加的文件头和文件结束标志. 声音本身是模拟信号,而计算机只能识别数字信号,要在计算 ...

  7. Syncthing -- 开源的云储存和同步服务工具

    Syncthing  -- an open-source file synchronization client/server application Syncthing是一个开源的云存储和同步服务工 ...

  8. 怎样防止ddos攻击

    所有的主机平台都有抵御DoS的设置,总结一下,基本的有几种: 关闭不必要的服务 限制同时打开的Syn半连接数目 缩短Syn半连接的time out 时间 及时更新系统补丁 网络设置 网络设备可以从防火 ...

  9. jdbc连接rac的oracle数据库

    jdbc连接rac的oracle数据库需要配置所有racIP,如下: DB1 =(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(H ...

  10. Linux下安装Oracle的过程和涉及的知识点-系列6

    16.一路安装后.会提示下面界面.此时须要用root登录下面文件夹,然后运行这两个脚本. 至此,Oracle软件的安装就已经完毕了,接下来就能够创建数据库了. 17.选择自己定义数据库: 输入数据库名 ...