关于__int128的使用

关于__int128的使用

正常来说,unsigned long long已经是可以定义的最大的类型了,但是如果数据范围超过了264就会爆炸。如果要处理比其大一点又不是那么大的数,就可以使用__int128这个定义。

NOI及CSP系列中可以正常使用__int128:
https://www.cnblogs.com/littlehb/p/15935662.html

__int128 就是占用128字节的整数存储类型。由于是二进制,范围就是 (−2127)∼(2127−1),
如果使用了 unsigned __int128,则范围变成 (0) (2^128),即约39位数!

由于 __int128 仅仅是 (GCC) 编译器内的东西,不在 (C++98/03/11/14/17/20) 标准内,且仅 (GCC4.6) 以上64位版本支持,很多配套都没有,只有四则运算功能, 所以要自己写输入输出。使用方法与 int long long无异

平时在codeforces上做题,需要注意选择64位的编译器,否则编译会报错。

注意:如何确定考试中能不能用int128呢?办法就是先创建一个空白的cpp文件,然后使用 __int128 a;,测试一下,编译器不报错就是可以。

#include <bits/stdc++.h>

using namespace std;

typedef unsigned __int128 LLL;

LLL read() {
    LLL 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 out(LLL x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    LLL a, b;
    a = read(), b = read();
    out(a + b);
    return 0;
}

__EOF__

posted @ 2025-08-25 10:55  katago  阅读(3)  评论(0)    收藏  举报