Advertisement
esraa_syam

bad problem

Apr 8th, 2024
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. #define nl "\n"
  5. #define ll long long
  6. #define all(v) v.begin(), v.end()
  7. #define rall(v) v.rbegin(), v.rend()
  8. #define sz(v) (int) v.size()
  9.  
  10. template<typename T = int>
  11. istream &operator>>(istream &in, vector<T> &v) {
  12.     for (auto &x: v) in >> x;
  13.     return in;
  14. }
  15.  
  16. template<typename T = int>
  17. ostream &operator<<(ostream &out, const vector<T> &v) {
  18.     for (const T &x: v) out << x << " ";
  19.     return out;
  20. }
  21.  
  22. void Sira() {
  23.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  24. #ifndef ONLINE_JUDGE
  25.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28. int n;
  29. const int N = 1e6 + 5;
  30. int dp[N][4];
  31. int rec(int sum , int one){
  32.     // base case;
  33.     if(one > 3) return 0;
  34.     if(sum >= n) return sum == n and !one;
  35.  
  36.     // memo
  37.     int & ret = dp[sum][one];
  38.     if(~ret) return ret;
  39.  
  40.     // rec
  41.  
  42.     ret = 0;
  43.  
  44.     if(!one){
  45.         ret += rec(sum + 2 , 2);
  46.         ret += rec(sum + 1 , one + 1);
  47.     }
  48.  
  49.     ret += rec(sum + 2 , 0);
  50.  
  51.     return ret;
  52.  
  53. }
  54. void solve(){
  55.     cin >> n;
  56.  
  57.     for(auto & i : dp){
  58.         memset(i , -1 , sizeof i);
  59.     }
  60.  
  61.     cout << rec(1 , 1) + rec(2 , 0) << nl;
  62.  
  63. }
  64.  
  65. int main() {
  66.     Sira();
  67.     int t = 1;
  68. //    cin >> t;
  69.     while(t--){
  70.         solve();
  71.     }
  72.     return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement