<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 l,r;
  int p;
  int dp[64][2][2][3];
  int rec(int level,int tlo,int thi ,int prev){
    if(level==p){
        return 1;
    }

    if(dp[level][tlo][thi][prev]!=-1){
        return dp[level][tlo][thi][prev];
    }
    int lo = 0,hi = 1;
    if(tlo){
        lo = l[level] - '0';
    }
    if(thi){
        hi = r[level] - '0';
    }
    int ans = 0;
    //cout<<lo<<" "<<hi<<endl;
    for(int i=lo;i<=hi;i++){
        int ntlo = tlo;
        int nthi = thi;
        if(i!=l[level]-'0'){
            ntlo = 0;
        }
        if(i!=r[level]-'0'){
            nthi = 0;
        }
        if(i==1 && prev==2){
            continue;
        }
        ans = ans+rec(level+1,ntlo,nthi,i+1);
    }
    dp[level][tlo][thi][prev]= ans;
    return ans;
  }
    int findIntegers(int n) {
        r = "";
        while(n){
            r.push_back(n%2 + '0');
            n = n/2;
            l .push_back('0');
        }
        p = r.length();
        //cout<<n<<endl;
        reverse(r.begin(),r.end());
        cout<<l<<" "<<r<<endl;
        memset(dp,-1,sizeof(dp));
       // rec(0,1,1,0);
        return rec(0,1,1,0);      
        
    }
};
9100