Advertisement
Guest User

Untitled

a guest
Oct 27th, 2019
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n; cin >> n;
  7.     vector<int> a(n);
  8.     for (int i = 0; i < n; ++i) {
  9.         cin >> a[i];
  10.     }
  11.  
  12.     int w = a[0];
  13.     vector<int> dp(w, 2e9);
  14.     priority_queue<pair<int, int>> pq;
  15.     auto push = [&](int node, int dist) {
  16.         if (dp[node] <= dist) return;
  17.         dp[node] = dist;
  18.         pq.emplace(-dist, node);
  19.     };
  20.     push(0, 0);
  21.  
  22.     while (!pq.empty()) {
  23.         int node, dist; tie(dist, node) = pq.top(); pq.pop(); dist = -dist;
  24.         if (dp[node] != dist) continue;
  25.         for (int i = 0; i < n; ++i) {
  26.             push((node + a[i]) % w, dist + a[i]);    
  27.         }
  28.     }
  29.    
  30.     int q; cin >> q;
  31.     while (q--) {
  32.         int x; cin >> x;
  33.         int ans = dp[x % w];
  34.         if (ans <= x) {
  35.             cout << "TAK" << '\n';
  36.         } else {
  37.             cout << "NIE" << '\n';
  38.         }
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement