Codeforces Round #407 (Div. 2): E__onlinejudge

Source: Internet
Author: User
Tags prepare

E. The great mixing time limit/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 tion. The drink should also is tasty, so glass can contain only integer number of liters to each Coke type (some types can B E 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 your mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke of the AS.

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

The 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 imp Ossible. Examples input

4
100 300 450 500
Output
2
Input
2
100 25
Output
3
Note

In the "a" 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:. Question: The target concentration is given, and the concentration of the target can be determined by the density given.

Exercises

Then run a BFS on it, (note, to first determine whether it is feasible, because the si-n must have positive negative, so if all positive or negative, then 1).

Code:

Wud #include <iostream> #include <algorithm> #include <cstdio> #include <stack> #include < cstring> #include <vector> #include <bitset> #include <string> #include <cmath> #include <
set> #include <map> #include <queue> using namespace std;
typedef long Long LL;
const int MAXN = 1E6+7;
const int INF = 0X3F3F3F3F;
int n,m;
int S[MAXN];
Queue<int> que;
Map<int,int> MP;
    int main () {scanf ("%d%d", &n,&m);
        for (int i = 0;i < m;i++) {scanf ("%d", &s[i]);
    S[i] = s[i]-n;
    Sort (s,s+m);
    if (s[0]*s[m-1]>0) return 0*printf ("%d\n",-1);
    m = Unique (s,s+m)-S;
    Que.push (0);
    int f = 0;
        while (!que.empty ()) {int u = que.front (); Que.pop ();
            for (int i = 0;i < m;i++) {int v = u+s[i];
                if (v==0) {printf ("%d\n", mp[u]+1);
                f = 1;
            Break
    } if (!mp[v]) {            MP[V] = mp[u]+1;
            Que.push (v);
    } if (f) break;
    } if (f = = 0) printf ("%d\n",-1);
return 0;
 }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.