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. 输入一个天平若干(<=20)挂钩的位置,将若干(<=20)砝码挂到天平上,问有多少种使天平挂平衡的方法。

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

HINT

第一种放法把(4,8)放左边,(3,5)放右边
第二种放法把((3,4,5)放左边,8单独放右边

Solution

直接背包。

Code

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout)
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
#define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x))
char B[ << ], *S = B, *T = B;
inline int F()
{
R char ch; R int cnt = ; R bool minus = ;
while (ch = getc(), (ch < '' || ch > '') && ch != '-') ;
ch == '-' ? minus = : cnt = ch - '';
while (ch = getc(), ch >= '' && ch <= '') cnt = cnt * + ch - '';
return minus ? -cnt : cnt;
}
#include <map>
#define maxn 30
int a[maxn], b[maxn];
long long f[maxn][];
int main()
{
// setfile();
R int n = F(), m = F(), sum = ;
for (R int i = ; i <= n; ++i) a[i] = F();
for (R int i = ; i <= m; ++i) b[i] = F(), sum += b[i];
std::sort(a + , a + n + );
R int minn = a[] * sum, maxx = a[n] * sum;
f[][] = ;
for (R int k = ; k <= m; ++k)
for (R int i = minn; i <= maxx; ++i)
for (R int j = ; j <= n; ++j)
f[k][i + ] += f[k - ][i - a[j] * b[k] + ];
printf("%lld\n", f[m][] );
return ;
}

【BZOJ2022】Pku1837 Balance的更多相关文章

  1. 【mq】从零开始实现 mq-07-负载均衡 load balance

    前景回顾 [mq]从零开始实现 mq-01-生产者.消费者启动 [mq]从零开始实现 mq-02-如何实现生产者调用消费者? [mq]从零开始实现 mq-03-引入 broker 中间人 [mq]从零 ...

  2. 【POJ】2142 The Balance 数论(扩展欧几里得算法)

    [题意]给定a,b,c,在天平左边放置若干重量a的砝码,在天平右边放置若干重量b的砝码,使得天平两端砝码差为c.设放置x个A砝码和y个B砝码,求x+y的最小值. [算法]数论(扩展欧几里德算法) [题 ...

  3. 【习题 6-1 UVA-673】Parentheses Balance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 括号匹配. 栈模拟就好. 多种括号也是一样可以做的. [代码] #include <bits/stdc++.h> usi ...

  4. 【原】FMDB源码阅读(一)

    [原]FMDB源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 说实话,之前的SDWebImage和AFNetworking这两个组件我还是使用过的,但是对于 ...

  5. UOJ #148. 【NOIP2015】跳石头 二分

    #148. [NOIP2015]跳石头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/148 Descripti ...

  6. hadoop 常用配置项【转】

    hadoop 常用配置项[转] core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义 ...

  7. 【RAC】RAC相关基础知识

    [RAC]RAC相关基础知识 1.CRS简介    从Oracle 10G开始,oracle引进一套完整的集群管理解决方案—-Cluster-Ready Services,它包括集群连通性.消息和锁. ...

  8. 【算法】论平衡二叉树(AVL)的正确种植方法

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  9. 【OS】NMON的简介和使用

    [OS]NMON的简介和使用 目前NMON已开源,以sourceforge为根据地,网址是http://nmon.sourceforge.net. 1. 目的 本文介绍操作系统监控工具Nmon的概念. ...

随机推荐

  1. Leetcode 35.搜索插入位置 By Python

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输 ...

  2. spring+redis实例(二)

    这一篇redis实例是基于序列化储存-(写入对象,读取对象) 在spring+redis(一)中我们介绍了在spring中怎么去操作储存redis,基于string的储存,今天我们介绍一下redis基 ...

  3. 洛谷 P1472 奶牛家谱 Cow Pedigrees 题解

    题面 这道题我觉得是个不错的题: 根据题意可以较清晰的发现ans只和n和k有关:(因为输入的只有这两个数啊~): 那么设f[i][j]表示前i层用了j个节点的方案数,g[i][j]表示深度小于等于i并 ...

  4. php文件上传php.ini配置参数

    php文件上传服务器端配置参数 file_uploads = On,支持HTTP上传uoload_tmp_dir = ,临时文件保存目录upload_max_filesize = 2M,允许上传文件的 ...

  5. Picnic Planning POJ - 1639(度限制生成树)

    解题报告   题意理解 给定一张N个点,M个边的无向图,求出无向图的一颗最小生成树,但是我们要求一号节点的入度不可以超过给定的整数S 也就是一个最小生成树,要求它的一号节点,最多只能和S个节点相连. ...

  6. 【MQ】为什么选择RocketMQ?

    一.前言 提到mq,可能很多朋友都有多耳闻,很多大公司都在使用这种技术.就小编而言,听说使用mq可以进行秒杀的操作,而且使用十分的方便,效率十分的高.以前小编也做过关于秒杀的技术,就是使用悲观锁对DA ...

  7. mybatis动态sql详情

    mybatis动态拼装sql详情 MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choos ...

  8. JSON.parse()与JSON.stringify()和eval()使用方法详解

    在和后端对接口的时候,遇到了一个问题 就是series里面数据变量进行拼接的时候,data数据里面全部是数值int类型的 但是因为某些需求需要让他进行某个数据之前的数据都为空 我试过用空字符串和und ...

  9. scp 自动带密码参数复制文件到主机

    一.安装sshpass工具 [root@zabbix_server scripts]# yum install sshpass 二.运行 [root@zabbix_server scripts]# s ...

  10. linux编译安装mysql5.7

    一.下载源码包 -src cd /usr/local/src/mysql--src wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5 ...