翻翻故纸堆,偶然找出这么段程序,算法实现小BT,供各位玩赏
#define BitsOf(type) (8*sizeof(type))
typedef unsigned Int; // define your type of UNSIGNED integer.
inline int FastEstimateLeading0s(Int a)
{
if (a) {
int i = 0;
for(int j = BitsOf(Int) / 2; !(a >> j); j>>=1)
i += j;
return i;
}
return BitsOf(Int);
}
inline int EvenRound(int i)
{
return (i | 1) - 1;
}
Int isqrt(Int a)
{
int i = FastEstimateLeading0s(a);
a <<= i = EvenRound(i);
int rem = 0, root = 0;
for(int k = (BitsOf(Int) - i) >> 1; k; --k)
{
root <<= 1;
rem = (rem << 2) + ( a >> (BitsOf(Int) - 2));
a <<= 2;
if (root < rem)
rem -= (++ root) ++;
}
return root >> 1;
}