一 : 介绍:
int64_t 是 C/C++ 中的一个标准整型数据类型,表示 64 位带符号整数,范围是 -2^63 到 2^63-1。
int128_t 是 C/C++ 中的一个扩展整型数据类型,表示 128 位带符号整数,范围是 -2^127 到 2^127-1。
二 : 用途:
处理极大值的运算,当long long 和 unsigned long long 无法满足运算条件时使用
0 <= i , j ≤ 10 ^ 12
当 i 和 j 运算为 10 ^ 12次加法或者进行乘法时long long 和 unsigned long long 无法满足运算条件
三 : 头文件即使用方式
#include <cstdio>
#include <cstdint>
using namespace std;
using i64 = int64_t;
using i128 = __int128_t;
对于i64
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 A, B;
cin >> A >> B;
cout << A * B << endl;
}
可以执行以上操作:
例如输入俩个大数
1234567412418986541 123456789987654321232414214
结果 7988804624435789267
但是对于i128比较特殊
一般编译器不允许输入输出
例如:
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i128 A, B;
cin >> A >> B;
cout << A * B << endl;
}
这样是不被允许的
正确使用为通过函数接受转换i64的数据类型(也可以是字符串)
例如:
i128 ans(i128 ad, i128 hp)
{
i128 res = (i128)1 << 120, pre = 0;
res - ad * hp;
return res;
}
而主函数部分为
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 ad, hp;
cin >> ad >> hp;
ans(ad, hp);
}
当然由于不允许直接输出即使我们用一个i128变量接收ans的返回值也是不允许printf和cout的操作.
四 : 特殊类型i128如何输出 ?
通常输出是将i28转换为字符串后进行输出例如下列模板
template <typename T> void print(T x)
{
if (x == 0) cout << "0";
else
{
string s;
while (x)
{
s.push_back(char('0' + x % 10));
x /= 10;
}
cout << s;
}
}
总体实现
#include <cstdio>
#include<string>
#include<algorithm>
#include <cstdint>
#include<iostream>
using namespace std;
using i64 = int64_t;
using i128 = __int128_t;
i128 f(i128 ad, i128 hp)
{
i128 res = (i128)1 << 120, pre = 0;
res - ad * hp;
return res;
}
template <typename T> void print(T x)
{
if (x == 0) cout << "0";
else
{
string s;
while (x)
{
s.push_back(char('0' + x % 10));
x /= 10;
}
cout << s;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 ad, hp;
cin >> ad >> hp;
print(f(ad, hp));
}
输入1234567412418986541 123456789987654321232414214
计算 1 << 120 - A * B
结果 6754430820607083092785194875997229231