<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>

int wei(unsigned long long a){
    int cnt = 0;
    while(a){
        a /= 10;
        cnt++;
    }
    return cnt;
}
long long reverse(unsigned long long a,bool keepEnd){
    string s = std::to_string(a);
    int size = s.length();
    string tail;
    for(int i = 0;i < size;++i){
        if(i == size-1 && keepEnd)
            break;
        tail.insert(tail.begin() , s[i]);
    }
    s += tail;
    return atoll(s.c_str());
}
unsigned long long num(unsigned long long a){

    int bit = wei(a);
    int tmp = a / (pow(10,bit/2));
    if(bit % 2 == 0){
        int cnt1 = wei(tmp);
        tmp++;
        int cnt2 = wei(tmp);
        if(cnt2 > cnt1){
            return reverse(tmp,true);
        }
        else{
            return reverse(tmp,false);
        }
    }
    else{
        int cnt1 = wei(tmp);
        tmp++;
        int cnt2 = wei(tmp);
        if(cnt2 > cnt1){
            return reverse(tmp/10,false);
        }
        else{
            return reverse(tmp,true);
        }
    }


}
bool isPalindrome(unsigned long long a){
    vector<char> nList;
    while(a){
        nList.push_back(a % 10);
        a /= 10;
    }
    int size = nList.size();
    int i = 0;
    int j = size-1;
    while(i<j){
        if(nList[i] != nList[j])
            return false;
        i++;
        j--;
    }
    return true;
}
unsigned long long adjust(unsigned long long a){
    vector<int> nList;
    unsigned long long bak = a;
    while(a){
        nList.insert(nList.begin(),a % 10);
        a /= 10;
    }
    unsigned long long tmp = 0;
    int size = nList.size();
    int start = 0;
    if(size % 2== 0){
        start = size/2;
    }
    else{
        start = size/2+1;
    }
    for(;start<size;++start){
        nList[start] = nList[size-1-start];
    }
    for(int i = 0;i < size; ++i){
        tmp = tmp*10 + nList[i];
    }
    if(tmp > bak){
        return tmp;
    }
    else{
        return bak;
    }
}
class Solution {
public:
    int superpalindromesInRange(string left, string right) {
        unsigned long long a = atoll(left.c_str());
        unsigned long long b = atoll(right.c_str());
        unsigned long long roota = sqrt(a);
        if(roota*roota != a)
            roota++;
        unsigned long long rootb = sqrt(b);
        auto cur = roota;
        int cnt = 0;
        cur = adjust(cur);
        if(isPalindrome(cur)){
            if(isPalindrome(cur*cur)){
                cur = num(cur);
                cnt++;
            }
        }
        else{
            cur = num(cur);
        }
        while(cur<=rootb){
            if(isPalindrome(cur*cur)){
                cnt++;
            }
            cur = num(cur);
        }
        return cnt;
    }
};
163800