<p>Given a positive integer <code>n</code>, return the number of the integers in the range <code>[0, n]</code> whose binary representations <strong>do not</strong> contain consecutive ones.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong>
Here are the non-negative integers &lt;= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

// class Solution {
// public:
//     const int MOD = 1e9 + 7;
//     unordered_map<string, int> memoSol;
//     unordered_map<string, int> memoSolve;

//     int sol(string& s, int idx, int len, int n) {
//         if (idx >= len) {
//             int number = 0;
//             for (char bit : s) {
//                 number <<= 1;
//                 if (bit == '1') {
//                     number += 1;
//                 }
//             }
//             return number <= n ? 1 : 0;
//         }

//         string key = s + to_string(idx);
//         if (memoSol.find(key) != memoSol.end()) {
//             return memoSol[key];
//         }

//         int ans = 0;
//         if (s.back() == '1') {
//             s.push_back('0');
//             ans = (ans + sol(s, idx + 1, len, n)) % MOD;
//             s.pop_back();
//         } else {
//             s.push_back('1');
//             ans = (ans + sol(s, idx + 1, len, n)) % MOD;
//             s.pop_back();
//             s.push_back('0');
//             ans = (ans + sol(s, idx + 1, len, n)) % MOD;
//             s.pop_back();
//         }

//         memoSol[key] = ans;
//         return ans;
//     }

//     int solve(int idx, int len, int val) {
//         if (idx >= len) {
//             return 1;
//         }

//         string key = to_string(idx) + "-" + to_string(val);
//         if (memoSolve.find(key) != memoSolve.end()) {
//             return memoSolve[key];
//         }

//         int ans = 0;
//         if (val == 1) {
//             ans = (ans + solve(idx + 1, len, 0)) % MOD;
//         } else {
//             ans = (ans + solve(idx + 1, len, 1)) % MOD;
//             ans = (ans + solve(idx + 1, len, 0)) % MOD;
//         }

//         memoSolve[key] = ans;
//         return ans;
//     }

//     int findIntegers(int n) {
//         string s = "";
//         int temp = n;
//         while (temp > 0) {
//             s = (char)('0' + (temp % 2)) + s;
//             temp /= 2;
//         }
//         int ans = 0;
//         for (int i = 0; i < s.size(); i++) {
//             ans = (ans + solve(1, i, 1)) % MOD;
//         }
//         string g = "1";
//         ans = (ans + sol(g, 1, (int)s.size(), n)) % MOD;
//         return ans;
//     }
// };
#include <vector>
#include <string>

class Solution {
public:
    vector<vector<vector<int>>> memo;

    int dp(int pos, bool tight, bool prev_one, const string &binary) {
        if (pos == binary.size()) return 1;

        if (memo[pos][tight][prev_one] != -1) return memo[pos][tight][prev_one];

        int limit = tight ? binary[pos] - '0' : 1;
        int ans = 0;

        ans += dp(pos + 1, tight && (limit == 0), false, binary);

        if (!prev_one && limit >= 1) {
            ans += dp(pos + 1, tight && (limit == 1), true, binary);
        }

        return memo[pos][tight][prev_one] = ans;
    }

    int findIntegers(int n) {
        string binary = bitset<32>(n).to_string();
        int len = binary.size();

        int first_one = binary.find('1');
        binary = binary.substr(first_one);

        memo = vector<vector<vector<int>>>(len, vector<vector<int>>(2, vector<int>(2, -1)));

        return dp(0, true, false, binary);
    }
};

11900