二进制枚举即可:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int cmp(int a,int b){ return a > b; } int main(){ int t; scanf("%d",&t); while(t--){ int ans = 25; int a[10010]; int n,m; scanf("%d%d",&m,&n); for(int i = 0;i<m;i++) scanf("%d",&a[i]); sort(a,a+m,cmp); for(int i = 0;i<(1<<m);i++){ int nn = n; int ss = 0; for(int j = 0;j<m;j++){ if(i & (1<<j)){ nn = nn % a[j]; ss++; } } if(nn == 0){ ans = min(ans,ss); } } printf("%d\n",ans==25?-1:ans); } }