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. Openwrt Uboot烧写

    Openwrt 烧uboot 需要慎重,一般买一个带不死uboot的路由器再折腾会比较安全,因为 openwrt firmware对uboot分区进行了保护,而且带有不死uboot的路由器可以通过we ...

  2. #Linux学习笔记# Linux文件的属性以及权限说明

    1. Linux文件的属性 关于Linux文件的属性的说明和设置请参考鸟哥Linux私房菜:Linux 的文件权限与目录配置 2. 目录和文件的权限意义 在Linux系统中,每个文件都有三种身份的权限 ...

  3. EntityFramework_MVC4中EF5 新手入门教程之一 ---1.创建实体框架数据模型

    Contoso University  Web 应用程序 你会在这些教程中构建的应用程序是一个简单的大学网站. 用户可以查看和更新学生. 课程和教师信息.这里有几个屏幕,您将创建. 这个网站的用户界面 ...

  4. NLP的两种工具的java版使用:复旦FudanNLP,中科院计算所ICTCLAS2013

    编程语言:java 三种工具的简要介绍: FudanNLP google project上的介绍是: FudanNLP主要是为中文自然语言处理而开发的工具包,也包含为实现这些任务的机器学习算法和数据集 ...

  5. 详解js中的闭包

    前言 在js中,闭包是一个很重要又相当不容易完全理解的要点,网上关于讲解闭包的文章非常多,但是并不是非常容易读懂,在这里以<javascript高级程序设计>里面的理论为基础.用拆分的方式 ...

  6. WampServer集成环境安装与配置

    实习到了第三个礼拜了,原来我们小组是以开发php为主的,我们项目的服务器也是用php做的,因此我觉得很有必要学一下php的相关知识,首先当然是搭建环境了,写篇博客分享下经验. 目录: 一.软件下载 二 ...

  7. xml_MathML的基本知识点__这东西要自己实践最好

    1 : <mi> 一般的字符串 2: <mo> 操作字符串 <mo> ( </mo> <mo>∑</mo> 3:<mn&g ...

  8. 【HDU 5399】Too Simple

    题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...

  9. BZOJ-1878 HH的项链 树状数组+莫队(离线处理)

    1878: [SDOI2009]HH的项链 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 2701 Solved: 1355 [Submit][Statu ...

  10. TCP/IP详解 笔记八

    UDP协议 UDP是传输层协议,提供无连接不可靠的数据传输,其优点失效率高,确定确定是无序不可靠. 报文格式 UDP头部 TCP和UDP的端口号是独立的 UDP长度是指UDP数据报的总长度 UDP的校 ...