#include #include #include #include typedef long Counter; const int MAX = 1000; class Int { private: char val[MAX]; bool sign; Counter len; bool isValid(); void getSign(); void delZero(); void calPrep(Int &); void outPrep(); Int add(const Int &) const; Int subtract(const Int &) const; public: Int(); Int(char []); Int(const int &); ~Int(); friend istream &operator>>(istream&, Int &); friend ostream &operator<<(ostream&, const Int &); Int operator=(const Int &); Int operator=(const int &); Int operator=(char []); bool operator>(const Int &) const; bool operator<(const Int &) const; bool operator==(const Int &) const; bool operator!=(const Int &) const; bool operator<=(const Int &) const; bool operator>=(const Int &) const; Int operator+(const Int &) const; Int operator-(const Int &) const; Int operator*(const Int &) const; Int operator/(const Int &) const; Int operator%(const Int &) const; Int operator++(); Int operator--(); Int operator++(int n); Int operator--(int n); Int operator+=(const Int &); Int operator-=(const Int &); Int operator*=(const Int &); Int operator/=(const Int &); Int operator%=(const Int &); Int abs() const; bool isPrime() const; }; /***************** I/O overloading ******************/ istream &operator>>(istream &in, Int &n) { Int temp; in>>temp.val; if(temp.isValid()) { strcpy(n.val,temp.val); n.getSign(); n.delZero(); } return in; } ostream &operator<<(ostream &out, const Int &n) { if(n.sign) out<<'-'; else cout<<'+'; out<1) || isdigit(val[0])) { for(i = 1; i < Counter(strlen(val)) && isdigit(val[i]); i++); if(i == Counter(strlen(val))) flag = true; else flag = false; } else flag = false; return flag; } void Int::getSign() { switch(val[0]) { case '+': sign = false; val[0] = '0'; break; case '-': sign = true; val[0] = '0'; break; default: sign = false; } } void Int::delZero() { strrev(val); for(Counter i = strlen(val)-1; val[i] == '0'; i--) val[i] = 0; strrev(val); if(strlen(val)==0) strcat(val,"0"); len = strlen(val); } void Int::calPrep(Int &n) { strrev(val); strrev(n.val); while(strlen(val)strlen(n.val)) strcat(n.val, "0"); len = strlen(val); n.len = strlen(n.val); strrev(val); strrev(n.val); for(Counter i = 0; i0;i--) { while(val[i] < 0) { val[i]+=10; val[i-1]-=1; } while(val[i] >9) { val[i]-=10; val[i-1]+=1; } } for(i = 0; i'9') { strrev(val); strcat(val,"0"); len++; strrev(val); while(val[1]>'9') { val[1]-=10; val[0]+=1; } } delZero(); } Int Int::add(const Int &n) const { Int a,b,sum; a = (*this); b = n; b.calPrep(a); for(Counter i = 0; ib) { b.calPrep(a); for(Counter i = 0; istrlen(n.val)) flag = false; else { Counter i; for(i = 0; i(const Int &n) const { return !((*this)<=n); } bool Int::operator >=(const Int &n) const { return !((*this)0;i--) { temp = arr[i]; arr[i] = arr[i]%10; arr[i-1] +=temp/10; } for(i = prod.len-1; i>0;i--) prod.val[i] = char(arr[i]+'0'); temp = arr[0]; while(temp > 10) { prod.val[0] = char(temp%10+'0'); temp = temp/10; strrev(prod.val); strcat(prod.val,"0"); strrev(prod.val); } prod.val[0] = char(temp+'0'); prod.delZero(); prod.sign = (a.sign!=b.sign); return prod; } Int Int::operator /(const Int &n) const { Int a, b, quot, curra, currb, div; char temp[2]; temp[1]='\0'; if(n==0) return n; a = (*this); b = n; a.sign = false; b.sign = false; curra.sign = false; int i=0; while(i=b) { if(i < a.len) { temp[0]=a.val[i]; strcat(curra.val, temp); curra.len++; curra.delZero(); i++; } div = 0; currb = b; while(currb<=curra) { currb = currb + b; div = div+1; } quot=(quot*10)+div; curra = curra - div*b; curra.sign = false; } quot.sign = ((*this).sign!=n.sign); return quot; } Int Int::operator %(const Int &n) const { return ((*this)-((*this)/n)*n); } /****************************************************/ /**************** Unary Operators *******************/ Int Int::operator ++() { (*this)=(*this)+1; return (*this); } Int Int::operator --() { (*this)=(*this)-1; return (*this); } Int Int::operator++(int n) { Int temp = (*this); *this = (*this)+1; return temp; } Int Int::operator--(int n) { Int temp = (*this); *this = (*this)-1; return temp; } /****************************************************/ /*************** Shorthand Operators ****************/ Int Int::operator+=(const Int &n) { (*this)=(*this)+n; return *this; } Int Int::operator-=(const Int &n) { (*this)=(*this)-n; return *this; } Int Int::operator*=(const Int &n) { (*this)=(*this)*n; return *this; } Int Int::operator/=(const Int &n) { (*this)=(*this)/n; return *this; } Int Int::operator%=(const Int &n) { (*this)=(*this)%n; return *this; } /****************************************************/ /***************** Integer Methods ******************/ Int Int::abs() const { Int n=(*this); n.sign = false; return n; } bool Int::isPrime() const { if((*this)<2) return false; for(Int i = 2; i<(*this); i++) { if(((*this)%i)==0) return false; } return true; } /****************************************************/ Int factorial(Int x) { if(x>"1") return x*factorial(x-"1"); return "1"; } /********************* Driver ***********************/ void main() { Int n, m, prod; cin>>n>>m; prod = 23; prod = "999999999999912391238712873"; cout<