The Great Mixing CodeForces - 788C (BFS)


The Great Mixing

CodeForces - 788C

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.

Example
Input
400 4
100 300 450 500
Output
2
Input
50 2
100 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,只允许让你用已给的k种饮料,每个只允许用整数升,问你最少用多少升饮料就能配成目标浓度。

首先我们发现如果要满足条件,我假设第i种饮料取ki升,则

这样我们对于每个a[i],我们a[i]-=n,然后问题转换成了我们在这个数中选数,每个数能选多次,最后让所有选出的数的sum=0

然后用一个bfs去找次数的最小值就可以了,因为数值会出现负数,所以整体加上1000,这样可以标记

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1005
#define mask 1002
using namespace std;
int k,n,a[N*N],q[N*N];//a数组记录初始浓度,q是队列
int vis[2*N],f[2*N];//vis用于标记,f记录次数
int main(){
    scanf("%d%d",&n,&k);
    int i;
    for(i = 1; i <= k; i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+k);
    k = unique(a+1,a+1+k)-a-1;//unique函数返回第一个重复元素的迭代器,前面是非重复的
    for(i = 1; i <= k; i++)
        a[i] -= n;
    if(a[1]*a[k]>0){
        printf("-1\n");
        return 0;
    }
    int s = 1,t = 1;
    for(i = 1; i <= k; i++){
        q[t++] = a[i];
        f[a[i]+mask] = 1;
        vis[a[i]+mask] = 1;
    }
    while(s!=t&&!vis[mask]){
        for(i = 1; i <= k; i++){//枚举每个数,如果加上不够1002(实际是不到0),并且未访问过,入队 
            if(abs(a[i]+q[s])<=1002&&!vis[a[i]+q[s]+mask]){
                f[a[i]+q[s]+mask] = f[q[s]+mask]+1;//上一个状态的选取次数+1
                vis[a[i]+q[s]+mask] = 1;
                q[t++] = a[i]+q[s];
            }
        }
        s++;
    }
    printf("%d\n",f[mask]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值