Advertisement
Nita_Cristian

monezi2

Feb 4th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("monezi2.in");
  6. ofstream fout("monezi2.out");
  7.  
  8. int n, q, x, suma, p, indice;
  9. int v[100001];
  10. bool s[100001];
  11.  
  12. int main()
  13. {
  14.     fin >> n;/// citesc numarul de monezi
  15.     for(int i = 1; i <= n; i++)
  16.         fin >> x, v[i] = v[i-1] + x; // citesc monezile si calculez vectorul de sume partiale
  17.  
  18.     s[0] = 1;// suma 0 se poate obtine neadunand nici un element
  19.     for(int i = 1; i <= n; i++)// iau fiecare suma
  20.     {
  21.         suma = v[i], indice = 0;
  22.         while(indice + suma <= 100000)// cat timp nu am depasit suma maxima care poate exista
  23.         {
  24.             if(s[indice])// daca pot forma suma din indice
  25.                 s[indice + suma] = 1;// atunci se poate forma si suma din indice + suma
  26.             indice++;
  27.         }
  28.     }
  29.  
  30.     fin >> q;
  31.     for(int i = 1; i <= q; i++)
  32.     {
  33.         fin >> p;
  34.         if(s[p])
  35.             fout << "DA" << '\n';
  36.  
  37.         else
  38.             fout << "NU" << '\n';
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement