详解__int128
FReQuenter · · 算法·理论
前言
如果遇到 long long 开不下的情况,可以使用 __int128 来博一把!
note :__int128 仅 namespace std 中!
unsigned __int128 的精确范围则是
使用方法
由于 __int128 仅仅是 int long long 无异:
__int128 a=9,b=27;
a=10;
a+=b;
a*=b;
... ...
输入输出
由于不在 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
配套函数
如果你想赋一个 long long 最大值。想要
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 15:38
upd on 2022/1/19
添加
cincout输出
另祝:Happy New Year! (In my
虽然新年过了,春节不是还没到嘛?