Advertisement
FHVirus

Untitled

Dec 10th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. inline int hsh(vi vec){
  2.     int ans = 0;
  3.     for(int v: vec)
  4.         ans = ans * 50 + v;
  5.     return ans;
  6. }
  7.  
  8. int main(){
  9.     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10.     while(cin >> n){
  11.         for(int i = 0; i < n; ++i)
  12.             cin >> m[i];
  13.         cin >> t;
  14.  
  15.         bool oao = false;
  16.         for(int i = 0; i < n; ++i)
  17.             if(m[i] >= t) oao = true;
  18.         if(!oao){
  19.             cout << -1 << '\n';
  20.             continue;
  21.         }
  22.  
  23.         int g = 0;
  24.         for(int i = 0; i < n; ++i)
  25.             g = gcd(g, m[i]);
  26.         if(t % g){
  27.             cout << -1 << '\n';
  28.             continue;
  29.         }
  30.  
  31.         bool ans = false;
  32.  
  33.         queue<piv> q;
  34.         unordered_set<int> vis;
  35.         q.push({0, vi(n,0)});
  36.         vis.insert(hsh(vi(n,0)));
  37.  
  38.         while(!ans and !q.empty()){
  39.             int stp = q.front().ff;
  40.             vi jiz = q.front().ss;
  41.             q.pop();
  42.  
  43.             // cout << "Step: " << stp << '\n';
  44.             // for(int i = 0; i < n; ++i)
  45.             //  cout << jiz[i] << ' ';
  46.  
  47.             for(int i = 0; i < n; ++i)
  48.                 if(jiz[i] == t){
  49.                     ans = true;
  50.                     cout << stp << '\n';
  51.                     break;
  52.                 }
  53.  
  54.             // empty one cup
  55.             for(int i = 0; i < n; ++i){
  56.                 vi eek = jiz;
  57.                 eek[i] = 0;
  58.                 if(vis.count(hsh(eek)) == 0)
  59.                     vis.insert(hsh(eek)), q.push({stp + 1, eek});
  60.             }
  61.  
  62.             // fill one cup
  63.             for(int i = 0; i < n; ++i){
  64.                 vi eek = jiz;
  65.                 eek[i] = m[i];
  66.                 if(vis.count(hsh(eek)) == 0)
  67.                     vis.insert(hsh(eek)), q.push({stp + 1, eek});
  68.             }
  69.  
  70.             // pour from i to j
  71.             for(int i = 0; i < n; ++i){
  72.                 for(int j = 0; j < n; ++j){
  73.                     if(i == j) continue;
  74.                     vi eek = jiz;
  75.                     // fill up j
  76.                     if(m[j] - eek[j] <= eek[i]){
  77.                         eek[i] -= m[j] - eek[j];
  78.                         eek[j] = m[j];
  79.                     }
  80.                     // emtpy i
  81.                     else {
  82.                         eek[j] += eek[i];
  83.                         eek[i] = 0;
  84.                     }
  85.                     if(vis.count(hsh(eek)) == 0)
  86.                         vis.insert(hsh(eek)), q.push({stp + 1, eek});
  87.                 }
  88.             }
  89.         }
  90.         if(!ans) cout << -1 << '\n';
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement