浅谈__int128
很多时候,题目的数据范围刚好卡在long long的临界范围,经过多次运算后,最后答案就有可能会爆(例如洛谷P6101 出言不逊)。
有可能只是本蒟蒻太菜,不会用其他方法,我就偶然瞟到了有人在讨论__int128这个类型,我觉得很神奇,就自己试了试,发现:
woc,不能用cin,cout读入输出有个鬼用
于是我想到了类似于快读的好东西
读入程序如下:
1 inline void read(__int128 &x) { 2 x=0; 3 int f=1;//判断正负 4 char ch=getchar();//读入字符 5 while(ch<'0'||ch>'9') { 6 if(ch=='-') 7 f=-1; 8 ch=getchar(); 9 } 10 while(ch>='0'&&ch<='9') {//是数字就读 11 x=x*10+ch-'0';//之前结果乘10,加现在读入的 12 ch=getchar(); 13 } 14 x*=f;//添上符号返回 15 }
输出程序如下:
1 inline void write(__int128 x) { 2 if(x<0) { 3 putchar('-');//输出符号 4 x=-x; 5 } 6 if(x>9)write(x/10);//递归输出 7 putchar(x%10+'0');//输出个位 8 }
以下是操作实例:
1 #include<bits/stdc++.h> 2 using namespace std; 3 inline void read(__int128 &x) { 4 x=0; 5 int f=1;//判断正负 6 char ch=getchar();//读入字符 7 while(ch<'0'||ch>'9') { 8 if(ch=='-') 9 f=-1; 10 ch=getchar(); 11 } 12 while(ch>='0'&&ch<='9') {//是数字就读 13 x=x*10+ch-'0';//之前结果乘10,加现在读入的 14 ch=getchar(); 15 } 16 x*=f;//添上符号返回 17 } 18 inline void write(__int128 x) { 19 if(x<0) { 20 putchar('-');//输出符号 21 x=-x; 22 } 23 if(x>9)write(x/10);//递归输出 24 putchar(x%10+'0');//输出个位 25 } 26 __int128 a,b; 27 int main(){ 28 read(a);//读入a 29 read(b);//读入b 30 write(a+b);//输出a+b 31 return 0;//完美结束 32 }
__int128范围是-2127≤x< 2127。
那有时候条件是<2128 的非负整数数怎么办?
那就加个unsigned呀!
直接讲实例了:
1 #include<bits/stdc++.h> 2 using namespace std; 3 inline void read(unsigned __int128 &x) { 4 x=0; 5 char ch=getchar();//读入字符 6 while(ch>='0'&&ch<='9') {//是数字就读 7 x=x*10+ch-'0';//之前结果乘10,加现在读入的 8 ch=getchar(); 9 } 10 //已经读完 11 } 12 inline void write(unsigned __int128 x) { 13 if(x>9)write(x/10);//递归输出 14 putchar(x%10+'0');//输出个位 15 } 16 unsigned __int128 a,b;//无符号整型 17 int main(){ 18 read(a);//读入a 19 read(b);//读入b 20 write(a+b);//输出a+b 21 return 0;//完美结束 22 }
## 但是大家要注意,尽量少用,因为内存占得很多

浙公网安备 33010602011771号