VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: xxx  Solved: 2xx

题目连接

http://codeforces.com/contest/529/problem/E

Description

ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.

For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.

Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.

Input

The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20).

The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order.

The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make.

The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.

Output

For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.

Sample Input

Input
6 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
 
Input
5 2
1 2 3 5 8
8
1
3
5
7
9
11
13
15

Sample Output

Output
6
20
19
20
-1
3
-1
-1
Output
1
1
1
2
2
2
2
-1

HINT

题意:

有n种纸币,选择其中的两类纸币,最多拿m张,问最少多少张能够组成规定的面额

不能的话,就输出-1

题解:

暴力求解!

直接暴力枚举第一种钱,然后再暴力枚举第二种钱就好啦~

~\(≧▽≦)/~啦啦啦,这道题完啦

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* */
//************************************************************************************** map<int,int>s;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn];
int main()
{
int n=read(),m=read();
for(int i=;i<n;i++)
{
a[i]=read();
s[a[i]]=;
}
int q=read();
while(q--)
{
int ans=maxn;
int k=read();
for(int i=;i<n;i++)
{
for(int j=;j<=m;j++)
{
//cout<<a[i]*j<<endl;
if(a[i]*j>k)
break;
if(a[i]*j==k)
ans=min(ans,j);
for(int t=;t<=m-j;t++)
{
if(((k-a[i]*j)%t)==)
{
if(s.count((k-a[i]*j)/t)==)
ans=min(ans,j+t);
}
}
}
}
if(ans==maxn)
printf("-1\n");
else
printf("%d\n",ans);
}
}

Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!的更多相关文章

  1. VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) E. Correcting Mistakes 水题

    E. Correcting Mistakes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  2. VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp

    题目链接: http://codeforces.com/problemset/problem/533/B B. Work Group time limit per test2 secondsmemor ...

  3. VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值

    E. Rooks and Rectangles Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemse ...

  4. VK Cup 2015 - Round 2 E. Correcting Mistakes —— 字符串

    题目链接:http://codeforces.com/contest/533/problem/E E. Correcting Mistakes time limit per test 2 second ...

  5. VK Cup 2015 - Round 1 -E. Rooks and Rectangles 线段树最值+扫描线

    题意: n * m的棋盘, k个位置有"rook"(车),q次询问,问是否询问的方块内是否每一行都有一个车或者每一列都有一个车? 满足一个即可 先考虑第一种情况, 第二种类似,sw ...

  6. VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)

    VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) 代码 VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) A ...

  7. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

    题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...

  9. codeforces VK Cup 2015 - Qualification Round 1 B. Photo to Remember 水题

    B. Photo to Remember Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/522/ ...

随机推荐

  1. mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by

    在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ...

  2. python基础--os模块和sys模块

    os模块提供对操作系统进行调用的接口 # -*- coding:utf-8 -*-__author__ = 'shisanjun' import os print(os.getcwd())#获取当前工 ...

  3. Owin 自定义中间件(2)中间件进阶

    前面一篇文章简单的介绍了中间件的应用 下面编写一个自定义的中间件类库,并加入中间件参数以及引入日志记录中间件的异常 下面来看下中间件的构造,参数可以自定义 这里我定义了一个参数类 编写中间件需要引入 ...

  4. hdu 5112 (2014北京现场赛 A题)

    给出某个时刻对应的速度 求出相邻时刻的平均速度 输出最大值 Sample Input23 // n2 2 //t v1 13 430 31 52 0 Sample OutputCase #1: 2.0 ...

  5. 微商城三级分销源码公众号开发 微分销 C#源码

    需要源码,请加QQ:858-048-581 ,可以查看演示 运行环境:vs2012+ sql2008r2 [什么是微分销] 微分销是助力企业进军移动电商,完善分销体系搭建微信分销系统.基于微信平台,搭 ...

  6. day6面向对象

    面向对象介绍(http://www.cnblogs.com/alex3714/articles/5188179.htm)     世界万物,皆可分类     世界万物,皆为对象     只要是对象,就 ...

  7. 【LOJ】#2447. 「NOI2011」兔兔与蛋蛋的游戏

    题解 对于75分来说,操作肯定不会成环,可以暴搜 看成空格在移动,空格移动到原来的位置肯定经历了偶数个格子,但是操作的人是两个不同的人,所以肯定不会成环 对于满分做法,要找到一种更好的方式判先手是否会 ...

  8. Python全栈开发之17、tornado和web基础知识

    一.web基础知识 学习web框架之前,先来看一下web基础知识,首先要明白其本质就是socket,用户对应一个socket客户端,但是如果从socket开始开发web应用程序那么效率太了,正确的做法 ...

  9. lr11录制时报“Request Connection: Remote Server @ 0.0.0.0:1080 (Service=?) NOT PROXIED! )”解决方法

    在录制脚本的时候出现如下现象: 解决方法: LoadRunner录制脚本时出现:Unable to connect to remote server),有事件没有脚本的问题   1.首先要查看IE浏览 ...

  10. filebeat安装部署

    简单概述 最近在了解ELK做日志采集相关的内容,这篇文章主要讲解通过filebeat来实现日志的收集.日志采集的工具有很多种,如fluentd, flume, logstash,betas等等.首先要 ...