原创 2016年08月28日 23:48:02

Problem here


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.


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.


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





#include <iostream>
#include <memory.h>
#include <stdio.h>
using namespace std;

int cost[30], weight[30];
int dp[25][15001];
int n, m;

int main(){
    while(~scanf("%d %d", &n, &m)){
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
            scanf("%d", &cost[i]);
        for(int i = 1; i <= m; i++)
            scanf("%d", &weight[i]);

        dp[0][7500] = 1;
        for(int i = 1; i <= m; i++)
            for(int j = 0; j <= 15000; j++)
                    for(int k = 1; k <= n; k++)
                        dp[i][j+weight[i] * cost[k]] += dp[i-1][j];

        printf("%d", dp[m][7500]);
    return 0;
版权声明:本文为博主原创文章,並与http://cartonsbase.blogspot.com/同步,未经博主允许不得转载。 http://blog.csdn.net/LeongHouHeng/article/details/52349075


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

POJ 2142The Balance(扩展欧几里得)

The Balance Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measu...
  • wust_xhj
  • wust_xhj
  • 2015年07月17日 13:47
  • 1105

POJ 2142 The Balance 扩展欧几里得

题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为d的物品,要求a的数量x和b的数量y最小,以及x+y的值最小。 思路:是扩展欧几里得的应用。设ax + by = 1,求出x和...
  • wmn_wmn
  • wmn_wmn
  • 2012年07月30日 09:07
  • 2610

POJ2142 The Balance(扩展欧几里德)

The Balance Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5016   Accepted: 2197...
  • u013021513
  • u013021513
  • 2015年07月28日 10:10
  • 1361

POJ 1837 Balance(动态规划之背包问题)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11436   Accepted: 7130 ...
  • 2015年04月15日 20:12
  • 1571

POJ-1837-Balance -01背包

提示:动态规划,01背包 初看此题第一个冲动就是穷举。。。。不过再细想肯定行不通= =O(20^20)等着超时吧。。。 我也是看了前辈的意见才联想到01背包,用动态规划来解   题目大意: ...
  • RIPwangh
  • RIPwangh
  • 2015年08月21日 10:55
  • 276

POJ Balance (01背包)

POJ Balance (01背包)
  • MeJustOny
  • MeJustOny
  • 2017年04月09日 10:40
  • 228

笔记——负载均衡(Load Balance)

  • cjh095
  • cjh095
  • 2014年07月31日 14:17
  • 913

杭电 HDU ACM 1709 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • lsgqjh
  • lsgqjh
  • 2015年04月26日 11:51
  • 899

hadoop Balance 优化

优化拷贝带宽(带宽的设置是影响datanode,设置单个datanode的balance带宽上限): [-setBalancerBandwidth ] [@rm.tv.hadoop.sohuno.c...
  • chilianyi
  • chilianyi
  • 2016年03月21日 15:20
  • 1902