給你一個大小爲n的集合S,集合裏有n個互不相同正整數.
有q個詢問,每次詢問是否能選擇S中的一些數字 ( 同一個數字可以選擇多次,也可以任何數字都不選),使它們相加的和爲m.
Input
第一行一個數n(1≤n≤2000),表示集合S的大小.第二行n個數,第i個數ai(1≤ai≤50000)表示集合S中的第i個數.
第三行一個數q(1≤q≤10000),表示詢問次數.
接下來q行,每行一個數m(0≤m≤1000000000),表示該次詢問的數.
Output
每次詢問輸出一行,如果存在和爲m的方法,輸出YES,否則輸出NO.Sample Input
3 2 4 9 4 6 7 18 25Sample Output
YES NO YES YES
//分析:一樣的套路,取a1作爲模數,然後跑最短路,對於詢問中的一個數x,如果d[x % a[1]] > x則無解.
//
#include <bits/stdc++.h>
#define N 50005#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
ll n,x,Q,d[N],a[N];
bool done[N];
priority_queue< pii,vector<pii>,greater<pii> > q;
int main()
{
cin.sync_with_stdio(false);
cin>>n;
for(int i = 1;i <= n;i++) cin>>a[i];
sort(a+1,a+1+n);
memset(d,0x3f,sizeof(d));
memset(done,0,sizeof(done));
d[0] = 0;
q.push(make_pair(d[0],0));
while(!q.empty())
{
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = 1;i <= n;i++)
if(d[(a[i]+u) % a[1]] > d[u] + a[i])
{
d[(a[i]+u) % a[1]] = d[u] + a[i];
q.push(make_pair(d[(a[i]+u) % a[1]],(a[i]+u) % a[1]));
}
}
cin>>Q;
for(int i = 1;i <= Q;i++)
{
cin>>x;
if(d[x % a[1]] <= x) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}