Sunday, March 21, 2010

Bigint Library by Jan vai


Thanks to Jan vai for this, It will be a great help... :)

/*
Author : Jan
Problem Name : Big int for contest
Algorithm :
Complexity :
*/

#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

struct Bigint {
string a;
int sign;

Bigint() {}
Bigint( string b ) { (*this) = b; }
int size() { return a.size(); }
Bigint inverseSign() { sign *= -1; return (*this); }
Bigint normalize( int newSign ) {
sign = newSign;
for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- ) a.erase(a.begin() + i);
if( a.size() == 1 && a[0] == '0' ) sign = 1;
return (*this);
}
void operator = ( string b ) {
a = b[0] == '-' ? b.substr(1) : b;
reverse( a.begin(), a.end() );
this->normalize( b[0] == '-' ? -1 : 1 );
}
bool operator < ( const Bigint &b ) const {
if( a.size() != b.a.size() ) return a.size() < b.a.size();
for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] ) return a[i] < b.a[i];
return false;
}
Bigint operator + ( Bigint b ) {
if( sign != b.sign ) return (*this) - b.inverseSign();
Bigint c;
for( int i = 0, carry = 0; i < (int)a.size() || i < (int)b.size() || carry; i++ ) {
carry += (i < (int)a.size() ? a[i] - 48 : 0) + (i < (int)b.a.size() ? b.a[i] - 48 : 0);
c.a += (carry % 10 + 48);
carry /= 10;
}
return c.normalize(sign);
}
Bigint operator - ( Bigint b ) {
if( sign != b.sign ) return (*this) + b.inverseSign();
if( (*this) < b ) return (b - (*this)).inverseSign();
Bigint c;
for( int i = 0, borrow = 0; i < (int)a.size(); i++ ) {
borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
borrow = borrow >= 0 ? 0 : 1;
}
return c.normalize(sign);
}
Bigint operator * ( Bigint b ) {
Bigint c("0");
for( int i = 0, k = a[i]; i < (int)a.size(); i++, k = a[i] ) {
while(k-- - 48) c = c + b;
b.a.insert(b.a.begin(), '0');
}
return c.normalize(sign * b.sign);
}
Bigint operator / ( Bigint b ) {
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ) ;
Bigint c("0"), d;
for( int j = 0; j < (int)a.size(); j++ ) d.a += "0";
int dSign = sign * b.sign; b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b, d.a[i]++;
}
return d.normalize(dSign);
}
Bigint operator % ( Bigint b ) {
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ) ;
Bigint c("0");
int cSign = sign * b.sign; b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b;
}
return c.normalize(cSign);
}
void print() {
if( sign == -1 ) putchar('-');
for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
}
};

int main() {
Bigint a, b, c;
a = "511";
b = "10";

c = a + b;
c.print();
putchar('\n');

c = a - b;
c.print();
putchar('\n');

c = a * b;
c.print();
putchar('\n');

c = a / b;
c.print();
putchar('\n');

c = a % b;
c.print();
putchar('\n');

return 0;
}

Hope this helps my friends as well. Actually many of us are afraid of big integers, and also the above code may look hard, but this is actually what we do when performing these operations in real life... Use with caution, not a very first algorithm. Do not attempt to use *, /, % if numbers are 103 or more digits long.

5 comments:

  1. Is it alright if I borrow this?

    Truth be told, I'm afraid of bignums -- I don't like reinventing the wheel when a single mistake can skew the whole solution.

    ReplyDelete
  2. haha, Sure, I myself have borrowed it from some other one :)

    ReplyDelete
  3. I used this code in one of the problems and apparently the code was running too slow. When I ran the same code using Java BigInteger class, it was particularly fast, enough to get me an AC.
    Since the only variation of parameter in my two codes was the BigInteger class, it seems that this code is slow :(.

    ReplyDelete
  4. @sidi, probably u missed the bottom of the post, I have written about the performance: "Use with caution, not a very first algorithm. Do not attempt to use *, /, % if numbers are 103 or more digits long."

    ReplyDelete
  5. brother, what would I have to write if I want to take input when the program is running?

    ReplyDelete

Catagories

academic study (17) access modifiers (1) algorithm (28) bash (1) beginner (17) bfs (1) bigint (1) binary tree (1) bitwise (4) blogger (5) bpm (2) brainfuck (1) bst (1) c (1) c++ (36) changes (1) character device driver (1) combinatorics (2) command prompt (1) comparator (1) compression (1) computational geometry (2) confusion (1) contest (7) crc (1) cse (3) css (1) customize (1) data structure (10) database (1) decoding (1) design (1) device driver (1) divide and conquer (2) dp (3) driver (1) dynamic programming (9) encoding (1) encryption (1) error (2) esoteric language (2) euler circuit (1) euler path (1) executable (1) expression evaluation (1) extended euclid (1) facebook (1) factorization (1) funny (14) gcd (2) geometry (4) git (1) github (1) graph (7) hashing (1) hiding window (1) hints (5) hopcroft karp (1) huffman (1) jar (1) java (5) javascript (1) jdbc (1) kernel programming (2) lab (1) like (1) linear algebra (3) linux (5) ls (1) makefile (1) math (16) matrix (2) matrix algebra (1) matrix exponentiation (1) matrix multiplication (1) maxflow (1) maximum bipartite matching (2) maximum flow (1) merge sort (3) mistake (1) modular arithmatic (1) module compiling (2) mysql (1) number system (1) number theory (8) online judge (3) operating system (1) os (1) other (8) parallel programming (1) pollard rho (1) primes (3) problem (3) problem classifier (2) problem solving (34) programming (51) pthreaded (1) puzzle (1) python (3) recursion (5) repository (1) shell (1) shell script (1) sieve (1) simulation (1) sort (3) spacing (1) sphere online judge (12) spoj (12) syntax highlighting (1) system programming (4) table tag (1) tc (1) template (4) thread (1) topcoder (1) training (3) tree (1) tutorial (2) ubuntu (1) usaco (1) uva (5) uva online judge (5) vector (1) windows (1)