超级福音!!!高进度模板
两大传送门
- 原址传送门(有详细讲解)
- 发现地址传送门
废话不多说,直接上代码!
#include <bits/stdc++.h>
struct BigInt {
static const int maxlength = 1005;
int num[maxlength], len;
void clean() {
memset(num, 0, sizeof(num));
len = 1;
}
BigInt() {
clean();
}
void read() {
char str[maxlength];
scanf("%s", str);
len = strlen(str);
for (int i = 1; i <= len; i++) {
num[i] = str[len - i] - '0';
}
}
void write() {
for (int i = len; i; i--) {
printf("%d", num[i]);
}
putchar('\n');
}
void itoBig(int x) {
clean();
while (x != 0) {
num[len++] = x % 10;
x /= 10;
}
if (len != 1) {
len--;
}
}
bool operator <(const BigInt &cmp) const {
if (len != cmp.len) {
return len < cmp.len;
}
for (int i = len; i; i--) {
if (num[i] != cmp.num[i]) {
return num[i] < cmp.num[i];
}
}
return 0;
}
bool operator >(const BigInt &cmp) const {
return cmp < *this;
}
bool operator <=(const BigInt &cmp) const {
return !(cmp < *this);
}
bool operator !=(const BigInt &cmp) const {
return cmp < *this || *this < cmp;
}
bool operator ==(const BigInt &cmp) const {
return !(cmp < *this || *this < cmp);
}
BigInt operator +(const BigInt &A) const {
BigInt S;
S.len = max(len, A.len);
for (int i = 1; i <= S.len; i++) {
S.num[i] += num[i] + A.num[i];
if(S.num[i] >= 10) {
S.num[i] -= 10;
S.num[i + 1]++;
}
}
while(S.num[S.len + 1]) S.len++;
return S;
}
BigInt operator -(const BigInt &A) const {
BigInt S;
S.len = max(len, A.len);
for (int i = 1; i <= S.len; i++) {
S.num[i] += num[i] - A.num[i];
if(S.num[i] < 0) {
S.num[i] += 10;
S.num[i + 1]--;
}
}
while (!S.num[S.len] && S.len > 1) {
S.len--;
}
return S;
}
BigInt operator *(const BigInt &A) const {
BigInt S;
if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) {
return S;
}
S.len = A.len + len - 1;
for (int i = 1; i <= len; i++) {
for (int j = 1; j <= A.len; j++) {
S.num[i + j - 1] += num[i] * A.num[j];
S.num[i + j] += S.num[i + j - 1] / 10;
S.num[i + j - 1] %= 10;
}
}
while (S.num[S.len + 1]) {
S.len++;
}
return S;
}
BigInt operator /(const BigInt &A) const {
BigInt S;
if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) {
return S;
}
BigInt R, N;
S.len = 0;
for (int i = len; i; i--) {
N.itoBig(10);
R = R * N;
N.itoBig(num[i]);
R = R + N;
int flag = -1;
for (int j = 1; j <= 10; j++) {
N.itoBig(j);
if(N * A > R) {
flag = j - 1;
break;
}
}
S.num[++S.len] = flag;
N.itoBig(flag);
R = R - N * A;
}
for (int i = 1; i <= (S.len >> 1); i++) {
swap(S.num[i], S.num[len - i + 1]);
}
while (!S.num[S.len] && S.len > 1) {
S.len--;
}
return S;
}
BigInt operator %(const BigInt &A) const {
BigInt S;
BigInt P = *this / A;
S = *this - P * A;
return S;
}
};
int main(){
return 0;
}