<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 a)
{
    string binary = "";
    int mask = 1;
    for(int i = 0; i < 31; i++)
    {
        if((mask&a) >= 1)
            binary = "1"+binary;
        else
            binary = "0"+binary;
        mask<<=1;
    }
    // cout<<binary<<endl;
    return binary;
}
    int dp[64][2][3];
    int f(string &num,int pos, int tight,int lastPick){
        if(pos==num.size())
            return 1;
        if(dp[pos][tight][lastPick+1]!=-1)return dp[pos][tight][lastPick+1];
        int res=0;
        int limit=(tight)?num[pos]-'0':1;

        for(int digit=0;digit<=limit;digit++){
            if(lastPick==1&&digit==1)continue;
            res+= f(num,pos+1,tight&& (digit==limit),digit);
        }


        return dp[pos][tight][lastPick+1] = res;
    }
    int findIntegers(int n) {
        
        string num= binary(n);
        memset(dp,-1,sizeof(dp));
        return f(num,0,1,-1);  // string, pos, tight 
          
    }
};
10800