Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. template<ll base = 1000000000, ll hsize = 400> class BigInteger {
  2.     private:
  3.         ll data[hsize], size;
  4.     public:
  5.         BigInteger() {
  6.             size = 1;
  7.             reset(data, 0);
  8.         }
  9.         BigInteger(ll a) {
  10.             size = 0;
  11.             reset(data, 0);
  12.             while (a) {
  13.                 data[size++] = a % base;
  14.                 a /= base;
  15.             }
  16.         }
  17.         BigInteger(const BigInteger<base, hsize> &d) {
  18.             this->size = d.size;
  19.             memcpy(this->data, d.data, sizeof(d.data));
  20.         }
  21.         BigInteger operator + (BigInteger a) {
  22.             BigInteger d;
  23.             int n = max(this->size, a.size), mem = 0;
  24.             for (int i = 0; i < n; ++i) {
  25.                 d.data[i] = (this->data[i] + a.data[i] + mem) % base;
  26.                 mem = (this->data[i] + a.data[i] + mem) / base;
  27.             }
  28.             d.size = n;
  29.             if (mem) d.data[d.size++] = mem;
  30.             return d;
  31.         }
  32.         BigInteger operator - (BigInteger a) {
  33.             BigInteger d;
  34.             int n = this->size;
  35.             ll mem = 0;
  36.             for (int i = 0; i < n; ++i) {
  37.                 if (this->data[i] - mem < a.data[i]) {
  38.                     d.data[i] = this->data[i] - mem + base - a.data[i];
  39.                     mem = 1;
  40.                 }
  41.                 else {
  42.                     d.data[i] = this->data[i] - mem - a.data[i];
  43.                     mem = 0;
  44.                 }
  45.             }
  46.             while (n > 1 && d.data[n - 1] == 0) n--;
  47.             d.size = n;
  48.             return d;
  49.         }
  50.         void Show() {
  51.             printf("%d", (int)this->data[this->size - 1]);
  52.             for (int i = this->size - 2; i >= 0; --i)
  53.                 printf("%09d", (int)this->data[i]);
  54.         }
  55. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement