Блог пользователя DrinkMoreBoilingWater

Автор DrinkMoreBoilingWater, 4 года назад, По-английски

As an extension the integer scalar type __int128 is supported for targets which have an integer mode wide enough to hold 128 bits. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.

Here are some tips about __int128 read, print, compare

__int128 read() {
    __int128 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 * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
void print(__int128 x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
bool cmp(__int128 x, __int128 y) { return x > y; }
  • Проголосовать: нравится
  • +50
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hello. Thank you for your article. How can I use read() to read the numbers? After declaring your functions I wrote in main __int128 a, b; read(a); read(b); print(a + b); and it didnt work. No idea how to used read(). Can you help?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    You can try the following,

    __int128 a, b;

    a = read(); b = read(); print(a + b);

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hello, i got this error message expected unqualified-id before __int128 , how can I handle this error? thanks

»
4 года назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

Thanks for sharing this useful information. The following is a slight update to your read and write function that deals with larger values of unsigned 128-bit integers.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Are these read/print/compare functions necessary, or can we just use cin >>, cout <<, and >?

By the way, I think this comparator looks like it's backwards compared to a typical C++ comparator, so watch out for that.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    CMIIW, __int128 isn't handled by cin and cout by default. But you can make your own << operator for I/O streams (although I don't have any resources regarding how to do that).

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +16 Проголосовать: не нравится
      istream& operator >> (istream& in, __int128& num) {
        // Construct the answer in num, the same as read(num) from the blog, but using 
        // istream::get, istream::peek
        return in;
      }
      
      ostream& operator << (ostream& out, __int128 num) {
        // print the answer by either:
        //   - char-by-char using ostream::put
        //   - converting num to string and doing out << res_string
        return out;
      }
      

      You can also overload there operators for Point class in geometric problems, making it easier to debug them. Example for Point with floating-point coordinates.

      istream& operator >> (istream& in, Point& p) {
          in >> p.x >> p.y;
          return in;
      }
      
      
      ostream& operator << (ostream& out, const Point& p) {
          out << setprecision(10) << fixed;
          out << p.x << ' ' << p.y;
          return out;
      }
      
      • »
        »
        »
        »
        6 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Thanks bro

      • »
        »
        »
        »
        6 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        For a more specific version of overloaded i/o operators, you can use this:

        istream &operator>>(istream &is,lll &v) {
            string s;
            is>>s;
            v=0;
            for(auto &it:s) if(isdigit(it)) v=v*10+it-'0';
            if(s[0]=='-') v*=-1;
            return is;
        }
        
        ostream &operator<<(ostream &os,const lll &v) {
            if(v==0) return (os<<"0");
            lll num=v;
            if(v<0) os<<'-',num=-num;
            string s;
            for(;num>0;num/=10) s.pb((char)(num%10)+'0');
            reverse(all(s));
            return (os<<s);
        }
        
    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Ah, thanks!