Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<ll base = 1000000000, ll hsize = 400> class BigInteger {
- private:
- ll data[hsize], size;
- public:
- BigInteger() {
- size = 1;
- reset(data, 0);
- }
- BigInteger(ll a) {
- size = 0;
- reset(data, 0);
- while (a) {
- data[size++] = a % base;
- a /= base;
- }
- }
- BigInteger(const BigInteger<base, hsize> &d) {
- this->size = d.size;
- memcpy(this->data, d.data, sizeof(d.data));
- }
- BigInteger operator + (BigInteger a) {
- BigInteger d;
- int n = max(this->size, a.size), mem = 0;
- for (int i = 0; i < n; ++i) {
- d.data[i] = (this->data[i] + a.data[i] + mem) % base;
- mem = (this->data[i] + a.data[i] + mem) / base;
- }
- d.size = n;
- if (mem) d.data[d.size++] = mem;
- return d;
- }
- BigInteger operator - (BigInteger a) {
- BigInteger d;
- int n = this->size;
- ll mem = 0;
- for (int i = 0; i < n; ++i) {
- if (this->data[i] - mem < a.data[i]) {
- d.data[i] = this->data[i] - mem + base - a.data[i];
- mem = 1;
- }
- else {
- d.data[i] = this->data[i] - mem - a.data[i];
- mem = 0;
- }
- }
- while (n > 1 && d.data[n - 1] == 0) n--;
- d.size = n;
- return d;
- }
- void Show() {
- printf("%d", (int)this->data[this->size - 1]);
- for (int i = this->size - 2; i >= 0; --i)
- printf("%09d", (int)this->data[i]);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement