# POJ 1837 dp

Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 12209 Accepted: 7650
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][k + C[i]*G[j]] += dp[i-1][k].当时推到这里，觉得可以空间压缩。就直接吧dp数组变成了一个一维数组。dp[i]直接继承dp[i]的所有情况。但WA了后发现有问题。由于第i层的所有状态都是由第i-1层推来到的。那么根据转移方程dp[i][k + C[i]*G[j]] += dp[i-1][k]有可能在第i-1层有的状态到i层转移后就没有了。所以不能直接继承i-1的状态。那么就无法空间压缩。由于力矩有负数，而数组下标不可能是负数。因此需要合适的对负数处理。这里用了一个大数字，负数下标的都放在越过这个大数字的位置。那么dp[i][0]就是平衡状态。当没有放任何东西的时候，应该就是平衡的，所以dp[0][0] = 1开始初始化错了，WA了。

``````#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

int G[30][30];
int C[30];
int W[30];
int dp[30][200000];

int main(){
//    freopen("data.in","r",stdin);
//    freopen("data.out","w",stdout);
int x,y;
scanf("%d%d",&y,&x);
int i,j,k;
for(i = 0;i<y;i++) scanf("%d",&C[i]);
for(i = 1;i<=x;i++) scanf("%d",&W[i]);
int sum1 = 0,sum2 = 0;
for(i = 1;i<=x;i++)
for(j = 0;j<y;j++){
G[i][j] = W[i]*C[j];
if(G[i][j]>=0) sum1+=G[i][j];
else sum2 += G[i][j]*(-1);
}
int Max = max(sum1,sum2);

int yy;
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for(i = 1;i<=x;i++){
for(j = 0;j<y;j++){
if(i == 0){
if(G[i][j]<0)
yy = Max - 1 + -1*G[i][j];
else
yy = G[i][j];
dp[i][yy] = 1;
}else{
for(k = 0;k<2*Max;k++){
if(dp[i-1][k]){
if(k<Max){
if(k+G[i][j]<0)
yy = Max - 1 + -1*(k+G[i][j]);
else
yy = k+G[i][j];
}else{
if(-1*(k - Max + 1) +G[i][j]<0)
yy = Max - 1 + -1*(-1*(k - Max + 1)+G[i][j]);
else
yy = -1*(k - Max + 1)+G[i][j];

}
dp[i][yy] += dp[i-1][k];
}
}
}
}
}

printf("%d",dp[x][0]);
return 0;
}``````
• 本文已收录于以下专栏：

## POJ1837:Balance(01背包)

Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is differe...
• libin56842
• 2014年03月12日 21:10
• 4376

## poj1837 - Balance

• wangjian8006
• 2012年05月20日 16:39
• 2075

## POJ 1837 Balance (DP)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9627   Accept...
• u010709592
• 2014年01月20日 14:39
• 807

## POJ 1837 DP

POJ1837-Balance 动态规划
• qq_31785871
• 2016年03月06日 11:43
• 291

## POJ 1837 dp

//11091923 c00h00g 1837 Accepted 1632K 32MS G++ 829B 2012-12-09 00:39:50 //dp[i][j]表示前i种物品得到某个值（重量*臂...
• chen895281773
• 2012年12月09日 00:43
• 209

## POJ 1837 Balance DP

POJ:http://poj.org/problem?id=1837 下午看完某萌妹子的比赛后回来继续刷题，太困了想不出来T T，小睡一觉还是想不出来QAQ，搜了题解做出来了，Orz大牛。 大意：...
• murmured
• 2013年11月14日 17:52
• 606

## POJ-DP题目列表【开启疯狗模式】

• verticallimit
• 2013年12月04日 11:05
• 2028

## POJ 1837 Balance（DP）

• sunrainchy
• 2013年09月27日 20:56
• 397

## poj - 1837 - Balance（dp）

• SCNU_Jiechao
• 2015年05月11日 16:45
• 404

## Poj 1837 Balance【DP】

• t51645
• 2017年02月23日 21:47
• 113

举报原因： 您举报文章：POJ 1837 dp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)