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. hdu 1116 并查集判断欧拉回路通路

    判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...

  2. Python学习笔记(四):字符串的学习

    总结的内容: 1.字符串常用的方法 2.Python字符串格式化 3.Python字符串转义字 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...

  3. LogStash日志分析系统

    简介 通常日志管理是逐渐崩溃的——当日志对于人们最重要的时候,也就是出现问题的时候,这个渐进的过程就开始了.日志管理一般会经历一下3个阶段: 初级管理员将通过一些传统工具(如cat.tail.sed. ...

  4. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

  5. BTSync FREE vs BTSync PRO

    Although both BitTorrent Sync 2.0 FREE and PRO ensure high file transfer speed and ultimate security ...

  6. C#程序集系列10,强名称程序集

    当一个程序集的名称,版本,文化,Public Key都做了设置,就可以把这个程序集叫做"强名称程序集".强名称程序集可以防止被仿冒或篡改.本篇首先创建一个强名称程序集,接着模拟篡改 ...

  7. jQueryEasyUI创建菜单主页

    function addTab(title,path){ funcTab.tabs('add',{ title: title, fit:true, href:'${ctx}/login/forword ...

  8. UCN(User-Centric Networks,用户中心网络)

    UCN(User-Centric Networks,以用户为中心的网络)是下一代移动通信网络(5G)的发展方向,目前尚处于初级发展阶段.2016年11月,IEEE SDN研究组(聚焦研发SDN.NFV ...

  9. go语言基础之同级目录

    1.同级目录 分文件编程(多个源文件),必须放在src目录 同一个目录,包名必须一样 设置GOPATH环境变量 go env 查看go相关环境路径 GO PATH: 在windows系统中,添加go环 ...

  10. Android组件之自定义ContentProvider

    Android的数据存储有五种方式Shared Preferences.网络存储.文件存储.外储存储.SQLite,一般这些存储都只是在单独的一个应用程序之中达到一个数据的共享,有时候我们需要操作其他 ...