题意:给你n k个数 问你能不能k个数任意取 假设取出m个 能不能组成n的m倍 m最小 不存在输出-1
题解:
(s1+s2+...+sm)/(m*1000)=n/1000
s1+s2+...+sm=m*n
(s1-n)+(s2-n)+...+(sm-n)=0
所以我们把ai都减去n
然后从0开始广搜 搜到0即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<deque>
using namespace std;
int n,k;
deque<int>vis;
map<int,int>sp,sp1;
int main(){
scanf("%d%d",&n,&k);
int i,j,x;
for(i=1;i<=k;i++){
scanf("%d",&x);
sp[x-n]=1;
}
vis.push_back(0);
while(!vis.empty()){
int f=vis.front();
vis.pop_front();
for(i=-1000;i<=1000;i++){
if(sp[i]&&!sp1[f+i]){
sp1[f+i]=sp1[f]+1;
if(f+i==0){
printf("%d\n",sp1[f+i]);
return 0;
}
if(f+i>=-1000&&f+i<=1000)vis.push_back(f+i);
}
}
}
printf("-1\n");
return 0;
}