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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| class bign { private: vector<int> num; bool neg = false; void trim() { while (num.size() > 1 && num.back() == 0) num.pop_back(); if (num.size() == 1 && num[0] == 0) neg = false; } bign _abs() const { bign res = *this; res.neg = false; return res; } int abs_compare(const bign& other) const { if (num.size() != other.num.size()) return num.size() > other.num.size() ? 1 : -1; for (int i = num.size() - 1; i >= 0; i--) { if (num[i] != other.num[i]) return num[i] > other.num[i] ? 1 : -1; } return 0; } static void fft(vector<complex<double>>& a, bool invert) { int n = a.size(); for (int i = 1, j = 0; i < n; i++) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { double ang = 2 * PI / len * (invert ? -1 : 1); complex<double> wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { complex<double> w(1); for (int j = 0; j < len / 2; j++) { complex<double> u = a[i + j], v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) { for (complex<double>& x : a) x /= n; } } public: bign() : num({0}), neg(false) {} bign(long long x) { *this = x; } bign(const string& s) { from_string(s); } void from_string(const string& s) { num.clear(); neg = false; int start = 0; if (s[0] == '-') { neg = true; start = 1; } for (int i = s.size() - 1; i >= start; i--) { if (isdigit(s[i])) num.push_back(s[i] - '0'); } trim(); } bign& operator=(long long x) { num.clear(); neg = (x < 0); x = abs(x); if (x == 0) num.push_back(0); while (x) { num.push_back(x % 10); x /= 10; } return *this; } string to_string() const { if (num.empty()) return "0"; string s; if (neg) s += '-'; for (int i = num.size() - 1; i >= 0; i--) s += char(num[i] + '0'); return s; } bool operator<(const bign& other) const { if (neg != other.neg) return neg; if (neg) return abs_compare(other) > 0; return abs_compare(other) < 0; } bool operator>(const bign& other) const { return other < *this; } bool operator<=(const bign& other) const { return !(*this > other); } bool operator>=(const bign& other) const { return !(*this < other); } bool operator==(const bign& other) const { return neg == other.neg && num == other.num; } bool operator!=(const bign& other) const { return !(*this == other); } bign operator-() const { bign res = *this; res.neg = !neg; res.trim(); return res; } bign operator+(const bign& other) const { if (neg == other.neg) { bign res = *this; int carry = 0; int max_size = max(num.size(), other.num.size()); res.num.resize(max_size, 0); for (int i = 0; i < max_size; i++) { if (i < other.num.size()) res.num[i] += other.num[i]; res.num[i] += carry; carry = res.num[i] / 10; res.num[i] %= 10; } if (carry) res.num.push_back(carry); return res; } else { if (neg) return other - (-*this); else return *this - (-other); } } bign operator-(const bign& other) const { if (neg != other.neg) { return *this + (-other); } if (abs_compare(other) < 0) { bign res = other - *this; res.neg = !neg; return res; } bign res = *this; int borrow = 0; for (int i = 0; i < res.num.size(); i++) { if (i < other.num.size()) res.num[i] -= other.num[i]; res.num[i] -= borrow; borrow = 0; if (res.num[i] < 0) { res.num[i] += 10; borrow = 1; } } res.trim(); return res; } bign operator*(int x) const { if (x == 0) return bign(0); bign res; res.neg = neg ^ (x < 0); x = abs(x); res.num.resize(num.size() + 10, 0); long long carry = 0; for (int i = 0; i < num.size() || carry; i++) { if (i < num.size()) { carry += (long long)num[i] * x; } res.num[i] = carry % 10; carry /= 10; } res.trim(); return res; }
bign operator*(const bign& other) const { if (*this == 0 || other == 0) return bign(0); bign res; res.neg = neg ^ other.neg; vector<complex<double>> fa(num.begin(), num.end()); vector<complex<double>> fb(other.num.begin(), other.num.end()); int n = 1; while (n < fa.size() + fb.size()) n <<= 1; fa.resize(n); fb.resize(n); fft(fa, false); fft(fb, false); for (int i = 0; i < n; i++) fa[i] *= fb[i]; fft(fa, true); res.num.resize(n); int carry = 0; for (int i = 0; i < n; i++) { int digit = int(fa[i].real() + 0.5) + carry; carry = digit / 10; res.num[i] = digit % 10; } while (carry) { res.num.push_back(carry % 10); carry /= 10; } res.trim(); return res; } bign operator/(const bign& other) const { if (other == 0) throw runtime_error("Division by zero"); if (abs_compare(other) < 0) return bign(0); bign res, cur; res.num.resize(num.size()); res.neg = neg ^ other.neg; for (int i = num.size() - 1; i >= 0; i--) { cur.num.insert(cur.num.begin(), num[i]); cur.trim(); int quotient = 0; while (cur >= other._abs()) { cur = cur - other._abs(); quotient++; } res.num[i] = quotient; } res.trim(); return res; } bign operator%(const bign& other) const { bign quotient = *this / other; return *this - quotient * other; } friend ostream& operator<<(ostream& os, const bign& n) { os << n.to_string(); return os; } friend istream& operator>>(istream& is, bign& n) { string s; is >> s; n.from_string(s); return is; } };
|