关于__128的使用

本文介绍了如何使用__int128类型处理大于long long int所能表示的最大值的问题,并提供了输入输出的实现方法。通过模板函数实现了快速读取(__int128)和输出(__int128)的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于__int128

long long int 只能表示( 2 63 − 1 2^{63}-1 2631),而__int128可以表示( 2 127 − 1 2^{127}-1 21271),但是需要自己写输入和输出,类似于快读


关于快读:

template<typename T> inline void re(T &x) {
    int f = 1; x = 0; char c = getchar();
    while (!isdigit(c)) {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        //x = (x << 3) + (x << 1) + (c ^ 48);
        x = x * 10 + c - '0';
        c = getchar();
    }
    x *= f;
}

template<typename T, typename... Args> void re(T &first, Args& ... args) {
    re(first);
    re(args...);
}

实现__int128:

例如计算 a + b:

inline __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;
}

inline 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值