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)

相关推荐

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

    2024-06-11 11:42:05       12 阅读
  2. [COCI2018-2019#1] Zamjena 解题记录

    2024-06-11 11:42:05       17 阅读
  3. Codeforces Round 918 (Div. 4) 1

    2024-06-11 11:42:05       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 11:42:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 11:42:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 11:42:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 11:42:05       18 阅读

热门阅读

  1. Kappa架构介绍

    2024-06-11 11:42:05       9 阅读
  2. Eureka和Nacos有哪些区别?

    2024-06-11 11:42:05       9 阅读
  3. idea使用和了解

    2024-06-11 11:42:05       10 阅读
  4. 04-4.2.2 KMP 算法

    2024-06-11 11:42:05       10 阅读