需要10^30才可以存,我们可以用__int128来算
#include<bits/stdc++.h>
using namespace std;
bool is_sqr(__int128 x) {
__int128 l = -1, r = 1e16;
while (r - l > 1) {
__int128 m = (l + r) / 2;
__int128 m2 = m * m;
if (m2 <= x) {
l = m;
} else {
r = m;
}
}
if (l * l == x) {
return true;
} else {
return false;
}
}
long long cnt(__int128 x) { // >=x
__int128 l = -1, r = 1e16;
while (r - l > 1) {
__int128 m = (l + r) / 2;
__int128 m2 = m * m;
if (m2 >= x) {
r = m;
} else {
l = m;
}
}
return r;
}
long long range(__int128 l, __int128 r) {
return cnt(r + 1) - cnt(l);
}
int main() {
int n;
cin >> n;
string L, R;
cin >> L >> R;
int m = n / 2;
__int128 L1 = 0, R1 = 0;
for (int i = 0; i < m; i++) {
L1 = L1 * 10 + (L[i] - '0');
R1 = R1 * 10 + (R[i] - '0');
}
__int128 L2 = 0, R2 = 0;
for (int i = m; i < n; i++) {
L2 = L2 * 10 + (L[i] - '0');
R2 = R2 * 10 + (R[i] - '0');
}
if (L1 == R1) {
if (!is_sqr(L1)) {
cout << 0 << "\n";
return 0;
} else {
cout << range(L2, R2) << "\n";
return 0;
}
}
__int128 base = 1;
for (int i = 0; i < m; i++) {
base = base * 10;
}
__int128 ans = 0;
if (is_sqr(L1)) {
ans += range(L2, base - 1);
}
ans += (__int128)range(L1 + 1, R1 - 1) * range(0, base - 1);
if (is_sqr(R1)) {
ans += range(0, R2);
}
static int st[35];
int c = 0;
do {
st[++c] = ans % 10;
ans /= 10;
} while (ans);
while (c) {
cout << st[c];
c--;
}
}