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

【题意】给出一个左右两边的臂长都为15的天平,有n个挂钩,m个砝码,求多少的方法使他平衡

【思路】dp[i][j]表示i表示当前发码数,j表示当前平衡状态,不能为负,所以重新规定了平衡点15*20*25=7500;

j<7500左边重,反之右边重

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=**;
const int N=;
int c[N],g[N],dp[N][];
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
{
scanf("%d",&c[i]);
}
for(int i=; i<=m; i++)
{
scanf("%d",&g[i]);
}
memset(dp,,sizeof(dp));
dp[][M]=;//0个砝码时为平衡状态为1种,小于7500为左边重,反之右边重
for(int i=; i<=m; i++)
{
for(int j=; j<=M*; j++)
if(dp[i-][j])
{
for(int k=; k<=n; k++)
{
dp[i][j+c[k]*g[i]]+=dp[i-][j];
}
}
}
printf("%d\n",dp[m][M]);
}
return ;
}

Balance_01背包的更多相关文章

  1. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  2. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

  3. HDU2159 二维完全背包

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. CF2.D 并查集+背包

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  5. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  6. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  7. POJ1717 Dominoes[背包DP]

    Dominoes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6731   Accepted: 2234 Descript ...

  8. HDU3466 Proud Merchants[背包DP 条件限制]

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

  9. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

随机推荐

  1. IO流 总结二

    流只能操作数据. File 类 用来将文件或者文件夹封装成对象. 方便文件与文件夹进行操作 File对象可以作为参数传递给流的构造函数. 可以将已有的和已出现的文件或者文件夹封装成对象 File a ...

  2. SO修改

    FUNCTION Z_SD_SALESORDER_CHANGE1. *"----------------------------------------------------------- ...

  3. 500 TypeError: Cannot read property 'connect.sid' of undefined

    1:在写passport验证测试用例时,发现有几个引用中间件顺序的错误,检查发现,passport验证写的是session,在传错误信息的时候req.flash调用也需要用到session中间件,否则 ...

  4. Android开发--Activity的创建

    1.Activity概述 Activity是android四大基本组件之一.每一个activity文件对应一个界面,一个程序由多个activity组成. 2.Android工作目录

  5. HideProcess

    #ifndef CXX_HIDEPROCESS_H # include "HideProcess.h" #endif #ifdef _WIN64 #define ActivePro ...

  6. POJ 2159 Ancient Cipher 难度:0

    题目链接:http://poj.org/problem?id=2159 #include <cstring> #include <cstdio> #include <cc ...

  7. VS2010静态编译生成.exe可执行文件

    VS2010静态编译生成的.exe可执行文件,可以在其他未安装VS2010的电脑直接运行. 静态编译:就是在编译可执行文件的时候,将可执行文件需要调用的对应动态链接库(.so)中的部分提取出来,链接到 ...

  8. Android Phonebook编写联系人UI加载及联系人保存流程(四)

    2014-01-07 10:23:22 将百度空间里的东西移过来. 5. KindSectionView KindSectionView是何方神圣呢?它又是怎么怎么和一个DataKind,以及一个Ra ...

  9. [开发笔记]-火狐的event和jquery1.9.1.min的问题

    一:火狐不兼容window.event.keyCode问题 火狐的event是以参数形式传入的 function onlychinese(event) { event = event || windo ...

  10. format 字符串

    http://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html ShowMessage( Format('this is %.0f',[ t ...