当n是完全平方数时,返回它的开方,否则返回-1。
ll sqt(ll n) {
ll lo = 0, hi = 2 * sqrt(n);
while(lo < hi) {
ll mid = lo + hi + 1 >> 1;
if(mid * mid <= n) {
lo = mid;
} else {
hi = mid - 1;
}
}
return lo * lo == n ? lo : -1;
}
标签:lo,ll,mid,sqrt,hi,手写
From: https://www.cnblogs.com/cenqi/p/17615013.html