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. selenium学习-模拟键盘按键操作

    导入  from selenium.webdriver.common.keys import Keys  格式:Keys.XXX 一般这么用:send_keys(Keys.XXX) # coding= ...

  2. win10 hhctrl.ocx 丢失

    1.我的是从同事电脑上复制过来的,他电脑也是win102.复制文件“hhctrl.ocx”到系统目录下  32位系统目录为:C:\WINNT\System32:64位系统为C:\Windows\Sys ...

  3. c语言1作业07

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/9932 我在这个课程的目 ...

  4. C++ string 详细用法

    string不是STL的容器(知道这一点的时候我也很吃惊),但是它与STL容器有着很多相似的操作,不需要担心长度问题,还封装了多种多样的方法,十分好用. 用到的库 #include <strin ...

  5. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  6. import和from.…import…

    import和from.-import- 在讲之前我们先来讲一下怎样去下载第三方库,我们把python看作一部手机,pip就是应用管家,第三方库里面的模块就是应用管家里面的一个应用 一.import模 ...

  7. 认识react, 并简单与vue对比

    应用场景: 负责场景下的高性能 重用组件库,组件组合 中文官网:https://reactjs.org.cn/doc/in... 特点: 声明式编码(不需要关心如何实现,只需要关注在哪里做什么) 组件 ...

  8. RateLimit--使用guava来做接口限流

    转:https://blog.csdn.net/jiesa/article/details/50412027 一.问题描述   某天A君突然发现自己的接口请求量突然涨到之前的10倍,没多久该接口几乎不 ...

  9. Numpy 基础函数 --《Python 数据分析从入门到精通》

    在Numpy中,方向称作轴,轴的数目称作维.(array(z,y,x)) np.empty() 函数的使用待完全确定.(eg: np.empty([2,3])  -> ([[0,0,0] [0, ...

  10. Filter实现登录功能限制

    public void doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain chain) throws IOException, ...