The Great Mixing
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
400 4
100 300 450 500
2
50 2
100 25
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:
.
广搜,(a1+a2+...+am)/m == n;a1+a2+...+am = n * m;a1-n+a2-n+...am-n == 0;如此ai-n变为要存的值,节省了数组的空间。
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int visited[],ans[];
int n,k,s[],no;
int bfs()
{
memset(ans,-,sizeof(ans));
queue<int> q;
q.push();
ans[] = ;
int head,temp;
while(!q.empty())
{
head = q.front();
q.pop();
for(int i = ;i < no;i ++)
{
temp = head + s[i];
if(temp == )
{
ans[temp] = ans[head] + ;
return ans[temp];
}
else if(temp > && ans[temp] == -)
{
ans[temp] = ans[head] + ;
q.push(temp);
}
}
}
return -;
}
int main()
{
int d;
scanf("%d%d",&n,&k);
for(int i = ;i < k;i ++)
{
scanf("%d",&d);
if(visited[d] == )
{
visited[d] = ;
s[no ++] = d - n;
}
}
cout<<bfs();
return ;
}
The Great Mixing的更多相关文章
- Mixing Delphi and C++(相互调用)
Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...
- Mixing a dll boost library with a static runtime is a really bad idea错误的解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 同事在使用boost库时遇到一个问题,在编译时出现一个错误:Mixing a dll boost library wi ...
- Codeforces 788C The Great Mixing
The Great Mixing 化简一下公式后发现, 问题变成了, 取最少多少数能使其和为1, bitset优化一下背包就好啦. 题解中介绍了一种bfs的方法没, 感觉比较巧妙. #include& ...
- P1208 [USACO1.3]混合牛奶 Mixing Milk
P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...
- 洛谷——P1208 [USACO1.3]混合牛奶 Mixing Milk
P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...
- 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...
- Mixing Milk 混合牛奶 USACO 贪心
1009: 1.3.1 Mixing Milk 混合牛奶 时间限制: 1 Sec 内存限制: 128 MB提交: 9 解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1. ...
- Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water
题目链接:B.New Theatre Square 题意: 你要把所有"." 都变成"*",你可以有两个选择,第一种就是一次铺一个方块(1*1),第二种就是同一 ...
- 2019 GDUT Rating Contest I : Problem H. Mixing Milk
题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...
- Mixing ASP.NET and MVC routing
Here is the solution I settled on. I installed the NuGet Microsoft.AspNet.FriendlyUrls package. Then ...
随机推荐
- Openstak(M版)计算节点安装
#############修改hosts文件 10.0.0.11 controller10.0.0.31 compute110.0.0.32 compute210.0.0.41 block110.0. ...
- Python 模块续 configparser、shutil、XML、paramiko、系统命令、
一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...
- iOS 当公司有人向你提问,你该如何应对?
今天 因为iOS 开发的内部版本号耿耿于怀好久,释然后让我有了一个新想法:从前,能让我兴奋的点是解决一个有一个拗脑筋的问题,见大部分博客便知,都是技术方面的积累. 那么从今天起我决定让自己有个新起点, ...
- ssh登陆virtualbox虚拟机
- BFC与边距重叠详解
1.什么是BFC? 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位Box 是 CSS 布局的对象和基本单位, 直观点来说, ...
- copy deepcopy辨析
copy deepcopy讲的是复制源对象的改变对copy出来的对象的影响: 我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在. 所以改变原有被复制对象不会对已经复制 ...
- body-parser小解
body-parser用来解析http请求体,对不同的content-type有不同的处理方式, 首先介绍一下常见的四种Content-Type: 1.application/x-www-form-u ...
- UITableViewCell使用时注意事项
1,注意使用重用机制(有利于提高效率) 2,做到通过改变模型去间接改变UI样式(做到永久改变,无论怎样拖动刷新,都不会恢复改变) 3,在通过传递模型给Cell控件布局时,记得完全覆盖(嗯,不好解释,主 ...
- 在.h和.cpp中包含头文件的区别
1.在.h中包含头文件,是为了声明一系列这个头文件的变量等,可能会产生重复包含的问题: 2.在.cpp中包含头文件只是为了实现这个头文件或者使用其中的方法,不会有重复包含的问题,所以尽量在源文件中包含 ...
- sg函数的应用
刚刚接触到sg函数突然感觉到原来可以这么好用,sg函数应该算是博弈论中比较经典的东西了.下面来说说sg函数: 从网上搜集资料终于能看懂了下面解释来自http://www.cnblogs.com/cj6 ...