4

__int128:懒人的福音 - 201929

 1 year ago
source link: https://www.cnblogs.com/201929-whx/p/17062823.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

对于一个懒懒的,不想写高精的人(就是我),每次都会遭遇到答案爆long  long的危险

比如说这道题:

题目传送门

最后的23−25的两个点,long  long甚至unsigned  long  long都无法满足,难道真的要手打高精度了吗?

不,我们有__int128!

那么这到底是什么 可以吃吗 ?

关于__int128

先来看看一些常见的整数变量能存的范围与占用的字节:

类型名称 占用字节 存储范围
int 4 −231 ~ 231−1
long  long 8 −263 ~ 263−1
unsigned  long  long 8 0 ~ 264−1

再来看看__int128

存储范围为−2127 ~ 2127−1,但是占用了128字节

虽然内存占的多,但存储范围依然多

那么如何使用?

其实就是把快读输入改了一改

__int128 read()
{
    __int128 x=0;
    char ch=getchar();
    while(ch<'0' || ch>'9') ch=getchar();
    while('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar();
    return x;
}
...
__int128 n=read();

其实也是把快输改了一改

void write(__int128 x)
{
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
__int128 n;
n=...
write(n);

__int128是好,但是用的时候一定一定要注意空间限制!

当然,__int128并不能完全取代高精,所以如果__int128过不掉时,还是老老实实打高精吧!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK