跳到正文

科技·工程

压位高精度整数 3.0

Xudongning约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月6日
篇幅
约 11 分钟
获赞
8

版本

  1. v1当前

代码

代码

操作

记 n=⌊lg⁡max⁡(a,b)+1⌋,w=8n=\lfloor\lg\max(a,b)+1\rfloor,w=8。

表达式含义时间复杂度空间复杂度备注
a.to_string()求 aa 的string表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a.to_long_long()求 aa 的long long表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a.to_binary()求 aa 的二进制表示O(n2w)O(\dfrac{n^2}{w})O(n2)O(n^2)
a.to_int128()求 aa 的__int128表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a._digit_len()求 ⌊nw⌋\lfloor \dfrac{n}{w} \rfloorO(1)O(1)O(1)O(1)
-a求 −a-aO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
~a求 −a−1-a-1O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.abs()求 ∣a∣\mid a\midO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a <=> b求大小关系O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.div2()求 ⌊a2⌋\lfloor \dfrac{a}{2} \rfloorO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.divmod(b)除法 + 取余(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a.divmod(b)除法 + 取余(牛迭)O(nlog⁡nw)O(\dfrac{n\log n}{w})O(nlog⁡nw)O(\dfrac{n\log n}{w})
a + b加法O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a - b减法O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a * b乘法(普通)O(n2w2)O(\dfrac{n^2}{w^2})O(nw)O(\dfrac{n}{w})
a * b乘法(FFT)O(nlog⁡nw)O(\dfrac{n\log n}{w})O(n)O(n)
a / b除法(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a / b除法(牛迭)O(nlog⁡nw)O(\dfrac{n \log n}{w})O(nlog⁡nw)O(\dfrac{n \log n}{w})
a % b取余(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a % b取余(牛迭)O(nlog⁡nw)O(\dfrac{n \log n}{w})O(nlog⁡nw)O(\dfrac{n\log n}{w})
a.pow(x)乘方(普通)O(n2log⁡nlog⁡xw)O(\dfrac{n^2\log n\log x}{w})O(nx)O(nx)
a.pow(x,p)乘方(取余)O(n2log⁡nlog⁡xw)O(\dfrac{n^2\log n\log x}{w})O(n)O(n)
a.root(x)求 ⌊ax⌋\lfloor \sqrt[x]{a} \rfloorO(nlog⁡nw)O(\dfrac{n \log n}{w})O(nw)O(\dfrac{n}{w})
a.sqrt()求 ⌊a⌋\lfloor \sqrt{a} \rfloorO(nlog⁡nw)O(\dfrac{n \log n}{w})O(nw)O(\dfrac{n}{w})
a.gcd(b)求 gcd⁡(a,b)\gcd(a,b)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a.lcm(b)求 lcm⁡(a,b)\operatorname{lcm}(a,b)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a & b按位与O(n2)O(n^2)O(n)O(n)
a | b按位或O(n2)O(n^2)O(n)O(n)
a ^ b按位异或O(n2)O(n^2)O(n)O(n)
a << x求 a⋅2xa\cdot 2^xO(nxw)O(\dfrac{nx}{w})O(n)O(n)
a >> x求 ⌊a2x⌋\lfloor \dfrac{a}{2^x} \rfloorO(nxw)O(\dfrac{nx}{w})O(n)O(n)
a._move_l(x)求 a⋅10wxa\cdot 10^{wx}O(nw+x)O(\dfrac{n}{w}+x)O(nw+x)O(\dfrac{n}{w}+x)
a._move_r(x)求 ⌊a10wx⌋\lfloor \dfrac{a}{10^{wx}} \rfloorO(nw−x)O(\dfrac{n}{w}-x)O(nw−x)O(\dfrac{n}{w}-x)
random_big(x)生成位数为 xx 的随机正数O(n)O(n)O(n)O(n)

优点

  • 高度封装,支持许多操作;
  • 速度较快,大部分操作已优化到较优秀的复杂度;
  • 代码较短(约 30KB)。

缺点

  • 位运算速度慢;
  • 整数位数较大时,FFT 会出现误差;
  • 其他偶然错误。

性能测试

测试信息

HTML
CPU:12th Gen Intel(R) Core(TM) i3-12100   3.30 GHz
RAM:24.0 GB
系统类型:64 位操作系统,基于 x64 的处理器
版本:Windows 11 专业版
编译器:TDM-GCC 14.2.0 64-bit Release
编译语言:C++ 23
软件:Dev-C++ 6.7.5
备注:采用文件读写的方式,运用 Windows 系统函数测速,多次测速取算数平均数,最坏情况
单位:毫秒

测试结果

10410^45×1045\times 10^410510^55×1055\times 10^510610^65×1065\times 10^610710^7
加法0.00870.00870.05460.05460.10750.10750.53580.53581.40621.40625.31545.31549.40039.4003
减法0.00540.00540.03530.03530.08510.08510.46040.46041.36051.36054.06914.06918.43798.4379
乘法0.11830.11830.86810.86811.93431.93439.17529.175218.820318.8203207.892207.892413.864413.864
除法0.45510.45511.57091.57094.60364.603610.165810.165823.342723.3427226.032226.032515.148515.148
取余0.68640.68641.01691.01692.15622.156212.08812.08831.830431.8304265.982265.982491.209491.209
开平方3.36513.365118.737118.737138.37838.378189.588189.588352.961352.961
GCD152.062152.0624930.94930.9
LCM157.421157.4214962.434962.43
位运算191.679191.6795605.275605.27

优化内容

1. 基础架构差异

  • 异常处理:
    • BigInteger 3.0 使用 std::runtime_error 作为基类
    • BigInteger 2.7 使用 std::exception 作为基类
  • 内存管理:
    • BigInteger 3.0 实现了内存池机制,使用 allocate_digits 和 deallocate_digits 方法
    • BigInteger 2.7 使用传统的 new 和 delete 直接管理内存

2. 功能实现差异

  • 构造函数和赋值操作:
    • BigInteger 3.0 提供了 operator=(const __int128&) 和 to_int128() 方法
    • BigInteger 2.7 将这些方法放在 #ifdef __SIZEOF_INT128__ 条件编译块中
  • 数学运算:
    • BigInteger 3.0 提供了 random() 方法生成随机大整数
    • BigInteger 2.7 提供了 factorial() 静态方法计算阶乘
  • 模板支持:
    • BigInteger 3.0 的 pow 方法使用模板参数
    • BigInteger 2.7 有明确的 pow(const long long&, const BigInteger&) 重载

3. 实现细节差异

  • 实现:
    • BigInteger 3.0 使用 FastFourierTransformation 命名空间
    • BigInteger 2.7 使用 __FFT 命名空间
    • 具体实现细节和常量定义略有不同
  • 比较运算符:
    • BigInteger 2.7 只使用 operator<=>
    • BigInteger 2.7 同时支持 operator<=> 和传统的比较运算符重载
  • 位移操作:
    • BigInteger 3.0 的位移操作直接使用 pow 方法
    • BigInteger 2.7 的位移操作实现相同但代码风格略有不同

4. 代码风格差异

  • 方法实现位置:
    • BigInteger 3.0 将更多方法实现放在类定义内部
    • BigInteger 2.7 倾向于将方法实现在类定义外部
  • 命名风格:
    • BigInteger 3.0 使用更一致的命名风格
    • BigInteger 2.7 在某些地方使用了下划线前缀

5. 性能优化

  • BigInteger 3.0 的内存池实现可能提供更好的内存分配性能
  • BigInteger 2.7 的 factorial 方法提供了额外的数学运算功能,但速度慢

压位高精度整数 2.7

原文
存档评论3 条抓取于