跳到正文

个人记录

快读模板+大数模板!

aaron0919约 48 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 48 分钟
获赞
1

版本

  1. v1当前

快读模板 + 大数模板

使用方法

声明

本人为了调试,注释了一堆东西,如果想用 string,请自行去掉注释(可能和我一样 CE)。。

你可以自己扩展,例如将 numberxx 扩展到 uint256_t, uint2147483647_t,最好扩展 int128_t, int256_t 等。(也可以加入奇怪的模板,例如线段树,vector 等)

我已经 #define uint128 uint128_t,扩展时最好都已 _t 结尾,最后在 #define 回去,可以参考我的马蜂。

诚挚地感谢和欢迎合作\Huge{\text{诚挚地感谢和欢迎合作}}

▣ 若同意请勾选此选项 A‾\texttt{\underline{A}}

快读快输

  • io.gc() fread 读入,有 EOF
  • io.read() 返回一个整数
  • io.read(T* start, T* end) 读入整数数组
  • io.readf() 返回一个浮点数
  • io.reads(char* str /*或 string str*/) 读入一个字符串
  • io.readc() 返回一个字符- 以上读入加 s_ 需要传参,表示安全读入,会有 EOF 判断,加了 s_ 的数组读入会返回成功读入的数量
  • io.write(T x) 输出一个整数
  • io.write(T* start, T* end) 输出一个整数数组
  • io.writef(T x, int k = 6) 输出一个浮点数(默认保留 6 位小数,有误差)
  • io.writef(T* start, T* end, int k = 6) 输出一个浮点数组
  • io.writes(char* str /*或 string str*/) 输出一个字符串
  • 以上输出末尾默认自带空格(最后一个传参为),加 l 表示输出自带换行
  • io.push(char str) 输出一个字符

大数字

  • numberxx 大数扩展(已更新 uint128_t)
  • uint128_T a 一个 unsigned __int128 的实现
  • a = io.read<uint128_t>(), io.s_read(a) 都可实现读入
  • io.write() 直接支持输出

code

C++
#include <stdio.h>
#include <string>
// #include <initializer_list>
#define MAXSIZE (1 << 20)
#define FREAD 0
#define FREOPEN(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout);
#define _numberxx 1
#define _string 1

