Advertisement
shawon_majid

DP varient 3

Apr 30th, 2022
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. //Bismillahir Rahman-ir Rahim
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define debug(x) cout << '>' << #x << " : " << x << endl;
  5. #define all(c) c.begin(), c.end()
  6. #define F first
  7. #define S second
  8. #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  9. typedef unsigned long long ull;
  10. typedef long long ll;
  11.  
  12. /*
  13.     DP variant 3
  14. Check if it is possible to make sum of coins equal to n
  15. Now, you cannot use any coin more than once
  16.  
  17. */
  18.  
  19. vector <int> coins;
  20. int possible[100][10000]; // [numberOfCoins][sum]
  21.  
  22.  
  23.  
  24. int main(){
  25.  
  26.     int m;
  27.     cin >> m;
  28.     coins.resize(m+1);
  29.     for(int i = 1; i <= m; ++i){
  30.        
  31.         cin >> coins[i];
  32.        
  33.     }
  34.  
  35.     int n;
  36.     cin >> n;
  37.  
  38.  
  39.     possible[0][0] = 1;
  40.  
  41.     for(int j = 1; j <= m; j++){
  42.         for(int i = 1; i <= n; i++){
  43.             if(possible[j-1][i] || (i >= coins[j] && possible[j-1][i-coins[j]])){
  44.                 possible[j][i] = 1;
  45.             }
  46.         }
  47.     }
  48.  
  49.     if(possible[m][n]){
  50.         cout << "possible" << endl;
  51.     }
  52.     else cout << "not possible" << endl;
  53.  
  54.  
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement