src/jiangly/ds/143-BigInt.hpp
Code
/** 高精度(BigInt)
* 2023-09-11: https://qoj.ac/submission/176420
**/
constexpr int N = 1000;
struct BigInt {
int a[N];
BigInt(int x = 0) : a{} {
for (int i = 0; x; i++) {
a[i] = x % 10;
x /= 10;
}
}
BigInt &operator*=(int x) {
for (int i = 0; i < N; i++) {
a[i] *= x;
}
for (int i = 0; i < N - 1; i++) {
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
return *this;
}
BigInt &operator/=(int x) {
for (int i = N - 1; i >= 0; i--) {
if (i) {
a[i - 1] += a[i] % x * 10;
}
a[i] /= x;
}
return *this;
}
BigInt &operator+=(const BigInt &x) {
for (int i = 0; i < N; i++) {
a[i] += x.a[i];
if (a[i] >= 10) {
a[i + 1] += 1;
a[i] -= 10;
}
}
return *this;
}
};
std::ostream &operator<<(std::ostream &o, const BigInt &a) {
int t = N - 1;
while (a.a[t] == 0) {
t--;
}
for (int i = t; i >= 0; i--) {
o << a.a[i];
}
return o;
}
#line 1 "src/jiangly/ds/143-BigInt.hpp"
/** 高精度(BigInt)
* 2023-09-11: https://qoj.ac/submission/176420
**/
constexpr int N = 1000;
struct BigInt {
int a[N];
BigInt(int x = 0) : a{} {
for (int i = 0; x; i++) {
a[i] = x % 10;
x /= 10;
}
}
BigInt &operator*=(int x) {
for (int i = 0; i < N; i++) {
a[i] *= x;
}
for (int i = 0; i < N - 1; i++) {
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
return *this;
}
BigInt &operator/=(int x) {
for (int i = N - 1; i >= 0; i--) {
if (i) {
a[i - 1] += a[i] % x * 10;
}
a[i] /= x;
}
return *this;
}
BigInt &operator+=(const BigInt &x) {
for (int i = 0; i < N; i++) {
a[i] += x.a[i];
if (a[i] >= 10) {
a[i + 1] += 1;
a[i] -= 10;
}
}
return *this;
}
};
std::ostream &operator<<(std::ostream &o, const BigInt &a) {
int t = N - 1;
while (a.a[t] == 0) {
t--;
}
for (int i = t; i >= 0; i--) {
o << a.a[i];
}
return o;
}
Back to top page