namespace numberxx
{
#include <stdint.h>
#define uint128 uint128_t
#if _numberxx
#define _uint128_t 1
#else
#define _uint128_t 0
#endif
    // #if __cplusplus >= 201103L
    // #include <initializer_list>
    // #endif
    static const char HEXDIGITS[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    static const char HEXDIGITSUPPER[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

#if _uint128_t
    struct uint128_t;
    struct uint128_t
    {
        uint64_t up, low;
        uint128_t() : up(0), low(0) {}
        uint128_t(const uint64_t _low) : up(0), low(_low) {}
        uint128_t(const uint64_t _up, const uint64_t _low) : up(_up), low(_low) {}
        template <typename _Tp>
        uint128_t(const _Tp x)
        {
            up = x < 0 ? -1 : 0;
            low = x;
        }
        // #if __cplusplus >= 201103L
        //         uint128_t(std::initializer_list<uint64_t> initlist)
        //         {
        //             up = *initlist.begin();
        //             low = *(initlist.begin() + 1);
        //         }
        // #endif
        inline uint128_t operator+=(const uint128_t &x);
        inline uint128_t operator-=(const uint128_t &x);
        inline uint128_t operator*=(const uint128_t &x);
        inline uint128_t operator/=(const uint128_t &x);
        inline uint128_t operator%=(const uint128_t &x);
        inline uint128_t operator<<=(const int x);
        inline uint128_t operator>>=(const int x);
        inline uint128_t operator&=(const uint128_t &x);
        inline uint128_t operator|=(const uint128_t &x);
        inline uint128_t operator^=(const uint128_t &x);
        inline operator unsigned __int64()
        {
            return low;
        }
        inline char *c_str();
    };
    inline uint128_t operator&(const uint128_t &number1, const uint128_t &number2)
    {
        return uint128_t(number1.up & number2.up, number1.low & number2.low);
    }
    inline uint128_t operator|(const uint128_t &number1, const uint128_t &number2)
    {
        return uint128_t(number1.up | number2.up, number1.low | number2.low);
    }
    inline uint128_t operator~(const uint128_t &number)
    {
        return uint128_t(~number.up, ~number.low);
    }
    inline uint128_t operator^(const uint128_t &number1, const uint128_t &number2)
    {
        return uint128_t(number1.up ^ number2.up, number1.low ^ number2.low);
    }
    inline bool zero128(const uint128_t &number)
    {
        return !number.up && !number.low;
    }
    inline uint128_t operator<<(const uint128_t &number, const int x)
    {
        return x >= 128 ? 0 : x == 64         ? uint128_t(number.low, 0)
                          : !x                ? number
                          : x < 64            ? uint128_t((number.up << x) + (number.low >> (64 - x)), number.low << x)
                          : 128 > x && x > 64 ? uint128_t(number.low << (x - 64), 0)
                                              : (uint128_t)(0);
    }
    inline uint128_t operator>>(const uint128_t &number, const int x)
    {
        return x >= 128 ? 0 : x == 64         ? uint128_t(0, number.up)
                          : !x                ? number
                          : x < 64            ? uint128_t(number.up >> x, (number.up << (64 - x)) + (number.low >> x))
                          : 128 > x && x > 64 ? uint128_t(0, number.up >> (x - 64))
                                              : (uint128_t)(0);
    }
    inline int bits128(const uint128_t &number)
    {
        int result = 0;
        if (number.up)
        {
            result = 64;
            for (uint64_t up = number.up; up; ++result)
                up >>= 1;
        }
        else
            for (uint64_t low = number.low; low; ++result)
                low >>= 1;
        return result;
    }
    inline bool Bool(const uint128_t &x)
    {
        return x.up || x.low;
    }
    inline bool operator==(const uint128_t &number1, const uint128_t &number2)
    {
        return number1.up == number2.up && number1.low == number2.low;
    }
    inline bool operator!=(const uint128_t &number1, const uint128_t &number2)
    {
        return !(number1 == number2);
    }
    inline bool operator>(const uint128_t &number1, const uint128_t &number2)
    {
        return number1.up == number2.up ? number1.low > number2.low : number1.up > number2.up;
    }
    inline bool operator>=(const uint128_t &number1, const uint128_t &number2)
    {
        return number1 > number2 || number1 == number2;
    }
    inline bool operator<(const uint128_t &number1, const uint128_t &number2)
    {
        return !(number1 >= number2);
    }
    inline bool operator<=(const uint128_t &number1, const uint128_t &number2)
    {
        return !(number1 > number2);
    }
    inline bool operator&&(const uint128_t &number1, const uint128_t &number2)
    {
        return Bool(number1) && Bool(number2);
    }
    inline bool operator||(const uint128_t &number1, const uint128_t &number2)
    {
        return Bool(number1) || Bool(number2);
    }
    inline bool operator!(const uint128_t &number)
    {
        return !(number.up || number.low);
    }
    inline uint128_t operator+(const uint128_t &number1, const uint128_t &number2)
    {
        return uint128_t(number1.up + number2.up + (number1.low + number2.low < number1.low), number1.low + number2.low);
    }
    inline uint128_t operator-(const uint128_t &number1, const uint128_t &number2)
    {
        return uint128_t(number1.up - number2.up - (number1.low - number2.low > number1.low), number1.low - number2.low);
    }
    inline uint128_t operator-(const uint128_t &number)
    {
        return uint128_t(-number.up, -number.low);
    }
    inline uint128_t operator*(const uint128_t &number1, const uint128_t &number2)
    {
        uint128_t target, tmp, tmp2;
        uint64_t
            top[4] = {number1.up >> 32, number1.up & 0xffffffff, number1.low >> 32, number1.low & 0xffffffff},
            bottom[4] = {number2.up >> 32, number2.up & 0xffffffff, number2.low >> 32, number2.low & 0xffffffff}, products[4][4];
        for (signed char y = 3; y >= 0; --y)
            for (signed char x = 3; x >= 0; --x)
                products[3 - x][y] = top[x] * bottom[y];
        uint64_t fourth32 = products[0][3] & 0xffffffff;
        uint64_t third32 = (products[0][2] & 0xffffffff) + (products[0][3] >> 32) + (products[1][3] & 0xffffffff);
        uint64_t second32 = (products[0][1] & 0xffffffff) + (products[0][2] >> 32) + (products[1][2] & 0xffffffff) + (products[1][3] >> 32) + (products[2][3] & 0xffffffff);
        uint64_t first32 = (products[0][0] & 0xffffffff) + (products[0][1] >> 32) + (products[1][1] & 0xffffffff) + (products[1][2] >> 32) + (products[2][2] & 0xffffffff) + (products[2][3] >> 32) + (products[3][3] & 0xffffffff);
        return (uint128_t)fourth32 + uint128_t(second32, 0) + uint128_t(first32 << 32, 0) + uint128_t(third32 >> 32, third32 << 32);
    }
    inline void divmod128(const uint128_t &l, const uint128_t &r, uint128_t &retDiv, uint128_t &retMod)
    {
        uint128_t copyd, adder, resDiv = 0, resMod = l;
        const int diffBits = bits128(l) - bits128(r);
        if (r > l)
        {
            retMod = l;
            retDiv = 0;
        }
        else
        {
            copyd = r << diffBits;
            adder = uint128_t(1) << diffBits;
            if (copyd > resMod)
            {
                copyd = copyd >> 1;
                adder = adder >> 1;
            }
            while (resMod >= r)
            {
                if (resMod >= copyd)
                {
                    resMod = resMod - copyd;
                    resDiv = resDiv | adder;
                }
                copyd = copyd >> 1;
                adder = adder >> 1;
            }
            retDiv = resDiv;
            retMod = resMod;
        }
    }
    inline uint128_t operator/(const uint128_t &l, const uint128_t &r)
    {
        uint128_t copyd, adder, resDiv = 0, resMod = l, retDiv;
        const int diffBits = bits128(l) - bits128(r);
        if (r > l)
            retDiv = 0;
        else
        {
            copyd = r << diffBits;
            adder = uint128_t(1) << diffBits;
            if (copyd > resMod)
            {
                copyd = copyd >> 1;
                adder = adder >> 1;
            }
            while (resMod >= r)
            {
                if (resMod >= copyd)
                {
                    resMod = resMod - copyd;
                    resDiv = resDiv | adder;
                }
                copyd = copyd >> 1;
                adder = adder >> 1;
            }
            retDiv = resDiv;
        }
        return retDiv;
    }
    inline uint128_t operator%(const uint128_t &l, const uint128_t &r)
    {
        return l - l / r * r;
    }
    inline uint128_t uint128_t::operator+=(const uint128_t &r)
    {
        return *this = *this + r;
    }
    inline uint128_t uint128_t::operator-=(const uint128_t &r)
    {
        return *this = *this - r;
    }
    inline uint128_t uint128_t::operator*=(const uint128_t &r)
    {
        return *this = *this * r;
    }
    inline uint128_t uint128_t::operator/=(const uint128_t &r)
    {
        return *this = *this / r;
    }
    inline uint128_t uint128_t::operator%=(const uint128_t &r)
    {
        return *this = *this % r;
    }
    inline uint128_t uint128_t::operator<<=(const int r)
    {
        return *this = *this << r;
    }
    inline uint128_t uint128_t::operator>>=(const int r)
    {
        return *this = *this >> r;
    }
    inline uint128_t uint128_t::operator&=(const uint128_t &r)
    {
        return *this = *this & r;
    }
    inline uint128_t uint128_t::operator|=(const uint128_t &r)
    {
        return *this = *this & r;
    }
    inline static void reverseString(char *str, const unsigned long long length)
    {
        for (unsigned long long i = 0, j = length - 1; i < j; ++i, --j)
            str[i] ^= str[j] ^= str[i] ^= str[j];
    }
    inline void tostring128(const uint128_t &number, const unsigned char base, char *out, unsigned long long outLength)
    {
        uint128_t rDiv = number, rMod = 0;
        unsigned long long offset = 0;
        if (base < 2 || base > 36)
            return;
        do
        {
            if (offset > outLength - 1)
                return;
            divmod128(rDiv, base, rDiv, rMod);
            out[offset++] = HEXDIGITS[rMod.low];
        } while (!zero128(rDiv));
        out[offset] = 0;
        reverseString(out, offset);
    }
    inline void tostring128_upper(uint128_t &number, const unsigned char base, char *out, unsigned long long outLength)
    {
        uint128_t rDiv = number, rMod = 0;
        unsigned long long offset = 0;
        if (base < 2 || base > 36)
            return;
        do
        {
            if (offset > outLength - 1)
                return;
            divmod128(rDiv, base, rDiv, rMod);
            out[offset++] = HEXDIGITSUPPER[rMod.low];
        } while (!zero128(rDiv));
        out[offset] = 0;
        reverseString(out, offset);
    }
    inline void tostring128(uint128_t &number, const unsigned char base, char *out, unsigned long long outLength, const char *BaseMap)
    {
        uint128_t rDiv = number, rMod = 0;
        unsigned long long offset = 0;
        if (base < 2 || base > 36)
            return;
        do
        {
            if (offset > outLength - 1)
                return;
            divmod128(rDiv, base, rDiv, rMod);
            out[offset++] = BaseMap[rMod.low];
        } while (!zero128(rDiv));
        out[offset] = 0;
        reverseString(out, offset);
    }
    inline char *uint128_t::c_str()
    {
        static char Str[80];
        tostring128(*this, 10, Str, 80);
        return Str;
    }
#endif

}

struct IO
{
#if FREAD
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;

    IO() : p1(buf), p2(buf), pp(pbuf) {}

    ~IO()
    {
        fwrite(pbuf, 1, pp - pbuf, stdout);
    }
#endif

    typedef long long ll;

    inline bool blank(const char &ch)
    {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == EOF;
    }

    inline bool line(const char &ch)
    {
        return ch == '\n' || ch == '\r' || ch == EOF;
    }

    inline bool isdigit(const char &ch)
    {
        return ch >= '0' && ch <= '9';
    }

    inline char gc()
    {
#if FREAD
        if (p1 == p2)
        {
            p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
        }
        return p1 == p2 ? EOF : *p1++;
#endif
        return getchar();
    }

    inline __int128 read()
    {
        bool sign = 0;
        __int128 x = 0;
        char ch = gc();
        for (; !isdigit(ch); ch = gc())
        {
            if (ch == '-')
            {
                sign = 1;
            }
        }
        for (; isdigit(ch); ch = gc())
        {
            x = x * 10 + (ch - '0');
        }
        if (sign)
        {
            return -x;
        }
        else
        {
            return x;
        }
    }

    inline long double readf()
    {
        bool sign = 0;
        long double x = 0, tmp = 1;
        char ch = gc();
        for (; !isdigit(ch); ch = gc())
        {
            if (ch == '-')
            {
                sign = 1;
            }
        }
        for (; isdigit(ch); ch = gc())
        {
            x = x * 10 + (ch - '0');
        }
        if (ch == '.')
        {
            for (ch = gc(); isdigit(ch); ch = gc())
            {
                tmp /= 10.0, x += tmp * (ch - '0');
            }
        }
        if (sign)
        {
            return -x;
        }
        else
        {
            return x;
        }
    }

    template <class T>
    inline bool s_read(T &x)
    {
        bool sign = 0;
        x = 0;
        char ch = gc();
        for (; !isdigit(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
            if (ch == '-')
            {
                sign = 1;
            }
        }
        for (; isdigit(ch); ch = gc())
        {
            x = x * T(10) + T(ch - '0');
        }
        if (sign)
        {
            x = -x;
        }
        return 1;
    }

    template <class T>
    inline bool s_readf(T &x)
    {
        bool sign = 0;
        x = 0;
        long double tmp = 1;
        char ch = gc();
        for (; !isdigit(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
            if (ch == '-')
            {
                sign = 1;
            }
        }
        for (; isdigit(ch); ch = gc())
        {
            x = x * 10 + (ch - '0');
        }
        if (ch == '.')
        {
            for (ch = gc(); isdigit(ch); ch = gc())
            {
                if (ch == EOF)
                {
                    if (sign)
                    {
                        x = -x;
                    }
                    return 0;
                }
                tmp /= 10.0, x += tmp * (ch - '0');
            }
        }
        if (sign)
        {
            x = -x;
        }
        return 1;
    }

    inline char readc()
    {
        return gc();
    }

    inline bool s_readc(char &ch)
    {
        return ch = gc(), ch != EOF;
    }

    inline void reads(char *s)
    {
        char ch = gc();
        for (; blank(ch); ch = gc())
            ;
        for (; !blank(ch); ch = gc())
        {
            *s++ = ch;
        }
        *s = 0;
    }

#ifdef _string
    inline void reads(std::string &s)
    {
        s.clear();
        char ch = gc();
        for (; blank(ch); ch = gc())
            ;
        for (; !blank(ch); ch = gc())
        {
            s.push_back(ch);
        }
    }
#endif

    inline bool s_reads(char *s)
    {
        char ch = gc();
        for (; blank(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
        }
        for (; !blank(ch); ch = gc())
        {
            *s++ = ch;
        }
        *s = 0;
        return 1;
    }

#ifdef _string
    inline bool s_reads(std::string &s)
    {
        s.clear();
        char ch = gc();
        for (; blank(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
        }
        for (; !blank(ch); ch = gc())
        {
            s.push_back(ch);
        }
        return 1;
    }
#endif

    inline void readls(char *s)
    {
        char ch = gc();
        for (; line(ch); ch = gc())
            ;
        for (; !line(ch); ch = gc())
        {
            *s++ = ch;
        }
        *s = 0;
    }

#ifdef _string
    inline void readls(std::string &s)
    {
        s.clear();
        char ch = gc();
        for (; line(ch); ch = gc())
            ;
        for (; !line(ch); ch = gc())
        {
            s.push_back(ch);
        }
    }
#endif

    inline bool s_readls(char *s)
    {
        char ch = gc();
        for (; line(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
        }
        for (; !line(ch); ch = gc())
        {
            *s++ = ch;
        }
        *s = 0;
        return 1;
    }

#ifdef _string
    inline bool s_readls(std::string &s)
    {
        s.clear();
        char ch = gc();
        for (; line(ch); ch = gc())
        {
            if (ch == EOF)
            {
                return 0;
            }
        }
        for (; !line(ch); ch = gc())
        {
            s.push_back(ch);
        }
        return 1;
    }
#endif

    template <class T>
    inline void read(T *start, T *end)
    {
        for (; start != end; start++)
        {
#ifdef _numberxx
            *start = read<T>();
#else
            *start = read();
#endif
        }
    }

    template <class T>
    inline void readf(T *start, T *end)
    {
        for (; start != end; start++)
        {
            *start = readf();
        }
    }

    template <class T>
    inline int s_read(T *start, T *end)
    {
        int res = 0;
        for (; start != end; start++)
        {
            res += s_read(*start);
        }
        return res;
    }

    template <class T>
    inline int s_readf(T *start, T *end)
    {
        int res = 0;
        for (; start != end; start++)
        {
            res += s_readf(*start);
        }
        return res;
    }

    void push(const char &ch)
    {
#if FREAD
        if (pp - pbuf == MAXSIZE)
        {
            fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        }
        *pp++ = ch;
#else
        putchar(ch);
#endif
    }

    template <class T>
    void write(T x, const char lastChar = ' ')
    {
        if (x < T(0))
        {
            x = -x, push('-');
        }
        static char sta[1024];
        int top = 0;
        do
        {
            sta[top++] = x % T(10), x /= T(10);
        } while (x);
        while (top)
        {
            push(sta[--top] + '0');
        }
        if (lastChar)
        {
            push(lastChar);
        }
    }

    template <class T>
    void writel(T x, const char lastChar = ' ')
    {
        write(x, lastChar);
        push('\n');
    }

    template <class T>
    void writef(T x, int k = 6, const char lastChar = ' ')
    {
        static char sta[1024];
        int top = 0;
        if ((int)x == 0)
        {
            push('0');
        }
        else
        {
            while (x >= 1)
            {
                sta[++top] = (int)x % 10;
                x = (x - (int)x) + (int)x / 10;
            }
            while (top)
            {
                push(sta[top--] + '0');
            }
        }

        if (k != 0)
        {
            push('.');
            for (int i = 0; i <= k; i++)
            {
                x *= 10;
                push((int)x + '0');
                x -= (int)x;
            }
        }

        if (lastChar)
        {
            push(lastChar);
        }
    }

    template <class T>
    void writelf(T x, const char lastChar = ' ')
    {
        writef(x, lastChar);
        push('\n');
    }

    template <class T>
    void write(T *start, T *end, const char lastChar = ' ')
    {
        for (; start != end; start++)
        {
            write(*start, lastChar);
        }
    }

    template <class T>
    void writel(T *start, T *end, const char lastChar = ' ')
    {
        for (; start != end; start++)
        {
            write(*start, lastChar);
        }
        push('\n');
    }

    template <class T>
    void writef(T *start, T *end, int k = 6, const char lastChar = ' ')
    {
        for (; start != end; start++)
        {
            writef(*start, k, lastChar);
        }
    }

    template <class T>
    void writelf(T *start, T *end, int k = 6, const char lastChar = ' ')
    {
        for (; start != end; start++)
        {
            writef(*start, k, lastChar);
        }
        push(lastChar);
    }

    void writes(const char *s, const char lastChar = ' ')
    {
        for (int i = 0; s[i]; i++)
        {
            push(s[i]);
        }
        if (lastChar)
        {
            push(lastChar);
        }
    }

    void writels(const char *s, const char lastChar = ' ')
    {
        for (int i = 0; s[i]; i++)
        {
            push(s[i]);
        }
        if (lastChar)
        {
            push(lastChar);
        }
        push('\n');
    }

#ifdef _string
    void writes(std::string s, const char lastChar = ' ')
    {
        for (int i = 0; s[i]; i++)
        {
            push(s[i]);
        }
        if (lastChar)
        {
            push(lastChar);
        }
    }

    void writels(std::string s, const char lastChar = ' ')
    {
        for (int i = 0; s[i]; i++)
        {
            push(s[i]);
        }
        if (lastChar)
        {
            push(lastChar);
        }
        push('\n');
    }
#endif

    void writec(char ch, const char lastChar = ' ')
    {
        push(ch);
        if (lastChar)
        {
            push(lastChar);
        }
    }

    void writelc(char ch, const char lastChar = ' ')
    {
        push(ch);
        if (lastChar)
        {
            push(lastChar);
        }
        push('\n');
    }

#if _numberxx
    template <class T>
    inline T read()
    {
        T x(0);
        s_read(x);
        return x;
    }
#endif
} io;

using namespace numberxx;

int main()
{
#ifndef ONLINE_JUDGE
    FREOPEN(1);
#endif

    return 0;
}
原文