http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1954
这个题给我们的感觉就是完全背包 但是N太大 需要用鸽巢原理优化
先将a(1---n)排序
(a1,a2,a3.......an) 每个数选择的个数为(k1,k2,k3........kn)(ki可以为0)
使得 k1*a1+k2*a2+k3*a3+.........+kn*an==N
则 (k1+k2+k3+.....kn-1)<an
用反证法证明: 假如说sum= (k1+k2+k3+.....kn-1)>=an 那么根据鸽巢原理 在前sum个数里面 一定存在 连续的几个数(ai---aj)之和为an的倍数
那么用 一定数量的an代替这几个数(ai----aj) 一定更优
所以(k1+k2+k3+.....kn-1)<an 成立
所以前(n-1)个数的数量之和最多也得小于an(100) 前(n-1)个数的和小于10000 然后对前(n-1)个数进行背包再枚举就可以了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include <iomanip>
using namespace std;
#define LL long long
#define ULL unsigned long long
const double eps=1e-6;
const int INF=0x5fffffff;
const int N=103;
const int M=10003;
int a[N];
int dp[M];
void packComplete(int cost,int weight,int V)
{
for(int v=cost;v<=V;++v)
dp[v]=min(dp[v],dp[v-cost]+weight);
}
int main()
{
//freopen("data.in","r",stdin);
int T;
cin>>T;
while(T--)
{
int V=10000;
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i)
cin>>a[i];
sort(a,a+n);
int ans=INF;
for(int i=1;i<=V;++i) dp[i]=INF;
dp[0]=0;
for(int i=0;i<n-1;++i)
packComplete(a[i],1,V);
for(int i=0;i<=V;++i)
if(dp[i]!=INF&&(m-i)%a[n-1]==0)
ans=min(ans,dp[i]+(m-i)/a[n-1]);
if(ans==INF)
cout<<"-1"<<endl;
else
cout<<ans<<endl;
}
return 0;
}