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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
| #ifndef _LONGER_H #define _LONGER_H #include<vector> #include<string> #include<sstream> #include<iomanip>
class longer { typedef long long ll; static constexpr ll MAX = 1000000000; static constexpr int DIGIT = 9; bool sign; std::vector<ll> number;
public: longer(); longer(const ll&); longer(const longer&); longer(const char*); longer(const std::string&);
longer operator +()const; longer operator +(const longer&)const; longer operator -()const; longer operator -(const longer&)const; longer operator *(const longer&)const; longer operator /(const longer&)const; longer operator %(const longer&)const;
longer& operator ++(); longer operator ++(int); longer& operator --(); longer operator --(int);
longer& operator =(const ll&); longer& operator =(const longer&); longer& operator =(const char*); longer& operator =(const std::string&);
longer& operator +=(const longer&); longer& operator -=(const longer&); longer& operator *=(const longer&); longer& operator /=(const longer&);
inline bool operator <(const longer&)const; inline bool operator >(const longer&)const; inline bool operator <=(const longer&)const; inline bool operator >=(const longer&)const; inline bool operator ==(const longer&)const; inline bool operator !=(const longer&)const;
friend std::istream& operator >>(std::istream&, longer&); friend std::ostream& operator <<(std::ostream&, const longer&);
int digit()const; static longer lrabs(const longer&); static longer lrsqrt(const longer&);
private: longer(const int, const ll&); static longer& get_addition(longer&, const longer&, bool); static longer& get_subtraction(longer&, const longer&, bool); int get_comparison(const longer&)const; inline ptrdiff_t get_ptrdiff()const; inline longer& get_zero(); };
constexpr longer::ll longer::MAX; constexpr int longer::DIGIT;
longer::longer() { number.push_back(0); } longer::longer(const ll& n) { *this = n; } longer::longer(const longer& n) { *this = n; } longer::longer(const char* s) { *this = s; } longer::longer(const std::string& s) { *this = s; } longer::longer(const int i, const ll& n) : number(i, n), sign(0) {}
longer longer::operator +()const { return *this; }
longer longer::operator +(const longer& n)const { bool judge = get_comparison(n) >= 0; longer aa(judge ? *this : n); auto &bb = (judge ? n : *this); if ((!aa.sign) && (!bb.sign)) return get_addition(aa, bb, 0); if ((!aa.sign) && (bb.sign)) return get_subtraction(aa, bb, !judge); if ((aa.sign) && (!bb.sign)) return get_subtraction(aa, bb, judge); return get_addition(aa, bb, 1); }
longer longer::operator -()const { longer t(*this);t.sign = (t.sign ? 0 : 1);return t; }
longer longer::operator -(const longer& n)const { bool judge = get_comparison(n) >= 0; longer aa(judge ? *this : n); auto &bb = (judge ? n : *this); if ((!aa.sign) && (!bb.sign)) return get_subtraction(aa, bb, !judge); if ((!aa.sign) && (bb.sign)) return get_addition(aa, bb, 0); if ((aa.sign) && (!bb.sign)) return get_addition(aa, bb, 1); return get_subtraction(aa, bb, judge); }
longer longer::operator *(const longer& n)const { int leni = get_ptrdiff(), lenj = n.get_ptrdiff(); longer re(leni + lenj, 0); ll flag; sign == n.sign ? re.sign = 0 : re.sign = 1; for (int i = 0;i < leni;++i) { longer t(n); int j; flag = 0; for (j = 0;j < lenj;++j) { t.number[j] = t.number[j] * number[i] + flag; flag = t.number[j] / MAX; t.number[j] -= flag*MAX; } if (flag) t.number.push_back(flag); flag = 0; for (j = 0;j < t.get_ptrdiff();++j) { re.number[i + j] += t.number[j] + flag; if (re.number[i + j] >= MAX) re.number[i + j] -= MAX, flag = 1; else flag = 0; } if (flag) re.number[i + j] += flag; } while (re.get_ptrdiff() > 1 && !*re.number.rbegin()) re.number.pop_back(); return re.get_zero(); }
longer longer::operator /(const longer& n)const { if (n == longer((ll)0)) { std::cerr << "除数为0!\n";exit(1); } longer th(*this), re((ll)0); re.sign = th.sign = 0; const int times = digit() - n.digit(); for (int i = times;i >= 0;--i) { longer digi(i / DIGIT + 1, 0); *digi.number.rbegin() = ((ll)pow(10, i % DIGIT)); longer t(n*digi); t.sign = 0; while (th.get_comparison(t) == 1) th-=t, re += digi; if (th.get_comparison(t) == 0) { re += digi;break; } } if (sign != n.sign) re.sign = 1; return re.get_zero(); }
longer longer::operator %(const longer& n)const { longer re(*this - (*this / n)*n); return re.get_zero(); }
longer& longer::operator ++() { return *this = *this + 1; } longer longer::operator ++(int) { longer t = *this;*this = *this + 1;return t; } longer& longer::operator --() { return *this = *this - 1; } longer longer::operator --(int) { longer t = *this;*this = *this - 1;return t; }
longer& longer::operator =(const ll& n) { if (n < 0) sign = 1, number.push_back(-n); else sign = 0, number.push_back(n); return *this; }
longer& longer::operator =(const longer& n) { number = n.number; sign = n.sign; return *this; }
longer& longer::operator =(const char* s) { return *this = std::string(s); }
longer& longer::operator =(const std::string& s) { auto ps = s.rbegin(); char c[DIGIT + 1], *pc; ll temp; sign = 0; while (ps != s.rend()) { *(pc = &c[DIGIT]) = '\0'; int i; for (i = 0;i < DIGIT && ps != s.rend();++i) *--pc = *ps++; if (*(ps - 1) == '-') *pc = '0', sign = 1; while (i++ < DIGIT) *--pc = '0'; std::istringstream in(c); in >> temp; number.push_back(temp); } if (get_ptrdiff()>1 && !*number.rbegin()) number.pop_back(); return *this; }
longer& longer::operator +=(const longer& n) { return *this = *this + n; } longer& longer::operator -=(const longer& n) { return *this = *this - n; } longer& longer::operator *=(const longer& n) { return *this = *this * n; } longer& longer::operator /=(const longer& n) { return *this = *this / n; }
inline bool longer::operator <(const longer& n)const { if ((!sign) && (n.sign)) return 0; if ((sign) && (!n.sign)) return 1; if ((!sign) && (!n.sign)) return get_comparison(n) == -1; return get_comparison(n) == 1; }
inline bool longer::operator >(const longer& n)const { if ((!sign) && (n.sign)) return 1; if ((sign) && (!n.sign)) return 0; if ((!sign) && (!n.sign)) return get_comparison(n) == 1; return get_comparison(n) == -1; }
inline bool longer::operator <=(const longer& n)const { if ((!sign) && (n.sign)) return 0; if ((sign) && (!n.sign)) return 1; if ((!sign) && (!n.sign)) return get_comparison(n) <= 0; return get_comparison(n) >= 0; }
inline bool longer::operator >=(const longer& n)const { if ((!sign) && (n.sign)) return 1; if ((sign) && (!n.sign)) return 0; if ((!sign) && (!n.sign)) return get_comparison(n) >= 0; return get_comparison(n) <= 0; }
inline bool longer::operator ==(const longer& n)const { if (((!sign) && (n.sign)) || ((sign) && (!n.sign))) return 0; return !get_comparison(n); }
inline bool longer::operator !=(const longer& n)const { if (((!sign) && (n.sign)) || ((sign) && (!n.sign))) return 1; return get_comparison(n) != 0; }
std::istream& operator >>(std::istream& is, longer& n) { std::string s;is >> s; n = s; return is; }
std::ostream& operator <<(std::ostream& os, const longer& n) { auto p = n.number.rbegin(); if (n.sign) os << '-'; os << *p; for (++p;p != n.number.rend();++p) { int i = 0;auto n = *p; while (n /= 10) ++i; for (;++i < longer::DIGIT;std::cout << '0'); os << *p; } return os; }
int longer::digit()const { int a = (get_ptrdiff() - 1) * DIGIT; ll b = *number.rbegin(); do ++a;while (b /= 10); return a; }
longer longer::lrabs(const longer& n) { longer t(n);if (t.sign) t.sign = 0;return t; }
longer& longer::get_addition(longer& aa, const longer& bb, bool si) { auto a = aa.number.begin(), &aend = aa.number.end(); auto b = bb.number.begin(), &bend = bb.number.end(); bool flag = 0; aa.sign = si; do { *a += (b == bend ? 0 : *b++) + flag; if (*a >= MAX) *a -= MAX, flag = 1; else if (flag) flag = 0; } while (++a != aend && (b != bend || flag)); if (flag) aa.number.push_back(flag); return aa.get_zero(); }
longer& longer::get_subtraction(longer& aa, const longer& bb, bool si) { auto a = aa.number.begin(), &aend = aa.number.end(); auto b = bb.number.begin(), &bend = bb.number.end(); bool flag = 0; aa.sign = si; do { *a -= (b == bend ? 0 : *b++) + flag; if (*a < 0) *a += MAX, flag = 1; else if (flag) flag = 0; } while (++a != aend && (b != bend || flag)); while (aa.get_ptrdiff()>1 && !*aa.number.rbegin()) aa.number.pop_back(); return aa.get_zero(); }
int longer::get_comparison(const longer& n)const { if (get_ptrdiff() < n.get_ptrdiff()) return -1; if (get_ptrdiff() > n.get_ptrdiff()) return 1; auto a = number.rbegin(), b = n.number.rbegin(); while (a != number.rend()) { if (*a < *b) return -1; if (*a > *b) return 1; ++a, ++b; } return 0; }
inline ptrdiff_t longer::get_ptrdiff()const { return number.end() - number.begin(); }
inline longer& longer::get_zero() { if (get_ptrdiff() == 1 && number[0] == 0 && sign == 1) sign = 0; return *this; }
#endif
|