https://codeforces.com/gym/100753/problem/M
Can't think of the classic template question https://blog.csdn.net/liufengwei1/article/details/103997675
This question is more template than that question, but the complexity of this question is O(ailogai+ai*n), n=5e4 is obviously not easy to pass. As a result, I asserted that n<=5e3. . .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxl=5e4+10;
const ll inf=1ll<<60;
int n,m;
//struct ed{int v,l;};
//vector<ed>e[maxl];
int a[maxl];ll dis[maxl];
typedef pair<ll,int> p;
priority_queue<p,vector<p>,greater<p> >q;
inline void prework()
{
scanf("%d",&n);
assert(n<=5000);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
}
inline void mainwork()
{
for(int i=1;i<a[1];i++)
dis[i]=inf;
dis[0]=0;q.push({dis[0],0});
while(!q.empty())
{
p d=q.top();q.pop();
int u=d.second;
if(dis[u]!=d.first)
continue;
for(int i=2;i<=n;i++)
{
int v=(u+a[i])%a[1];
if(dis[v]>dis[u]+a[i])
{
dis[v]=dis[u]+a[i];
q.push({dis[v],v});
}
}
}
}
inline void print()
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int b;
scanf("%d",&b);
puts((dis[b%a[1]]<=b)?"TAK":"NIE");
}
}
int main()
{
prework();
mainwork();
print();
return 0;
}