<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:
    string binary(int n)
    {
        string s = "";
        while(n>0)
        {
            int a = n%2;
            s+=to_string(a);
            n/=2;
        }
        reverse(s.begin(),s.end());
        return s;
    }
    int dp[32][2][2];
    int rec(string &num,int n,bool tight,bool one)
    {
        if(n==0)
        {
            return 1;
        }
        if(dp[n][tight][one]!=-1)
        {
            return dp[n][tight][one];
        }
        int ans=0;
        int ub = (tight)?num[num.size()-n]-'0':1;
        for(int dig=0;dig<=ub;dig++)
        {
            if(one and dig==1)
            {
                continue;
            }
            else if(one and dig==0)
            {
                ans+=rec(num,n-1,tight&(dig==ub),0);
            }else if(!one and dig==1)
            {
               ans+=rec(num,n-1,tight&(dig==ub),1);
            }else
            {
                ans+=rec(num,n-1,tight&(dig==ub),0);
            }
        }
        return dp[n][tight][one]=ans;
    }
    int findIntegers(int n) {
        string num = binary(n);
        memset(dp,-1,sizeof(dp));
        return rec(num,num.size(),1,0);
    }
};
10500