Codeforces 789E The Great Mixing【Bfs+dp】

来源:互联网 发布:eplan电气绘图软件 编辑:程序博客网 时间:2024/05/09 17:31

E. The Great Mixing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration. Assume that the friends have unlimited amount of each Coke type.

Input

The first line contains two integers n,k (0 ≤ n ≤ 1000,1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration, or-1 if it is impossible.

Examples
Input
400 4100 300 450 500
Output
2
Input
50 2100 25
Output
3
Note

In the first sample case, we can achieve concentration using one liter of Coke of types and:.

In the second case, we can achieve concentration using two liters of type and one liter of type:.


题目大意:

给你K种浓度的药水,让你配出浓度为N的药水、

问最少需要用多少个药水,一种药水可以无限次使用。


思路:


1、要求出浓度为N的药水 ,我们不妨先将每种药水都减去N,那么问题就变成了凑0.


2、那么要怎么凑0呢?

我们设定dp【i】表示浓度为i的药水最少配成需要的药水个数,那么只要设定大小为2000+即可。

然后确定一点,ai<=1000,那么药水的种类最多也就是1000种那么我们此时Bfs维护dp数组的时间复杂度就是O(1000*2000);

可以轻松Ac.


Ac代码:


#include<stdio.h>#include<string.h>#include<queue>#include<map>using namespace std;int a[5000];int dp[5000];int tot;void Bfs(){    memset(dp,0,sizeof(dp));    queue<int >s;    int mid=2000;    for(int i=0;i<tot;i++)    {        dp[a[i]+mid]=1;        s.push(a[i]);    }    while(!s.empty())    {        int u=s.front();        s.pop();        for(int i=0;i<tot;i++)        {            if(u+a[i]>=-1005&&u+a[i]<=1005)            {                if(dp[u+a[i]+mid]==0)                {                    dp[u+a[i]+mid]=dp[u+mid]+1;                    s.push(u+a[i]);                }            }        }    }    if(dp[0+mid]==0)printf("-1\n");    else    printf("%d\n",dp[0+mid]);}int main(){    int n,k;    while(~scanf("%d%d",&n,&k))    {        map<int ,int >s;        tot=0;        for(int i=0;i<k;i++)        {            int x;            scanf("%d",&x);            x-=n;            if(s[x]==0)            {                s[x]=1;                a[tot++]=x;            }        }        Bfs();    }}











0 0
原创粉丝点击