1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <bits/stdc++.h> using namespace std; struct BigInteger { typedef long long ll; static const ll DIGIT = 100000000; static const int DIGIT_LEN = 9; static const int SIZE = 305; ll a[SIZE]; int len, sig; BigInteger() { len = 1; memset(a, 0, sizeof(a)); } BigInteger(char *s) { len = 1; memset(a, 0, sizeof(a)); *this = s; } BigInteger(ll x) { len = 1; memset(a, 0, sizeof(a)); *this = x; } inline void clear() { memset(a, 0, sizeof(ll) * (len + 1)); len = 1; sig = 0; } inline void fix() { while (len > 1 && !a[len]) len--; } inline void minus(BigInteger &a, BigInteger &b, BigInteger &c) { if (b.sig) { b.sig = 0, add(a, b, c), b.sig = 1; return; } if (a.sig) { a.sig = 0, add(a, b, c), a.sig = 1, c.sig = 1; return; } c.clear(); register int flag = 0, i = 1; BigInteger *x = &a, *y = &b; if (a < b) flag = 1, swap(x, y); for (; i <= x->len; i++) { c.a[i] += x->a[i] - y->a[i]; if (c.a[i] < 0) c.a[i] += DIGIT, c.a[i + 1]--; } c.len = i; c.fix(); c.sig = flag; } inline void add(BigInteger &a, BigInteger &b, BigInteger &c) { if (b.sig) { b.sig = 0, minus(a, b, c), b.sig = 1; return; } if (a.sig) { a.sig = 0, minus(b, a, c), a.sig = 1; return; } c.clear(); int i = 1, l = max(a.len, b.len); ll k = 0; for (; i <= l || k; ++i) { c.a[i] = a.a[i] + b.a[i] + k; k = c.a[i] / DIGIT; if (c.a[i] >= DIGIT) c.a[i] %= DIGIT; } c.len = i; c.fix(); } inline void multiply(BigInteger &a, BigInteger &b, BigInteger &c) { c.clear(); for (register int i = 1; i <= a.len; i++) { for (register int j = 1; j <= b.len; j++) { register int pos = i + j - 1; c.a[pos] += a.a[i] * b.a[j]; c.a[pos + 1] += c.a[pos] / DIGIT; c.a[pos] %= DIGIT; } } c.len = a.len + b.len; c.fix(); c.sig = a.sig ^ b.sig; if (c.a[1] == 0 && c.len == 1) c.sig = 0; } inline void divide(BigInteger &a, ll b, BigInteger &c) { c.clear(); ll t = 0; for (int i = len; i; --i) { c.a[i] = (a.a[i] + t) / b; t = ((a.a[i] + t) % b) * DIGIT; } c.len = len; c.fix(); } inline void divide(BigInteger &a, BigInteger &b, BigInteger &c) { c.clear(); BigInteger l, r = a, mid, TP, ONE = (ll)1; while (l <= r) { add(l, r, TP); divide(TP, 2, mid); multiply(mid, b, TP); if (TP <= a) add(mid, ONE, l); else minus(mid, ONE, r); } minus(l, ONE, c); c.sig = a.sig ^ b.sig; if (c.a[1] == 0 && c.len == 1) c.sig = 0; } inline BigInteger sqrt() { BigInteger l, r = *this, mid, TP, ONE = (ll)1; while (l <= r) { add(l, r, TP); divide(TP, 2, mid); multiply(mid, mid, TP); if (TP <= *this) add(mid, ONE, l); else minus(mid, ONE, r); } minus(l, ONE, TP); return TP; } inline bool operator<(BigInteger &b) { if (b.sig && !sig) return 0; if (!b.sig && sig) return 1; if (b.sig && sig) { sig = b.sig = 0; bool ret = b < *this; sig = b.sig = 1; return ret; } if (len != b.len) return len < b.len; for (register int i = len; i >= 1; i--) if (a[i] < b.a[i]) return true; else if (a[i] > b.a[i]) return false; return false; } inline BigInteger& operator= (ll b) { clear(); len = 0; if (b == 0) { len = 1; return *this; } if (b < 0) sig = 1, b = -b; while (b) { a[++len] = b % DIGIT; b /= DIGIT; } return *this; } inline BigInteger& operator= (char *s) { clear(); if (s[0] == '-') sig = 1, ++s; len = 0; int l = strlen(s), t = 0, k = 1; for (register int i = l - 1; i >= 0; i--) { t = t + (s[i] ^ '0') * k; k = (k << 1) + (k << 3); if (k >= DIGIT) a[++len] = t % DIGIT, t = 0, k = 1; } if (k != 1) a[++len] = t % DIGIT; return *this; } inline BigInteger& operator= (const BigInteger &x) { clear(); memcpy(a, x.a, sizeof(ll) * (x.len + 1)); len = x.len, sig = x.sig; return *this; } inline BigInteger operator+ (BigInteger x) { BigInteger c; add(*this, x, c); return c; } inline BigInteger operator- (BigInteger x) { BigInteger c; minus(*this, x, c); return c; } inline BigInteger operator* (BigInteger x) { BigInteger c; multiply(*this, x, c); return c; } inline BigInteger operator/ (BigInteger x) { BigInteger c; divide(*this, x, c); return c; } inline BigInteger operator/ (ll x) { BigInteger c; divide(*this, x, c); return c; } inline BigInteger& operator+= (BigInteger x) { BigInteger c; add(*this, x, c); return *this = c; } inline BigInteger& operator-= (BigInteger x) { BigInteger c; minus(*this, x, c); return *this = c; } inline BigInteger& operator*= (BigInteger x) { BigInteger c; multiply(*this, x, c); return *this = c; } inline BigInteger& operator/= (BigInteger x) { BigInteger c; divide(*this, x, c); return *this = c; } inline BigInteger& operator/= (ll x) { BigInteger c; divide(*this, x, c); return *this = c; } inline BigInteger& operator++ () { return *this += 1; } inline BigInteger operator++ (int) { BigInteger ret = *this; ++*this; return ret; } inline BigInteger& operator-- () { return *this -= 1; } inline BigInteger operator-- (int) { BigInteger ret = *this; --*this; return ret; } inline bool operator> (BigInteger &x) { return x < *this; } inline bool operator== (BigInteger &x) { return x <= *this && x >= *this; } inline bool operator<= (BigInteger &x) { return !(x < *this); } inline bool operator>= (BigInteger &x) { return !(x > *this); } inline void print() { if (sig) putchar('-'); printf("%d", (int)a[len]); char od[8]; od[0] = '%'; od[1] = '0'; sprintf(od + 2, "%d", DIGIT_LEN - 1); int l = strlen(od); od[l] = 'd'; od[l + 1] = '\0'; for (register int i = len - 1; i >= 1; i--) printf(od, (int)a[i]); } };
|