众所周知,unsigned long long 是 64 位的,而有些凉心出题人会造数据炸 unsigned long long,所以我们可以手写一个 __uint128。
以下将 unsigned long long 称为 ull
一、存储
一个 ull 存高位,一个 ull 存低位,然后写几个构造函数。
typedef unsigned long long ull;
struct uint128 {
ull high, low;
uint128(ull a) {
high = 0;
low = a;
}
uint128(ull high_, ull low_) {
high = high_;
low = low_;
}
};二、加法
发现两个 low 加起来有可能产生进位,也就是溢出了。
所以我们把两个低位拆成四个 unsigned int,然后做加法,最后判断进位就行了。
const ull high32 = 0xFFFFFFFF00000000ULL;
const ull low32 = 0x00000000FFFFFFFFULL;
uint128 operator + (const uint128 & a, const uint128 & b) {
ull alh = (a.high & high32) >> 32;
ull all = a.low & low32;
ull blh = (b.high & high32 >> 32);
ull bll = b.low & low32;
all = all + bll;
alh = alh + blh + ((all & high32) >> 32);
return uint128(a.high + b.high + ((alh & high32) >> 32), ((alh & low32) << 32) | (all & low32));
}三、减法
将 b 取反后加一,然后计算 a+b 即可,不知道为啥的自行 bdfs 补码。
uint128 operator - (const uint128 & a, const uint128 & b) {
return a + (uint128(~b.high, ~b.low) + 1);
}四、乘法
我们先实现两个 ull 相乘得到 uint128,还是拆分成高 32 位和低 32 位,对应相乘即可。
uint128 mul(const ull & a, const ull & b) {
ull ah = a & high32;
ull al = a & low32;
ull bh = b & high32;
ull bl = b & low32;
ull ahbh = ah * bh;
ull ahbl = ah * bl;
ull albh = al * bh;
ull albl = al * bl;
return uint128(ahbh, 0) + uint128((ahbl & high32) >> 32, (ahbl & low32) << 32) + uint128((albh & high32) >> 32, (albh & low32) << 32) + uint128(0, albl);
}表示 的高 64 位, 表示 的低 64 位。
然后因为我们写得是 uint128,所以要 ,然后变为:
然后自然溢出就行了。
uint128 operator * (const uint128 & a, const uint128 & b) {
return mul(a.low, b.low) + uint128(a.low * b.high + a.high * b.low, 0);
}五、除法
最慢的就是这个除法。
首先 uint128 除以 ull 可以快速求出,把低 64 位拆开,然后算就行了。
uint128 operator / (const uint128 & a, const ull & b) {
ull alh = (a.low & high32) >> 32;
ull all = a.low & low32;
ull temp = (a.high % b) << 32 | alh;
return uint128(a.high / b, ((temp / b) << 32) | (((temp % b) << 32) | all) / b);
}然后 uint128 除以 uint128 我们可以使用试除法。
uint128 operator << (const uint128 & a, const int & bit) {
return uint128((a.high << bit) | (a.low & (((1ULL << (64 - bit)) - 1) << bit)), a.low << bit);
}
uint128 operator >> (const uint128 & a, const int & bit) {
return uint128(a.high >> bit, ((a.high & ((1ULL << bit) - 1)) << (64 - bit)) | (a.low >> bit));
}
bool operator < (const uint128 & a, const uint128 & b) {
return a.high == b.high ? a.low < b.low : a.high < b.high;
}
bool operator > (const uint128 & a, const uint128 & b) {
return a.high == b.high ? a.low > b.low : a.high > b.high;
}
bool operator == (const uint128 & a, const uint128 & b) {
return a.high == b.high && a.low == b.low;
}
bool operator != (const uint128 & a, const uint128 & b) {
return a != b;
}
bool operator <= (const uint128 & a, const uint128 & b) {
return a < b || a == b;
}
bool operator >= (const uint128 & a, const uint128 & b) {
return a > b || a == b;
}
uint128 operator / (const uint128 & a, const uint128 & b) {
if (a == 0) {
return 0;
} else if (b == 0) {
printf("div by 0.\n");
throw;
} else {
int highbit_a = 0;
for (int i = 127; i >= 0; i--) {
if ((a >> i) != 0) {
highbit_a = i;
break;
}
}
int highbit_b = 0;
for (int i = 127; i >= 0; i--) {
if ((b >> i) != 0) {
highbit_b = i;
break;
}
}
uint128 ans = 0;
uint128 t = a;
for (int i = highbit_b - highbit_a; i >= 0; i--) {
if (t > (b << i)) {
t = t - (b << i);
ans = ans | (1 << i);
}
}
return ans;
}
}六、取模
与除法类似。
ull operator % (const uint128 & a, const ull & b) {
return ((((((a.high % b) << 32) | ((a.low & high32) >> 32)) % b) << 32) | (a.low & low32)) % b;
}
uint128 operator % (const uint128 & a, const uint128 & b) {
if (a == 0) {
return 0;
} else if (b == 0) {
printf("div by 0.\n");
throw;
} else {
int highbit_a = 0;
for (int i = 127; i >= 0; i--) {
if ((a >> i) != 0) {
highbit_a = i;
break;
}
}
int highbit_b = 0;
for (int i = 127; i >= 0; i--) {
if ((b >> i) != 0) {
highbit_b = i;
break;
}
}
uint128 t = a;
for (int i = highbit_b - highbit_a; i >= 0; i--) {
if (t > (b << i)) {
t = t - (b << i);
}
}
return t;
}
}结束
结束
结束
结束
结束
结束
结束
结束