Advertisement
Josif_tepe

Untitled

Feb 18th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int dp[20];
  8. int rec(int x) {
  9.     if(x <= 2) {
  10.         return 1;
  11.     }
  12.    
  13.     if(dp[x] != -1) {
  14.         return dp[x];
  15.     }
  16.    
  17.     return dp[x] = rec(x - 1) + rec(x - 2);
  18. }
  19. int main()
  20. {
  21.     memset(dp, -1, sizeof dp);
  22.     cout << rec(8) << endl;
  23.  
  24.     return 0;
  25. }
  26.  
  27. // rec(5) = rec(4) +  rec(3)  = 3 + 2 = 5
  28. // rec(4) = rec(3) + rec(2) = 2 + 1 = 3
  29. // rec(3) = rec(2) + rec(1) = 1 + 1 = 2
  30. // rec(2) = 1
  31. // rec(1) = 1
  32. // rec(2) = 2
  33. // rec(3) = rec(2) + rec(1) = 1 + 1 = 2
  34. // rec(2) = 1
  35. // rec(1) = 1
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement