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

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=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int a[maxn];
int main()
{
    int n=read(),m=read();
    for(int i=0;i<n;i++)
    {
        a[i]=read();
        s[a[i]]=1;
    }
    int q=read();
    while(q--)
    {
        int ans=maxn;
        int k=read();
        for(int i=0;i<n;i++)
        {
            for(int j=1;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=1;t<=m-j;t++)
                {
                    if(((k-a[i]*j)%t)==0)
                    {
                        if(s.count((k-a[i]*j)/t)==1)
                            ans=min(ans,j+t);
                    }
                }
            }
        }
        if(ans==maxn)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
}

 

posted @ 2015-03-25 23:55  qscqesze  阅读(462)  评论(0编辑  收藏  举报