<p>Let&#39;s say a positive integer is a <strong>super-palindrome</strong> if it is a palindrome, and it is also the square of a palindrome.</p>

<p>Given two positive integers <code>left</code> and <code>right</code> represented as strings, return <em>the number of <strong>super-palindromes</strong> integers in the inclusive range</em> <code>[left, right]</code>.</p>

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

<pre>
<strong>Input:</strong> left = &quot;4&quot;, right = &quot;1000&quot;
<strong>Output:</strong> 4
<strong>Explanation</strong>: 4, 9, 121, and 484 are superpalindromes.
Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.
</pre>

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

<pre>
<strong>Input:</strong> left = &quot;1&quot;, right = &quot;2&quot;
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= left.length, right.length &lt;= 18</code></li>
	<li><code>left</code> and <code>right</code> consist of only digits.</li>
	<li><code>left</code> and <code>right</code> cannot have leading zeros.</li>
	<li><code>left</code> and <code>right</code> represent integers in the range <code>[1, 10<sup>18</sup> - 1]</code>.</li>
	<li><code>left</code> is less than or equal to <code>right</code>.</li>
</ul>

class Solution {
public:
long long low, high;
long long max_p= 1e+9;
long long high_sqrt, low_sqrt;
int ans;

// check whether a number is a palidrome or not.
bool isPali(long long k){
    vector<int> c;
    while(k>0) { c.push_back(k%10); k/=(long long)10; }
    for(int i=0;i<c.size()/2;i++){ if(c[i]!=c[c.size()-1-i]) return false;}
    return true;
}
void sub(long long base, long long left, long long right){
    
    int len= (left==1)? 3:2;
    if(left==right){ for(int i=0;i<=len;i++){ long long tmp= base+(long long)i*(left);
            if(isPali(tmp)&& isPali(tmp*tmp) && tmp<=high_sqrt && tmp>=low_sqrt) { 
                                                                                  ans++;} }
                   }
    else if(left==right*(long long)10) {
        for(int i=0;i<=len;i++){ 
            long long tmp= base+(long long)i*(left+right);
            if(isPali(tmp)&& isPali(tmp*tmp)&&tmp<=high_sqrt && tmp>=low_sqrt) 
			{    ans++;} }
    }
    else { for(long long i=0;i<=2;i++) sub(base+i*(left+right), left/10, right*10 );}
}
// binary search the number n , where n*n<k and return n
long long bst(long long k){
    
    long long left=1; long long right=max_p-1;
    while(right-left>=0){
        long long mid=(left+right)/2;
        long long tmp= mid*mid; long long tmp_plus= tmp+2*mid+1;
        if(tmp<=k&& tmp_plus>k) return mid;
        else if(tmp_plus<=k) left=mid+1;
        else                 right=mid-1;
    }
    return left;
}
int superpalindromesInRange(string left, string right) {
    low=high=0;
    long long pow=1;
    ans=0;
    for(int i=left.size()-1;i>=0;i--){ low+= (long long)(left[i]-'0')*pow; pow*=(long long)10; }
    pow=1;
    for(int i=right.size()-1;i>=0;i--){ high+= (long long)(right[i]-'0')*pow; pow*=(long long)10; }
    low_sqrt= bst(low);
    high_sqrt= bst(high);
    pow=1;
    while(pow<=high_sqrt){
        sub(0,pow,1);
        pow*=(long long)10;
    }
    return ans;
}
};
11800