Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9240 Accepted: 5670

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

Romania OI 2002

DP......

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int tianping[30],fama[30],dp[30][11000],C,G;
const int BASE=5000;

int main()
{
    while(scanf("%d%d",&C,&G)!=EOF)
    {
        for(int i=1;i<=C;i++)
            scanf("%d",&tianping);
        for(int i=1;i<=G;i++)
            scanf("%d",&fama);
        memset(dp,0,sizeof(dp));
        dp[0][BASE]=1;
        for(int i=1;i<=C;i++)
        {
            dp[1][BASE+tianping*fama[1]]++;
        }
        for(int i=2;i<=G;i++)
        {
            for(int j=1;j<=C;j++)
            {
                int VK=tianping[j]*fama;
                for(int k=-5000;k<=5000;k++)
                {
                    dp[k+BASE+VK]+=dp[i-1][k+BASE];
                }
            }
        }
        printf("%d\n",dp[G][BASE]);
    }
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 1837 Balance的更多相关文章

  1. POJ 1837 -- Balance(DP)

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

  2. poj 1837 Balance(背包)

    题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. POJ 1837 Balance 01背包

    题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...

  4. POJ 1837 Balance 水题, DP 难度:0

    题目 http://poj.org/problem?id=1837 题意 单组数据,有一根杠杆,有R个钩子,其位置hi为整数且属于[-15,15],有C个重物,其质量wi为整数且属于[1,25],重物 ...

  5. POJ 1837 Balance(01背包变形, 枚举DP)

    Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...

  6. [poj 1837] Balance dp

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

  7. poj 1837 Balance (0 1 背包)

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10326   Accepted: 6393 题意:给你n个挂 ...

  8. POJ 1837 Balance 【DP】

    题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...

  9. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

随机推荐

  1. 浅析java类加载器ClassLoader

    作为一枚java猿,了解类加载器是有必要的,无论是针对面试还是自我学习. 本文从JDK提供的ClassLoader.委托模型以及如何编写自定义的ClassLoader三方面对ClassLoader做一 ...

  2. 关于sql 的convert 格式设置

    CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...

  3. Git & Github 一页简明笔记

    由于小组工程需要使用git&github的版本控制来协作,但我对其使用并不熟悉,特此写篇一页的笔记放在手边,备随时查阅. 相信这种一页的简明笔记,对大家也是有帮助的.我的笔记总结自廖雪峰的Gi ...

  4. ASP.NET 系列:RBAC权限设计

    权限系统的组成通常包括RBAC模型.权限验证.权限管理以及界面访问控制.现有的一些权限系统分析通常存在以下问题: (1)没有权限的设计思路 认为所有系统都可以使用一套基于Table设计的权限系统.事实 ...

  5. LinuxMint下Apache Http源码安装过程

    1. 源码包下载 Apache Http安装要求必须安装APR.APR-Util.PCRE等包. Apache Http包下载地址:http://httpd.apache.org/download.c ...

  6. Linq之Lambda进阶

    目录 写在前面 系列文章 带有标准查询运算符的Lambda Lambda中类型推断 Lambda表达式中变量作用域 异步Lambda 总结 写在前面 上篇文章介绍了Lambda的基本概念以及匿名方法, ...

  7. oracle 简述

    1.数据库有很多的管理工具,Sqlplus是最好的管理工具. 2.sql语句是学习中最难的部分,如何编写出高效的sql 语句是我们的目标 3.oracle的日常最终要的工作就是备份,永远是备份,有数据 ...

  8. Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)

      一.之前抛异常是将json库改成了fastjson解决的,参见: http://www.cnblogs.com/gossip/p/5369670.html     异常信息:     二.解决步骤 ...

  9. ES6(ECMAScript 2015) 编码规范与详细注意要点

    本规范是基于JavaScript规范拟定的,只针对ES6相关内容进行约定 如变量命名,是否加分号等约定的请参考JavaScript规范 应注意目前的代码转换工具(如Babel,Traceur)不够完善 ...

  10. scanf的一些技巧

    一.scanf和gets 1.不同点: char string[50]; scanf("%s",string); //当遇到回车,空格和tab键会自动在字符串后面添加'\0',但是 ...