char signChar(BigInteger b) {
return (b.sign==NEGATIVE) ? '-' : ' ';
}
BigInteger getBigInt(void) {
BigInteger b;
scanf("%s",b.number);
if(b.number[0]=='-') {
xstrcpy(b.number, &b.number[1]);
b.sign=NEGATIVE;
}
else {
b.sign=NON_NEGATIVE;
}
return b;
}
BigInteger addBigInt(BigInteger b1, BigInteger b2) {
BigInteger result;
int l1,l2,l;
int i;
int i1,i2,carry;
if(b1.sign != b2.sign) {
b2.sign = (b2.sign+1)%2;
return subBigInt(b1,b2);
}
l1 = xstrlen(b1.number);
l2 = xstrlen(b2.number);
l = l1<l2?l2:l1;
l1--;l2--;
result.number[l]=NULL;
carry=0;
for(i=0; i<l; i++, l1--, l2--) {
i1 = (l1>=0 ? ( b1.number[l1]) - '0' : 0);
i2 = (l2>=0 ? ( b2.number[l2]) - '0' : 0);
result.number[i] = ( i1 + i2 + carry)%10 + '0';
carry = ( i1 + i2 + carry)/10;
}
if(carry != 0) {
result.number[i++] = carry + '0';
}
result.number[i] = NULL;
xstrcpy(xstrrev(result.number),result.number);
result.sign = b1.sign;
return result;
}
BigInteger subBigInt(BigInteger b1, BigInteger b2) {
BigInteger result;
int l1,l2,l;
int i;
int i1,i2,carry;
if(b1.sign != b2.sign) {
b2.sign = (b2.sign+1)%2;
return addBigInt(b1,b2);
}
l1 = xstrlen(b1.number);
l2 = xstrlen(b2.number);
l = l1<l2?l2:l1;
l1--;l2--;
result.number[l]=NULL;
result.sign = b1.sign;
carry=0;
for(i=l-1; i>=0; i--, l1--, l2--) {
i1 = (l1>=0 ? ( b1.number[l1]) - '0' : 0);
i2 = (l2>=0 ? ( b2.number[l2]) - '0' : 0);
result.number[i] = ( i1 - i2 + carry + 10)%10 + '0';
carry = ( i1 - i2 + carry)<0?-1:0;
}
if(carry!=0) {
for(i=l-1; i>=0; i--) {
result.number[i] = '9' - result.number[i] + '0';
}
if(result.sign == NON_NEGATIVE)
result = addBigInt(result, ONE);
else
result = subBigInt(result, ONE);
result.sign = (result.sign+1)%2;
}
return result;
}
BigInteger mulBigInt(BigInteger b1, BigInteger b2) {
int l1,l2;
int i;
BigInteger temp,result;
temp = ZERO;
result = ZERO;
l1 = xstrlen(b1.number);
l2 = xstrlen(b2.number);
for(i=0; i<l2; i++) {
int c,j,num,carry=0;
for(c=0,j=0;c<i;c++,j++) {
temp.number[j] = '0';
}
for(c=0; c<l1 || carry!=0; c++,j++) {
num = (b2.number[l2-i-1] - '0')
* (c<l1 ? b1.number[l1-c-1] - '0' : 0)
+ carry;
temp.number[j] = num%10 +'0';
carry = num/10;
}
temp.number[j]=NULL;
xstrcpy(temp.number, xstrrev(temp.number) );
result = addBigInt(temp, result);
}
if(b1.sign != b2.sign) {
result.sign = NEGATIVE;
}
return result;
}