Balance

Time Limit: 1000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
/*
01背包 题意:C个钩码(2—20) G个物品(2—20) 钩码位置(-25—25) 物品重量(0—20) 物品都用上且天平平衡有多少种方案 dp[i][j]:挂前i个物品达到状态j 状态j的取值范围时-25*25*20——25*25*20 所以j取(-7500--7500) 防止出现负值 所以令j==15000 即j==7500时为平衡位置
想~~每次挂砝码都会影响天平的平衡 即状态j 影响因素是力臂=c[i]*w[k] (n,m影响它的取值)
挂前i个物品时状态是dp[i-1][j] 则挂第i个物品后状态变为dp[i][j+c[i]*w[k]]
假设dp[i-1][j]的值是num 那么 dp[i][j+c[i]*w[k]]也是num
即dp[i][j+c[i]*w[k]]+=dp[i-1][j] 前面状态影响后面的 */
#include <iostream>
#include <string.h>
#include <stdio.h> int dp[][]; ///前i个物品达到j的状态有的dp[][]种 int main()
{
int n,m; ///钩子个数 砝码个数
int c[]; ///钩子的位置
int w[]; ///砝码重量 scanf("%d%d",&n,&m); for(int i=;i<=n;i++)
scanf("%d",&c[i]);
for(int j=;j<=m;j++)
scanf("%d",&w[j]); memset(dp,,sizeof(dp));
dp[][]=; ///因为防止出现负数情况 所以dp[][1500]了 同时dp[][7500]是平衡状态 for(int i=;i<=m;i++)
{
for(int j=;j<=;j++)
{
for(int k=;k<=n;k++)
{
dp[i][j+c[k]*w[i]]+=dp[i-][j]; ///核心 在前面介绍
}
}
}
printf("%d\n",dp[m][]);
}

poj 1837 01背包的更多相关文章

  1. poj 2184 01背包变形【背包dp】

    POJ 2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14657   Accepte ...

  2. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

  3. POJ 3628 01背包 OR 状压

    思路: 1.01背包 先找到所有奶牛身高和与B的差. 然后做一次01背包即可 01背包的容积和价格就是奶牛们身高. 最后差值一减输出结果就大功告成啦! 2. 搜索 这思路很明了吧... 搜索的确可以过 ...

  4. poj 1837 Balance(背包)

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

  5. Proud Merchants(POJ 3466 01背包+排序)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  6. POJ 3624 01背包

    初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出. //在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个 ...

  7. POJ之01背包系列

    poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...

  8. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  9. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 判断某个字符串里面是否包含caoyang 这个字符串?

    $string = 'Lorem ipsum dolor sit amet'; $preg = '/caoyang/'; $status = preg_match($preg, $string,$ma ...

  2. MySQL单行注释和多行释

    单行注释:# 或者 -- 多行注释:/* ... */ 使用 # 时,# 和注释的内容之间可以没有空格 使用 -- 时,-- 和注释内容之间必须有空格

  3. 解决linux ubuntu不能识别华为手机的问题--升级内核

    敝人手中有一个华为mate8,但是debian, ubuntu及一系列衍生版均不能识别.只能识别出一个华为手机助手,但是无法使用华为的内置存贮. 在fedora上是可以完美使用的. 归根到底的原因,是 ...

  4. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  5. 解决maven update project 后项目jdk变成1.5的问题

    一.问题描述 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.7, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5. 二.原因分析 ...

  6. Servlet 知识点 中文乱码的本质与解决

    本质原因:在servlet中出现中文乱码的原因编码和解码采用的不是一个编码表或者两个编码表不是兼容 例如UTF-8编码.GBK编码都可以读取中文,那么如果采用UTF-8编码保存文件,但是采用GBK编码 ...

  7. Mac版Java安装与配置

    一.下载并安装JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 双击下载的 ...

  8. 20155312 2006-2007-2 《Java程序设计》第三周学习总结

    20155312 2006-2007-2 <Java程序设计>第三周学习总结 课堂内容总结 yyp复制上一行代码 5不是false statistics.sh换成.bat就可以在windo ...

  9. CSS-弹性布局-伪类选择器-复杂选择器

    1.定位 1.堆叠顺序 一旦将元素变为已定位元素的话,元素们则有可能出现堆叠的效果. 如何改变堆叠顺序? 属性:z-index 取值:无单位的数字,数字越大越靠上. 注意: 1.父子元素间,z-ind ...

  10. c#的装箱和拆箱及值类型和引用类型

    装箱:它允许根据值类型创建一个对象,然后使用对这新对象的一个引用. int i = 5; object o = i; int j = (int)o; 装箱:运行时将在堆上创建一个包含值5的对象(它是一 ...