Advertisement
shawon_majid

dp 3

Apr 30th, 2022
963
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 dp[10000][100];
  21.  
  22.  
  23. int possible(int n, int i){
  24.     if(n == 0){
  25.         return true;
  26.     }
  27.     else if(n != 0 and i < 0) return false;
  28.     else if(dp[n][i] != -1) return dp[n][i];
  29.     return dp[n][i] = possible(n, i-1) || possible(n-coins[i], i-1);
  30. }
  31.  
  32.  
  33. int main(){
  34.  
  35.     int m;
  36.     cin >> m;
  37.     coins.resize(m);
  38.     memset(dp, -1, sizeof dp);
  39.     for(int i = 0; i < m; ++i){
  40.        
  41.         cin >> coins[i];
  42.        
  43.     }
  44.  
  45.     int n;
  46.     cin >> n;
  47.  
  48.  
  49.     if(possible(n, m-1)){
  50.         cout << "Possible" << endl;
  51.     }
  52.     else cout << "Not Possible" << endl;
  53.    
  54.  
  55.  
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement