struct BigInt {
static const int MAXN = 1010;
int num[MAXN], len;
BigInt() {clean();}
void clean() {
memset(num, 0, sizeof num);
len = 1;
}
void read() {
char str[MAXN];
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 >= 1; i --)
printf("%d",num[i]);
}
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 >= 1; i --)
if(num[i]!=cmp.num[i])
return num[i]<cmp.num[i];
return false;
}
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 >= 1; 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 / 2; 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;
}
};