跳到正文

个人记录

使用 unsigned long long 写出 int128

cancan123456约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月15日
更新
2026年8月10日
篇幅
约 9 分钟

版本

  1. v1当前

众所周知,unsigned long long 是 64 位的,而有些凉心出题人会造数据炸 unsigned long long,所以我们可以手写一个 __uint128。

以下将 unsigned long long 称为 ull

一、存储

一个 ull 存高位,一个 ull 存低位,然后写几个构造函数。

C++
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,然后做加法,最后判断进位就行了。

C++
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 补码。

C++
uint128 operator - (const uint128 & a, const uint128 & b) {
	return a + (uint128(~b.high, ~b.low) + 1);
}

四、乘法

我们先实现两个 ull 相乘得到 uint128,还是拆分成高 32 位和低 32 位,对应相乘即可。

C++
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);
}

ahah 表示 aa 的高 64 位,alal 表示 bb 的低 64 位。

a×b=(264ah+al)(264bh+bl)=2128ah×bh+264(ah×bl+al×bh)+al×bla\times b=(2^{64}ah+al)(2^{64}bh+bl)=2^{128}ah\times bh+2^{64}(ah\times bl+al\times bh)+al\times bl

然后因为我们写得是 uint128,所以要  mod 2128\bmod2^{128},然后变为:

a×b mod 2128=264(ah×bl+al×bh mod 264)+al×bl mod 2128a\times b\bmod2^{128}=2^{64}(ah\times bl+al\times bh\bmod2^{64})+al\times bl\bmod2^{128}

然后自然溢出就行了。

C++
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 位拆开,然后算就行了。

C++
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 我们可以使用试除法。

C++
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;
	}
}

六、取模

与除法类似。

C++
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;
	}
}

结束

结束

结束

结束

结束

结束

结束

结束

原文
存档评论2 条抓取于