版本比较
一些乱七八糟的板子(已封装)
正文
增加 299 行删除 0 行
254 行未改动全部展开255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557254 行未改动全部展开 }}using namespace fast_rw;```### 高精度```cppclass bigint {#define VC vector<char>#define pb push_back#define bt bigint#define f(i, a, b) for(int i=(a);i<=(b);++i)#define rf(i, a, b) for(int i=(a);i>=(b);--i) public: int symbol = 1; std::VC _a; int size() const { return _a.size(); } char &operator[](int i) { return _a[i]; } static bt add(bt A, bt B) { if (A.size() < B.size())return add(B, A); std::VC C; int t = 0; f(i, 0, A.size() - 1) { t += A[i]; if (i < B.size()) t += B[i]; C.pb(t % 10); t /= 10; } if (t) C.pb(t); return {C}; } void resize(int size) { _a.reserve(size); } static bigint plu(bigint A, bigint B) { if (A.symbol == -1 && B.symbol == -1) { auto t = add(A, B); t.symbol = -1; return t; } if (A.symbol == -1 && B.symbol == 1) { return sub(B, A); } if (A.symbol == 1 && B.symbol == -1) { return sub(A, B); } return add(A, B); } template<class T, class Y>friend bt operator+(T A, Y B) { return plu(A, B); } static bt sub(bt A, bt B) { A.symbol = 1, B.symbol = 1; std::VC C; if (A < B) { bt a; a = sub(B, A); a.symbol = -1; return a; } int t = 0; f(i, 0, A.size() - 1) { t = A[i] - t; if (i < B.size()) t -= B[i]; C.pb((t + 10) % 10); if (t < 0) t = 1; else t = 0; } while (C.size() > 1 && C.back() == 0) C.pop_back(); return bigint(C); } static bt SUB(bt A, bt B) { if (A.symbol == -1 && B.symbol == -1) { bigint t; t = sub(A, B); t.symbol *= -1; return t; } if (A.symbol == 1 && B.symbol == -1) { return add(A, B); } if (A.symbol == -1 && B.symbol == 1) { bigint a = add(A, B); a.symbol = -1; return a; } return sub(A, B); } template<class T, class Y> friend bt operator-(T A, Y B) { return SUB(A, B); } static bigint mul(bt A, bt B) { int len1 = A.size(), len2 = B.size(), cnt = len1 + len2; VC x; long long *c = new long long[len1 + len2 + 10], t = 0; f(i, 0, len1 + len2 + 8)c[i] = 0; f(i, 0, len1 - 1)f(j, 0, len2 - 1)c[i + j] += A[i] * B[j]; f(i, 0, cnt - 1) { t += c[i]; c[i] = t % 10; t /= 10; } while (t) { c[++cnt] = t % 10; t /= 10; } while (cnt >= 1 && c[cnt] == 0) cnt--; x.assign(c, c + cnt + 1); bigint ans(x); delete[] c; return {x}; } friend bigint MUL(bt A, bt B) { if (A.symbol * B.symbol == 1)return mul(A, B); else { bt t = mul(A, B); t.symbol = -1; return t; } } template<class T, class Y> friend bt operator*(T A, Y B) { return MUL(A, B); } static bigint smilediv(bt A, int b) { char *cha = new char[A.size()], *ans = new char[A.size()]; int l = A.size(), m = l - 1, x = 0; std::copy(A._a.begin(), A._a.end(), cha); rf(i, l - 1, 0) { ans[i] = (x * 10 + cha[i]) / b; x = (cha[i] + x * 10) % b; } while (!ans[m] && m >= 1)m--; bigint z(0); z._a.assign(ans, ans + m + 1); delete []cha; delete[]ans; return z; } static bigint div(bt A, bt B) { if (A < B)return {0}; bt l = 1, r = A; while (l <= r) { if (r.symbol == -1)exit(1); bt mid = smilediv(l + r, 2); if (A < mid * B)r = mid - 1; else l = mid + 1; } return r; } friend bigint DIV(bt A, bt B) { bt t = div(A, B); if (A.symbol * B.symbol == -1)t.symbol = -1; return t; } template<class T, class Y>friend bt operator/(T A, Y B) { return DIV(A, B); } static bigint mod(bt A, bt B) { bt x = A / B; return A - x * B; } friend bigint MOD(bt A, bt B) { return mod(mod(A, B) + B, B); } template<class T, class Y> friend bt operator%(T A, Y B) { return MOD(A, B); } bigint(VC c) : _a(std::move(c)) { } bigint() { _a.assign({0}); } template<class T>bigint(T z) { _a.reserve(50); _a.clear(); string s; if (typeid(z) != typeid(const char *) && typeid(z) != typeid(string))s = to_string((long long) z); else s = z; if (s[0] != '-') { rf(i, s.size() - 1, 0)_a.pb(s[i] - '0'); } else { rf(i, s.size() - 1, 1)_a.pb(s[i] - '0'); symbol = -1; } } friend std::ostream &operator<<(std::ostream &os, bt cc) { if (cc.symbol == -1 && !(cc._a.size() == 1 && cc[0] == 0))os << '-'; rf(i, cc.size() - 1, 0)os << int(cc[i]); return os; } friend std::istream &operator>>(std::istream &is, bt &cc) { cc._a.clear(); std::string x; is >> x; if (x[0] == '-') { cc.symbol = -1; rf(i, x.size() - 1, 1)cc._a.pb(x[i] - '0'); } else rf(i, x.size() - 1, 0)cc._a.pb(x[i] - '0'); return is; } template<class T>bool operator<(T A) { bigint other = A; if (this->symbol == -1 && other.symbol == 1 && !(*(this) == other))return 1; if (this->size() < other.size())return 1; else if (this->size() > other.size())return 0; else if (this->size() == other.size())rf(i, this->size() - 1, 0) { if ((*this)[i] < other[i])return 1; } return 0; } bool operator<(bigint A) { bigint other = A; if (this->symbol == -1 && other.symbol == 1 && !(*(this) == other))return 1; if (this->size() < other.size())return 1;; if (this->size() > other.size())return 0; if (this->size() == other.size())rf(i, this->size() - 1, 0) { if ((*this)[i] < other[i])return 1; else if ((*this)[i] > other[i])return 0; } return 0; } template<class T>bool operator==(T A) { bigint other(A); if (other._a == this->_a && other.symbol == this->symbol || (this->size() == 1 && this->symbol == -1 && other.symbol == 1 && other.size() == 1 && (*this)[0] == 0 && other[0] == 0))return 1; return 0; } template<class T>bool operator>(T A) { bigint other(A); if (*this < other == 0 && (*this == other) == 0)return true; return false; } template<class T>bool operator<=(T A) { bigint other(A); if (*(this) < other || *this == other)return 1; return 0; } template<class T>bool operator>=(T A) { bigint other(A); if (*(this) > other || *this == other)return 1; return 0; } bigint operator-() { auto t = *this; t.symbol *= -1; return t; } bigint operator+=(bt other) { *this = *this + other; return *this; } bigint operator-=(bt other) { *this = *this - other; return *this; } bigint operator*=(bt other) { *this = *this * other; return *this; } bigint operator/=(bt other) { *this = *this / other; return *this; } bigint operator%=(bt other) { *this = *this % other; return *this; } bigint operator++(int) { bt t = *this; *this = *this + 1; return t; } bigint operator++() { *this = *this + 1; return *this; } bigint operator--(int) { bt t = *this; *this = *this - 1; return t; } bigint operator--() { *this = *this - 1; return *this; } operator int() const { int t = 0; for (int i = _a.size() - 1; i >= 0; i--) { t = t * 10 + _a[i]; } return t; } bigint fact() { bigint res = 1; for (bigint i = 1; i <= *this; i++) res *= i; return res; }};```