老纸不想写高精度故学了下__int128_int128(有符号)_类型128位有符号整数最大能存约170,141,183,460,469,231,731,687,303,715,884,105,727(约 1.7 × 10³⁸)十进制大约能存 39 位数。不过用的时候有两点要特别注意它不是标准C__int128是 GCC 和 Clang 编译器的扩展Visual Studio 的 MSVC 编译器不支持。在竞赛或Linux环境下通常没问题。输入输出麻烦你不能直接用cin、cout、scanf或printf来读写__int128需要自己写专门的快读快写函数。输入输出方法一手写快读快写函数最常用这是竞赛中最常见的方式直接调用read()和write()函数#include iostream #include string #include algorithm using namespace std; // 快读 __int128 read() { __int128 x 0, f 1; char ch getchar(); while (ch 0 || ch 9) { if (ch -) f -1; ch getchar(); } while (ch 0 ch 9) { x x * 10 ch - 0; ch getchar(); } return x * f; } // 快写 void write(__int128 x) { if (x 0) { putchar(-); x -x; } if (x 9) write(x / 10); putchar(x % 10 0); } int main() { __int128 a read(); __int128 b read(); write(a b); return 0; }方法二重载和运算符重载之后就可以像普通类型一样直接用cin/cout了写起来更顺手#include iostream #include string #include algorithm using namespace std; // 重载输入 istream operator(istream is, __int128 n) { string s; is s; n 0; bool neg false; int i 0; if (s -) { neg true; i 1; } for (; i s.size(); i) { n n * 10 (s[i] - 0); } if (neg) n -n; return is; } // 重载输出 ostream operator(ostream os, __int128 n) { if (n 0) return os 0; if (n 0) { os -; n -n; } string s; while (n) { s.push_back(0 n % 10); n / 10; } reverse(s.begin(), s.end()); return os s; } int main() { __int128 a, b; cin a b; cout a b endl; return 0; }需要注意的点编译器限制__int128是 GCC 和 Clang 的扩展MSVCVisual Studio不支持在 Linux 或竞赛环境如洛谷、Codeforces中通常没问题。性能重载运算符内部用字符串转换效率比快读快写稍低但一般够用。如果数据量极大比如百万级建议用getchar/putchar版本的快读快写。负数处理写函数时别忘了处理负号否则输出会出错。还不如写高精度呢超
算法札记:C++ __int128
老纸不想写高精度故学了下__int128_int128(有符号)_类型128位有符号整数最大能存约170,141,183,460,469,231,731,687,303,715,884,105,727(约 1.7 × 10³⁸)十进制大约能存 39 位数。不过用的时候有两点要特别注意它不是标准C__int128是 GCC 和 Clang 编译器的扩展Visual Studio 的 MSVC 编译器不支持。在竞赛或Linux环境下通常没问题。输入输出麻烦你不能直接用cin、cout、scanf或printf来读写__int128需要自己写专门的快读快写函数。输入输出方法一手写快读快写函数最常用这是竞赛中最常见的方式直接调用read()和write()函数#include iostream #include string #include algorithm using namespace std; // 快读 __int128 read() { __int128 x 0, f 1; char ch getchar(); while (ch 0 || ch 9) { if (ch -) f -1; ch getchar(); } while (ch 0 ch 9) { x x * 10 ch - 0; ch getchar(); } return x * f; } // 快写 void write(__int128 x) { if (x 0) { putchar(-); x -x; } if (x 9) write(x / 10); putchar(x % 10 0); } int main() { __int128 a read(); __int128 b read(); write(a b); return 0; }方法二重载和运算符重载之后就可以像普通类型一样直接用cin/cout了写起来更顺手#include iostream #include string #include algorithm using namespace std; // 重载输入 istream operator(istream is, __int128 n) { string s; is s; n 0; bool neg false; int i 0; if (s -) { neg true; i 1; } for (; i s.size(); i) { n n * 10 (s[i] - 0); } if (neg) n -n; return is; } // 重载输出 ostream operator(ostream os, __int128 n) { if (n 0) return os 0; if (n 0) { os -; n -n; } string s; while (n) { s.push_back(0 n % 10); n / 10; } reverse(s.begin(), s.end()); return os s; } int main() { __int128 a, b; cin a b; cout a b endl; return 0; }需要注意的点编译器限制__int128是 GCC 和 Clang 的扩展MSVCVisual Studio不支持在 Linux 或竞赛环境如洛谷、Codeforces中通常没问题。性能重载运算符内部用字符串转换效率比快读快写稍低但一般够用。如果数据量极大比如百万级建议用getchar/putchar版本的快读快写。负数处理写函数时别忘了处理负号否则输出会出错。还不如写高精度呢超