「CodeNote」 __int128

Posted by Dawn-K's Blog on August 19, 2019

__int128

介绍

这是个玄学东西,应该是只能Linux评测机可以使用。但是能在使用的情况下还是非常方便的。

__int128(注意有两个下划线),也就是128位的整数数据类型.可以用scanf("%lld")方式输入(但绝对不能用cin&cout),不会报错,但是一般是自己写输入输出,或者仅仅在中途会爆数据范围的地方使用.

模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 已经过测试
void scan(__int128 &x)//输入
{
    x = 0;
    int f = 1;
    char ch;
    if((ch = getchar()) == '-')
        f = -f;
    else
        x = x*10 + ch-'0';
    while((ch = getchar()) >= '0' && ch <= '9')
        x = x*10 + ch-'0';
    x *= f;
}
void _print(__int128 x)
{
    if(x > 9)
        _print(x/10);
    putchar(x%10 + '0');
}
void print(__int128 x)//输出
{
    if(x < 0)
    {
        x = -x;
        putchar('-');
    }
    _print(x);
}

int main()
{
    __int128 a, b;
    scan(a);
    scan(b);
    print(a + b);
    return 0;
}

2019牛客多校第一场J

Fraction Comparision

这个题可以先判断整数,再(用分数)判断小数部分,但是用__int128就会变得非常简单,直接交叉相乘即可.

AC 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<bits/stdc++.h>
 
#define ll long long
using namespace std;
 
 
int main() {
    ll x, a, y, b;
    ios::sync_with_stdio(0);
    while (cin >> x >> a >> y >> b) {
        __int128 c1, c2;
        c1 = (__int128) x * b;  // 只在代码中途使用,就能很好地避免输入输出问题
        c2 = (__int128) a * y;
        if (c1 > c2) {
            cout << ">" << endl;
        } else if (c1 == c2) {
            cout << "=" << endl;
        } else {
            cout << "<" << endl;
        }
    }
 
    return 0;
}