提交时间:2026-10-29 12:59:31

运行 ID: 43361

#include <bits/stdc++.h> using namespace std; const int N=1e6+9; int T,n,A,a[30],ans; bool v,s[N]; struct node{ int x,i,y; }; queue<node>b; int main(){ cin>>T; while(T--){ cin>>n>>A; while(!b.empty())b.pop(); for(int i=0;i<=A;i++)s[i]=0; v=0; for(int i=1;i<=n;i++)cin>>a[i]; sort(a+1,a+n+1); for(int i=1;i<=n;i++)b.push({A%a[i],i,1}),s[A%a[i]]=1; while(!b.empty()){ node x=b.front(); b.pop(); if(x.x==0){ v=1; cout<<x.y<<endl; break; } else for(int i=1;i<x.i;i++)if(s[x.x%a[i]]==0)b.push({x.x%a[i],i,x.y+1}),s[x.x%a[i]]=1; } if(v==0)cout<<-1<<endl; } return 0; }