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. Java基础之一组有用的类——Observable和Observer对象(Horrific)

    控制台程序. Obserable类提供了一个有趣的机制,可以把类对象中发生的改变通知给许多其他类对象. 对于可以观察的对象来说,类定义中需要使用java.util.Observable类.只需要简单地 ...

  2. Android项目目录结构分析

    Android项目目录结构分析 1.HelloWorld项目的目录结构1.1.src文件夹1.2.gen文件夹1.3.Android 2.1文件夹1.4.assets 1.5.res文件夹1.6.An ...

  3. 链表操作,空间复杂度要求为O(1)

    对于O(1)的空间复杂度要求,不能对链表进行复制等操作,双指针法对处理该类问题比较有效. 同时由于链表头结点的特殊性,可以考虑引入一个空的头结点来辅助操作.

  4. NSDateFormatter

    NSDate *now = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = ...

  5. PostgreSQL Replication之第十一章 使用Skytools(1)

    向您介绍了 Slony 之后,我们将介绍另外一种流行的复制工作.Skytools 是一个最初有 Skype 开发的软件包,它有多种用途.Skytools 不只是一个单一的程序,而且是一个工具与服务的集 ...

  6. andriod之应用内置浏览器 webview

    参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...

  7. Java如何对ArrayList里的元素排序

  8. Imageloarder

    首先需要加载jar包,和在配置文件中配置Mainactivity.javapackage com.baidu.day13_2imageloader; import com.nostra13.unive ...

  9. android 应用架构随笔一(架构搭建)

    1.拷贝积累utils以及PagerTab类 2.定义BaseApplication类 3.定义BaseActivity类 4.改写MainActivity 5.定义布局文件 6.定义BaseFrag ...

  10. DButil

    纲要: Properties prop = new Properties(); BasicDataSource ds = new BasicDataSorce(); Connection conn = ...