fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int INF = 2000000000,A[5005],dist[50005];
  4. set < pair<int,int> > dijk;
  5. int main()
  6. {
  7. // freopen("input.txt", "r", stdin);
  8. int n;
  9. scanf("%d", &n);
  10. for (int i = 0; i < n; ++i)
  11. {
  12. scanf("%d", &A[i]);
  13. }
  14. for (int i = 0; i < A[0]; ++i)
  15. {
  16. dist[i] = INF;
  17. }
  18. dist[0] = 0;
  19. dijk.insert(make_pair(dist[0],0));
  20. while(!dijk.empty())
  21. {
  22. pair<int,int> top = *dijk.begin();
  23. dijk.erase(top);
  24. int v = top.second;
  25. int d = top.first;
  26. for (int i = 1; i < n; ++i)
  27. {
  28. int d2 = d+A[i];
  29. int v2 = (v+A[i])%A[0];
  30. if(d2 < dist[v2])
  31. {
  32. if(dist[v2] < INF)
  33. dijk.erase(make_pair(dist[v2],v2));
  34. dist[v2] = d2;
  35. dijk.insert(make_pair(dist[v2],v2));
  36. }
  37. }
  38. }
  39. int k;
  40. scanf("%d", &k);
  41. while(k--)
  42. {
  43. int x;
  44. scanf("%d", &x);
  45. if(dist[x%A[0]] <= x)
  46. printf("TAK\n");
  47. else
  48. printf("NIE\n");
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 3492KB
stdin
3
2
5
7
6
0
1
4
12
3
2
stdout
TAK
NIE
TAK
TAK
NIE
TAK