前言

如果遇到 long long 开不下的情况,可以使用 __int128 来博一把!

note__int128 仅 \(64\) 位 \(GCC G++\) 支持,不在 \(C++\) 标准中!不在 namespace std 中!

\(64\) 位 \(GCC\) 可直接使用。

存储范围

顾名思义, __int128 就是占用128字节的整数存储类型。由于是二进制,范围就是 \(-2^{127}\) ~ \(2^{127}-1\),如果使用了 unsigned __int128,则范围变成 \(0\) ~ \(2^{128}\),即约39位数!

经作者实测,这样 __int128 的精确范围是

\(-170141183460469231731687303715884105728\) ~ \(170141183460469231731687303715884105727\)

unsigned __int128 的精确范围则是 \(0\) ~ \(340282366920938463463374607431768211455\)

使用方法

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

__int128 a=9,b=27;
a=10;
a+=b;
a*=b;
... ...

输入输出

由于不在 \(C++\) 标准内,没有配套的 printf scanf cin cout 输入输出,只能手写。

方法一:(对于 cin cout ):

思路:写一个类(更推荐结构体),实现快读(换一下数据),写一个返回值,将读入过程转换成一个函数。

note :此种方法用的还是 \(C\) 的输入输出!切记不要把它和 istream ostream 混为一谈!

如果程序中关闭了同步(ios::sync_with_stdio(0);)造成输入输出混乱,后果自负!

namespace fastio{
struct reader{
template<typename T>Reader&operator>>(T&x){
char c=getchar();short f=1;
while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}
x=0;while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}x*=f;return *this;
}
}cin;
struct writer{
template<typename T>Writer&operator<<(T x){
if(x==0)return putchar('0'),*this;
if(x<0)putchar('-'),x=-x;
static int sta[45];int top=0;
while(x)sta[++top]=x%10,x/=10;
while(top)putchar(sta[top]+'0'),--top;
return*this;
}
}cout;
};
#define cin fastio::cin
#define cout fastio::cout

在程序的最上面(头文件下第一行,using namespace std前)加入此代码,就可以使用 cin cout 输入输出 __int128 啦!

note :这是自己写的 cin cout ,用std::cin std::cout不可以的


方法二:快读、快写板子:

其实就是把快读、快写的数据类型改了一下(偷懒)

#define int __int128
inline void read(int &n){
int 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<<1)+(x<<3)+(ch^48);
ch=getchar();
}
n=x*f;
}
inline void print(int n){
if(n<0){
putchar('-');
n*=-1;
}
if(n>9) print(n/10);
putchar(n % 10 + '0');
}
#undef int

至于 printf scanf \(……\) \(hmm\) \(……\) 那是 \(C\) 的函数,除非你再写一个?


配套函数

如果你想赋一个 \({巨大无比}\) 的初值,最大只能赋值成 long long 最大值。想要 \(1000000000000000000000000000000000000000\) 这样的数只能用字符串:

inline __int128 to_int128(string s){
int l=s.length();
__int128 m=0;
for(int i=0;i<l;i++){
m*=10;
m+=s[i]-48;
}
return m;
}

END.

2021/12/31

upd on 2022/1/19

添加 cin cout 输出


另祝:Happy New Year! (In my \(luogu\) \(blog\)

虽然新年过了,春节不是还没到嘛?

最新文章

  1. Atitit.研发团队的管理原则---立长不立贤与按资排辈原则
  2. iOS从零开始学习直播之音频2.后台播放和在线播放
  3. 经典 socket通讯 -- 已验证
  4. AJAX状态值与状态码
  5. spring webservice 搭建出现的异常处理。异常: NAMESPACE_ERR: An attempt is made to create or change an object in a way whi
  6. Java实战之01Struts2-02配置文件
  7. 转:Google论文之三----MapReduce
  8. windows下体验Redis
  9. 七种Prolog解释器/编译器
  10. js实现input输入框只能输入数字的功能
  11. 摆脱printf的噩梦
  12. 在vue中使用highcharts的仪表图等扩展
  13. python pip 使用时错误: Patal error in launcher:Unable to create process using &#39;&quot;&#39;
  14. php根据时间显示刚刚,几分钟前,几小时前的实现代码
  15. 函数 day9
  16. python string tuple list dict 相互转换的方法
  17. 20155328 网络攻防 实验五:MSF基础应用
  18. Loadrunner如何进行有效的IP欺骗
  19. frame与bounds的区别比较
  20. MFC如何获取硬盘的序列号

热门文章

  1. 对ORM的理解
  2. CF1547A Shortest Path with Obstacle 题解
  3. CF1443A Kids Seating 题解
  4. VS c/c++常用配置项
  5. 逆波兰(加、减、乘、除、括号)表达式原理及C++代码实现
  6. 【LeetCode】1466. 重新规划路线 Reorder Routes to Make All Paths Lead to the City Zero (Python)
  7. 【LeetCode】1056. Confusing Number 解题报告(C++)
  8. 【九度OJ】题目1196:成绩排序 解题报告
  9. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)
  10. 【剑指Offer】10- II. 青蛙跳台阶问题 解题报告(Python & C++)