Balance

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 11947 Accepted: 7464

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

题意:

有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。

其中可以把天枰看做一个以x轴0点作为平衡点的横轴

动态规划dp[i][j],i代表第i个钩码,j代表平衡状态

将所有的钩码都挂上的极端情况就是[-7500,7500],所以移动平衡点,将7500作为新的平衡点,所以对于dp[i-1][j],dp[i][j+len[k]*w[j]]+=dp[i-1][j],有挂i-1个钩码状态转移到挂i个钩码时对应的状态

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std; const int MAX = 15000; int dp[25][MAX+10]; int w[25],len[25]; int main()
{
int C,G;
while(~scanf("%d %d",&C,&G))
{
for(int i=1;i<=C;i++)
{
scanf("%d",&len[i]);
}
for(int i=1;i<=G;i++)
{
scanf("%d",&w[i]);
}
memset(dp,0,sizeof(dp));
dp[0][7500]=1;
for(int i=1;i<=G;i++)
{
for(int j=0;j<=MAX;j++)
{
if(dp[i-1][j])
{
for(int k=1;k<=C;k++)
{
dp[i][j+len[k]*w[i]]+=dp[i-1][j];
}
}
}
}
printf("%d\n",dp[G][7500]);
}
return 0;
}

Balance的更多相关文章

  1. Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)

    有时我们在实际分类数据挖掘中经常会遇到,类别样本很不均衡,直接使用这种不均衡数据会影响一些模型的分类效果,如logistic regression,SVM等,一种解决办法就是对数据进行均衡采样,这里就 ...

  2. LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级

    LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一 ...

  3. Balance - 七夕悠然

    想争取一个月至少一篇博客的,还是没搭上七月的末班车.两个小妹妹来上海看我了,工作上又有点儿忙,充分利用所有时间了,还是没有挪出时间来写东西,貌似写东西也要时机一样,需要在可以静静思考的时候,再加上有淡 ...

  4. Java-集合=第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: List list = new ArrayList(); list.add(new A

    第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得 ...

  5. java异常处理:建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),

    建立exception包,建立Bank类,类中有变量double  balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount ...

  6. UVa 673 Parentheses Balance -SilverN

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  7. POJ1837 Balance[分组背包]

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 Descript ...

  8. UVa 673 Parentheses Balance

    一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...

  9. A feature in Netsuite Reports > Financial > Balance Sheet

    最新版本的Customize balance sheet page Left side > Layout > Add Reference Row Then in right side, y ...

  10. POJ 3142 The Balance

    Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...

随机推荐

  1. linux:/etc/rc.local 不能自动启动问题

    前段时间安装LNMP环境,配置/etc/rc.local的时候配置了启动mysql.nginx.php以及关闭防火墙,可结果重启了七八次还是自启动不了后来终于找到原因了 看下图: /etc/rc.lo ...

  2. Vagrant配置与使用

    Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境. Vagrant is an amaz ...

  3. iOS 工程中文件变成红色是什么情况

    iOS 工程中文件变成红色是原有的文件路径改变了,系统找不到了.

  4. Linux的set

    功能说明: 设置shell 语 法: set [+-abCdefhHklmnpPtuvx] 补充说明: set指令能设置所使用shell的执行方式,可依照不同的需求来做设置. 参 数: -a 标示已修 ...

  5. C++之路进阶——poj2104(K-th Number)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44537   Accepted: 14781 Ca ...

  6. JavaOOP QuickHit项目分析

    项目需求:游戏等级6级,随机字符串每级长度不同.每升一级减少比较次数,但是字符串长度相应增加!每级总分数不同,如果游戏中途输入错误则游戏退出!玩家每次在规定时间内输入字符串的同时,打印出游戏难度等级. ...

  7. php 无限循环

    什么是无限分类呢?就像windows下新建一个文件夹,在新建的文件夹下又可以新建 一个文件夹,这样无限循环下去,无限分类也是这样,父类可以分 出它子类,子类又 可以分出它的子类,这样一直无限循环下去. ...

  8. 16---Net基础加强

    更新中,敬请期待............ Xml介绍 xml读写练习 xml练习1 xml练习2

  9. RobotFrameWork接口报文测试-----(二)demo的升级版

    在上一篇,简单的demo实现了讲xml的数据发送服务器端并取得recvi_buf,然后进行了简单的解析的操作.现在就要解决之前提过的2个问题: 1. 步骤这么多,难道每写一个脚本都要重复一次么? 2. ...

  10. 在MVC里面使用Response.Redirect方法后记得返回EmptyResult

    在ASP.NET MVC中我们很多时候都会在拦截器和Controller中直接使用Response.Redirect方法做跳转,但是实际上Response.Redirect方法执行后ASP.NET并不 ...