#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
ll wei(ll k)
{
ll cnt=0;
while(k)
{
cnt++;
k/=10;
}
return cnt;
}
ll p[20];
void solve(){
int x;
cin >> x;
int sq = sqrt(x);
if(sq * sq == x){
cout << sq << '\n';
return;
}
__int128 t = x;
for(int i = 1; i <= 18; i++){
t = t * 10 + 9;//往后面加9不加0,如1230,开根再平方后结果小于1230,前缀就不是123,1239开根再平方就有可能前缀是123
int sq = sqrt(double(t));
if(sq > 1e9){
cout << -1 << '\n';
return;
}
int square = sq * sq;
if(square / p[i] == x){
cout << sq << '\n';
return;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
p[0] = 1;
for(int i = 1; i <= 18; i++) p[i] = p[i - 1] * 10;
int T;
cin >> T;
while(T--){
solve();
}
}
Square
于 2023-11-07 17:08:29 首次发布