VK Cup 2015 - Round 1 C. The Art of Dealing with ATM

The Art of Dealing with ATM

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

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 k 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 , 1   000 10, 50, 100, 500, 1\ 000 10,50,100,500,1 000 and 5   000 5\ 000 5 000 burles, then at k   =   20 k = 20 k= 20 such ATM can give sums 100   000 100\ 000 100 000 burles and 96   000 96\ 000 96 000 burles, but it cannot give sums 99   000 99\ 000 99 000 and 101   000 101\ 000 101 000 burles.

Let’s suppose that the country uses bills of n n 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 q 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 q q of requests for cash withdrawal.

Input

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

The next line contains n n n space-separated integers a i a_i ai ( 1   ≤ a i ≤   1 0 7 1 \le a_i \le 10^7 1 ai 107) — the denominations of the bills that are used in the country. Numbers a i a_i ai follow in the strictly increasing order.

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

The next q q q lines contain numbers x i x_i xi ( 1   ≤ x i   ≤   2 ⋅ 1 0 8 1 \le x_i \le 2·10^8 1 xi 2108) — 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 -1 1, if it is impossible to get the corresponding sum.

Example

i n p u t \tt input input
6 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
o u t p u t \tt output output
6
20
19
20
-1
3
-1
-1
i n p u t \tt input input
5 2
1 2 3 5 8
8
1
3
5
7
9
11
13
15
o u t p u t \tt output output
1
1
1
2
2
2
2
-1

Tutorial

由于最多只可能用两种不同的面额,所以可以把这一题抽象成两数之和,因为最多只能取 k k k 张纸币,所以可以将每种面额取 x ( x ∈ [ 0 , k ] ) x(x \in [0, k]) x(x[0,k]) 张的总金额算出来,最后对于每次询问请求,直接用哈希表解法解决即可

此解法时间复杂度为 O ( n k ) \mathcal O(nk) O(nk)

Solution

import sys
input = lambda: sys.stdin.readline().strip()
from collections import Counter

n, k = map(int, input().split())
a = list(map(int, input().split()))
mp = Counter()
for ai in a:
    for i in range(k + 1):
        mp[ai * i] = i

for _ in range(int(input())):
    x = int(input())
    ans = k + 1
    for i in mp:
        if (x - i) in mp:
            ans = min(ans, mp[i] + mp[x - i])
    print(-1 if ans > k else ans)
  • 18
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值