题目
C. 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, ..., a**k (0 ≤ a**i ≤ 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
Copy
400 4 100 300 450 500
output
Copy
2
input
Copy
50 2 100 25
output
Copy
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:
.
思路
Lemma
对于一个序列\(a_1, a_2, a_3, a_4, ... ,a_n\), 且\(a_i \in [-k, k]\),设\(f(x) = \sum \limits^x_i {a_i}\), 且 \(f(n) = 0\),则将\(a_i\)的顺序进行一定的排列,可以使\(f(x) \in [0, k]\)。
严格的证明真是不会了,这里给出一个直观的证明由介值定理,证明\(f(x) \in [0, k]\),我们只需要证明该区间的最大值是\(x \in [0, k]\), 最小值是0.
因为\(f(n) = 0\), \(max(f(1)) = max(a[i]) \in [0, k]\) , 我们假设\(f(1)\)为k,直观上理解,若使该函数递减至小于0,那么该数不合法,若使该函数大于k,我们可以排列将其值在0,k之间波动。
知道这些后,我们便可以通过BFS使其降低复杂度。
做法
因为上述公式化简后,可以变为
\(\sum \limits^n_{i =1}(a_i - n) = 0\)
所以我们只需要进行BFS枚举每一步该选什么值,然后搜索出最短路,复杂度为\(O((2n)^2)\)
通过上述引理,我们可以将数值缩小在\([0, n]\) 间复杂度\(O(n^2)\)
另外我们并不需要考虑\(k\)的取值,而是考虑\(a_i - n\)的取值
另外这道题也可以当成完全背包来做,每个物品的价值为\(a_i-n\), 那么求的就是考虑前i个物品价值为0时的最小物品容量
Code
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <cstring>
using namespace std;
const int N = 1e3 + 10;
int a[N], cnt, vis[N], dep[N];
/*
很巧妙的一道题, 最终将
问题转化为求: 从一个序列中任意挑出k个数,使其和为0,求最小的k
这个问题再转化为bfs也很巧妙
另外有个结论 对于 -k k 之间的数若存在一个前缀和序列为0那么调整一下,可以保证
任意的前缀和0, k之间
*/
int bfs(){
memset(dep, -1, sizeof dep);
queue<int> q1;
q1.push(0), dep[0] = 0;
while(q1.size()) {
int t = q1.front(); q1.pop();
for(int i = 1; i <= cnt; i ++) {
if(t + a[i] == 0){
return dep[t] + 1;
}
if(t + a[i] > 0 && t + a[i] <= 1000 && dep[t + a[i]] == -1) {
q1.push(t + a[i]);
dep[t + a[i]] = dep[t] + 1;
}
}
}
return -1;
}
int main() {
int n, k;
cin >> n >> k;
for(int i = 1; i <= k; i ++) {
int t;
cin >> t;
if (!vis[t]) vis[t] = true, a[++cnt] = t - n;
}
cout << bfs();
}