高精度模板
struct BigNum {
int val[N], len = 1;
void init() { val[1] = len = 1; }
BigNum operator + (const BigNum &x) const {
static BigNum t = *this;
t.len = max(t.len, x.len);
for (int i = 1; i <= t.len; i++) t.val[i] += x.val[i];
for (int i = 1; i <= len; i++) t.val[i + 1] += t.val[i] / 10, t.val[i] %= 10;
while (t.val[len] > 9) t.val[len + 1] += t.val[len] / 10, t.val[len] %= 10, t.len++;
return t;
}
BigNum operator - (const BigNum &x) const {
static BigNum t = *this;
t.len = max(t.len, x.len);
for (int i = 1; i <= max(t.len, x.len); i++) t.val[i] -= x.val[i];
for (int i = 1; i <= len; i++)
if (t.val[i] < 0)
t.val[i] += (9 - t.val[i]) / 10 * 10, t.val[i + 1] -= (9 - t.val[i]) / 10 * 10;
while (t.len > 1 && t.val[len] == 0) t.len--;
return t;
}
BigNum operator * (const BigNum &x) const {
static BigNum t = *this;
for (int i = 1; i <= t.len; i++)
for (int j = 1; j <= x.len; j++)
t.val[i + j - 1] += val[i] * x.val[j];
for (int i = 1; i <= t.len + x.len; i++) t.val[i + 1] += t.val[i] / 10, t.val[i] %= 10;
t.len += x.len;
while (t.len > 1 && t.val[len] == 0) t.len--;
return t;
}
BigNum operator / (int x) const{
static BigNum t;
int s = 0;
for (int i = t.len - 1; i >= 0; i--) {
s = s * 10 + t.val[i];
t.val[i] = s / x;
s %= x;
}
while (t.len > 0 && val[t.len - 1] == 0) t.len--;
return t;
}
int cmp(const BigNum &x) const {
if (len != x.len) return len < x.len;
for (int i = len; i >= 1; i--) if (val[i] != x.val[i]) return val[i] < x.val[i];
return 2;
}
bool operator < (const BigNum &x) const {
return !cmp(x);
}
bool operator = (const BigNum &x) const {
return cmp(x) == 2;
}
bool operator > (const BigNum &x) const {
return cmp(x);
}
void print() const{
for (int i = len; i >= 1; i--) printf("%d", val[i]);
puts("");
}
};
标签:const,val,高精度,int,len,return,BigNum,模板
From: https://www.cnblogs.com/louyuxuan/p/18313606