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. jar转dll

    IKVM http://www.cnblogs.com/luckeryin/archive/2012/03/28/2421274.html

  2. ASP.NET Web API路由规

    默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...

  3. 一个Java递归程序

    先来没事搜了一些面试题来做,其中一道:输入一个整数,求这个整数中每位数字相加的和? 思考:1.如何或得每一位数:假如是1234,   1234%10=4,得到个位:(1234/10)%10=3得到十位 ...

  4. mySql常用sql

    mysql sql语句大全1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server- ...

  5. Servlet作业--实现注册和登录

    1.注册页面  zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  6. PowerMock使用遇到的一些问题

    首先使用PowerMock  Mock对象如果不成功的话首先要检查在测试类上是否有这两个声明@RunWith(PowerMockRunner.class)                        ...

  7. 编写一个小Servlet程序

    1.编写一个java类,此类继承HttpServlet 创建完工程(见上一篇随笔:使用eclipse创建在myeclipse中运行的web工程),在src中新建一个包,包名字叫servlet:再新建一 ...

  8. 经典线程同步 关键段CS

    上一篇<秒杀多线程第四篇 一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,本篇将用关键段CRITICAL_SECTION来尝试解决这个问题. 本文首先介绍下如何使用关键段,然 ...

  9. 响应式架构:消息模式Actor实现与Scala、Akka应用集成

    这是一本最近很流行的书. 有时间就去看看

  10. c# 多线程创建 ---简单

    Thread t = new Thread(new ParameterizedThreadStart(UploadCard)); t.IsBackground = false;//后台线程  前台线程 ...