一份完整的高精模板
收藏不亏!!
直接贴板子就完事了 妈妈再也不用担心我的下标打错了qwq/大雾
没有压位 没有小数点 没有符号
使用vector
实现 有较大的常数 但没有长度限制
通过了AcWing.791-794的测试
像使用
int
一样使用bint
#include <bits/stdc++.h>
using namespace std;
struct bint:vector<int>
{
void format();
bint(int n)
{
do push_back(n % 10), n /= 10; while (n);
}
bint(long long n)
{
do push_back(n % 10), n /= 10; while (n);
}
bint(string s)
{
for (int i = s.size() - 1; i >= 0; i --) push_back(s[i] - '0');
}
bint()
{
}
};
istream& operator>>(istream& in, bint& n);
ostream& operator<<(ostream& out, bint n);
bool operator<(bint a, bint b);
bool operator<=(bint a, bint b);
bool operator>(bint a, bint b);
bool operator>=(bint a, bint b);
bool operator==(bint a, bint b);
bool operator!=(bint a, bint b);
bint operator+(bint a, bint b);
bint operator-(bint a, bint b);
bint operator*(bint a, bint b);
bint operator/(bint a, bint b);
bint operator%(bint a, bint b);
template<typename T>
bint operator*(bint a, T b);
template<typename T>
bint operator/(bint a, T b);
template<typename T>
bint operator%(bint a, T b);
template<typename T>
bint operator*(T a, bint b);
bint divmode(bint& a, bint b);
template<typename T>
bint divmode(bint a, T b, T& r);
template<typename T>
void operator+=(T& a, T b);
template<typename T>
void operator-=(T& a, T b);
template<typename T>
void operator*=(T& a, T b);
template<typename T>
void operator/=(T& a, T b);
template<typename T>
void operator%=(T& a, T b);
void operator--(bint& a);
void operator++(bint& a);
void bint::format()
{
while(size() > 1 && back() == 0) pop_back();
}
istream& operator>>(istream& in, bint& n)
{
string s;
in >> s;
n.clear();
for (int i = s.size() - 1; i >= 0; i --) n.push_back(s[i] - '0');
return in;
}
ostream& operator<<(ostream& out, bint n)
{
for (int i = n.size() - 1; i >= 0; i --) out << n[i];
return out;
}
bool operator<(bint a, bint b)
{
if (a.size() != b.size()) return a.size() < b.size();
for (int i = a.size() - 1; i >= 0; i --)
if (a[i] != b[i])
return a[i] < b[i];
return false;
}
bool operator<=(bint a, bint b)
{
return a < b || a == b;
}
bool operator>(bint a, bint b)
{
return !(a <= b);
}
bool operator>=(bint a, bint b)
{
return !(a < b);
}
bool operator==(bint a, bint b)
{
if (a.size() != b.size()) return false;
for (int i = a.size() - 1; i >= 0; i --)
if (a[i] != b[i])
return false;
return true;
}
bool operator!=(bint a, bint b)
{
return !(a == b);
}
bint operator+(bint a, bint b)
{
int t = 0;
bint c;
for (int i = 0; i < a.size() || i < b.size(); i ++)
{
if (i < a.size()) t += a[i];
if (i < b.size()) t += b[i];
c.push_back(t % 10);
t /= 10;
}
if (t) c.push_back(t);
return c;
}
bint operator-(bint a, bint b)
{
if (b > a)
{
cerr << "Error occurs at BigInteger operator-(BigInteger, BigInteger)" << endl;
cerr << "A negative result is produced" << endl;
return a;
}
int t = 0;
bint c;
for (int i = 0; i < a.size() ; i ++)
{
t += a[i];
if (i < b.size()) t -= b[i];
if (t < 0) c.push_back(t + 10), t = -1;
else c.push_back(t), t = 0;
}
c.format();
return c;
}
bint operator*(bint a, bint b)
{
bint c;
c.assign(a.size() + b.size() - 1, 0);
for(int i = 0; i < a.size(); i ++)
for(int j = 0; j < b.size(); j ++)
c[i + j] += a[i] * b[j];
for (int i = 0; i < c.size() - 1; i ++)
if (c[i] >= 10)
c[i + 1] += c[i] / 10, c[i] %= 10;
if (c[c.size() - 1] >= 10) c.push_back(c[c.size() - 1] / 10), c[c.size() - 2] %= 10;
c.format();
return c;
}
bint operator/(bint a, bint b)
{
return divmode(a, b);
}
bint operator%(bint a, bint b)
{
divmode(a, b);
return a;
}
template<typename T>
bint operator*(bint a, T b)
{
bint c;
T t = 0;
for (int i = 0; i < a.size() || t; i ++)
{
if (i < a.size()) t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
c.format();
return c;
}
template<typename T>
bint operator*(T a, bint b)
{
return b * a;
}
template<typename T>
bint operator/(bint a, T b)
{
T r = 0;
return divmode(a, b, r);
}
template<typename T>
T operator%(bint a, T b)
{
T r;
divmode(a, b, r);
return r;
}
bint divmode(bint& a, bint b)
{
if (b == 0)
{
cerr << "Error occurs at BigInteger operator/(BigInteger, BigInteger)" << endl;
cerr << "Divided by zero" << endl;
return a;
}
bint c, d, e;
for (int i = a.size() - b.size(); a >= b; i --)
{
d.clear(), d.assign(i + 1, 0), d.back() = 1;
int l = 0, r = 9, m;
while (l < r)
{
m = l + r + 1 >> 1;
e = b * d * m;
if (e <= a) l = m;
else r = m - 1;
}
a -= b * d * l, c += d * l;
}
return c;
}
template<typename T>
bint divmode(bint a, T b, T& r)
{
bint c;
r = 0;
for (int i = a.size() - 1; i >= 0; i -- )
{
r = r * 10 + a[i];
c.push_back(r / b);
r %= b;
}
reverse(c.begin(), c.end());
c.format();
return c;
}
template<typename T>
void operator+=(T& a, T b)
{
a = a + b;
}
template<typename T>
void operator-=(T& a, T b)
{
a = a - b;
}
template<typename T>
void operator*=(T& a, T b)
{
a = a * b;
}
template<typename T>
void operator/=(T& a, T b)
{
a = a / b;
}
template<typename T>
void operator%=(T& a, T b)
{
a = a % b;
}
void operator--(bint& a)
{
a -= bint(1);
}
void operator++(bint& a)
{
a += bint(1);
}
int main()
{
bint a, b;
cin >> a >> b;
cout << a + b;
return 0;
}
确实好用,如果有压位的板子就更好了qwq
NB
NB
楼主能增加负数吗?
现在还只能支持正整数