Advertisement
sve_vash

Untitled

Jun 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.42 KB | None | 0 0
  1. /*
  2. * InfInt - Arbitrary-Precision Integer Arithmetic Library
  3. * Copyright (C) 2013 Sercan Tutar
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. *
  10. * USAGE:
  11. * It is pretty straight forward to use the library. Just create an instance of
  12. * InfInt class and start using it.
  13. *
  14. * Useful methods:
  15. * intSqrt: integer square root operation
  16. * digitAt: returns digit at index
  17. * numberOfDigits: returns number of digits
  18. * size: returns size in bytes
  19. * toString: converts it to a string
  20. *
  21. * There are also conversion methods which allow conversion to primitive types:
  22. * toInt, toLong, toLongLong, toUnsignedInt, toUnsignedLong, toUnsignedLongLong.
  23. *
  24. * You may define INFINT_USE_EXCEPTIONS and library methods will start raising
  25. * InfIntException in case of error instead of writing error messages using
  26. * std::cerr.
  27. *
  28. * See ReadMe.txt for more info.
  29. *
  30. *
  31. * No overflows, happy programmers!
  32. *
  33. */
  34.  
  35. #ifndef INFINT_H_
  36. #define INFINT_H_
  37.  
  38. #include <iostream>
  39. #include <vector>
  40. #include <sstream>
  41. #include <iomanip>
  42. #include <climits>
  43.  
  44. //#include <limits.h>
  45. //#include <stdlib.h>
  46.  
  47. #ifdef _WIN32
  48. #define LONG_LONG_MIN LLONG_MIN
  49. #define LONG_LONG_MAX LLONG_MAX
  50. #define ULONG_LONG_MAX ULLONG_MAX
  51. #endif
  52.  
  53. #ifdef INFINT_USE_EXCEPTIONS
  54. #include <exception>
  55. #endif
  56.  
  57. typedef int ELEM_TYPE;
  58. typedef long long PRODUCT_TYPE;
  59. static const ELEM_TYPE BASE = 1000000000;
  60. static const ELEM_TYPE UPPER_BOUND = 999999999;
  61. static const ELEM_TYPE DIGIT_COUNT = 9;
  62. static const int powersOfTen[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
  63.  
  64. #ifdef INFINT_USE_EXCEPTIONS
  65. class InfIntException: public std::exception
  66. {
  67. public:
  68. InfIntException(const std::string& txt) throw ();
  69. ~InfIntException() throw ();
  70. const char* what() const throw ();
  71. private:
  72. std::string txt;
  73. };
  74.  
  75. inline InfIntException::InfIntException(const std::string& txt) throw () :
  76. std::exception(), txt(txt)
  77. {
  78. }
  79.  
  80. inline InfIntException::~InfIntException() throw ()
  81. {
  82. }
  83.  
  84. inline const char* InfIntException::what() const throw ()
  85. {
  86. return txt.c_str();
  87. }
  88. #endif
  89.  
  90. inline static div_t my_div(int num, int denom)
  91. {
  92. div_t result;
  93. result.quot = num / denom;
  94. result.rem = num - denom * result.quot;
  95. return result;
  96. }
  97.  
  98. inline static ldiv_t my_ldiv(long num, long denom)
  99. {
  100. ldiv_t result;
  101. result.quot = num / denom;
  102. result.rem = num - denom * result.quot;
  103. return result;
  104. }
  105.  
  106. inline static lldiv_t my_lldiv(long long num, long long denom)
  107. {
  108. lldiv_t result;
  109. result.quot = num / denom;
  110. result.rem = num - denom * result.quot;
  111. return result;
  112. }
  113.  
  114. class InfInt
  115. {
  116. friend std::ostream& operator<<(std::ostream &s, const InfInt &n);
  117. friend std::istream& operator>>(std::istream &s, InfInt &val);
  118.  
  119. public:
  120. /* constructors */
  121. InfInt();
  122. InfInt(const char* c);
  123. InfInt(const std::string& s);
  124. InfInt(int l);
  125. InfInt(long l);
  126. InfInt(long long l);
  127. InfInt(unsigned int l);
  128. InfInt(unsigned long l);
  129. InfInt(unsigned long long l);
  130. InfInt(const InfInt& l);
  131.  
  132. /* assignment operators */
  133. const InfInt& operator=(const char* c);
  134. const InfInt& operator=(const std::string& s);
  135. const InfInt& operator=(int l);
  136. const InfInt& operator=(long l);
  137. const InfInt& operator=(long long l);
  138. const InfInt& operator=(unsigned int l);
  139. const InfInt& operator=(unsigned long l);
  140. const InfInt& operator=(unsigned long long l);
  141. const InfInt& operator=(const InfInt& l);
  142.  
  143. /* unary increment/decrement operators */
  144. const InfInt& operator++();
  145. const InfInt& operator--();
  146. InfInt operator++(int);
  147. InfInt operator--(int);
  148.  
  149. /* operational assignments */
  150. const InfInt& operator+=(const InfInt& rhs);
  151. const InfInt& operator-=(const InfInt& rhs);
  152. const InfInt& operator*=(const InfInt& rhs);
  153. const InfInt& operator/=(const InfInt& rhs); // throw
  154. const InfInt& operator%=(const InfInt& rhs); // throw
  155. const InfInt& operator*=(ELEM_TYPE rhs);
  156.  
  157. /* operations */
  158. InfInt operator-() const;
  159. InfInt operator+(const InfInt& rhs) const;
  160. InfInt operator-(const InfInt& rhs) const;
  161. InfInt operator*(const InfInt& rhs) const;
  162. InfInt operator/(const InfInt& rhs) const; // throw
  163. InfInt operator%(const InfInt& rhs) const; // throw
  164. InfInt operator*(ELEM_TYPE rhs) const;
  165.  
  166. /* relational operations */
  167. bool operator==(const InfInt& rhs) const;
  168. bool operator!=(const InfInt& rhs) const;
  169. bool operator<(const InfInt& rhs) const;
  170. bool operator<=(const InfInt& rhs) const;
  171. bool operator>(const InfInt& rhs) const;
  172. bool operator>=(const InfInt& rhs) const;
  173.  
  174. /* integer square root */
  175. InfInt intSqrt() const; // throw
  176.  
  177. /* digit operations */
  178. char digitAt(size_t i) const; // throw
  179. size_t numberOfDigits() const;
  180.  
  181. /* size in bytes */
  182. size_t size() const;
  183.  
  184. /* string conversion */
  185. std::string toString() const;
  186.  
  187. /* conversion to primitive types */
  188. int toInt() const; // throw
  189. long toLong() const; // throw
  190. long long toLongLong() const; // throw
  191. unsigned int toUnsignedInt() const; // throw
  192. unsigned long toUnsignedLong() const; // throw
  193. unsigned long long toUnsignedLongLong() const; // throw
  194.  
  195. private:
  196. static ELEM_TYPE dInR(const InfInt& R, const InfInt& D);
  197. static void multiplyByDigit(ELEM_TYPE factor, std::vector<ELEM_TYPE>& val);
  198.  
  199. void correct(bool justCheckLeadingZeros = false, bool hasValidSign = false);
  200. void fromString(const std::string& s);
  201. void optimizeSqrtSearchBounds(InfInt& lo, InfInt& hi) const;
  202. void truncateToBase();
  203. bool equalizeSigns();
  204. void removeLeadingZeros();
  205.  
  206. std::vector<ELEM_TYPE> val; // number with base FACTOR
  207. bool pos; // true if number is positive
  208. };
  209.  
  210. inline InfInt::InfInt() : pos(true)
  211. {
  212. //PROFINY_SCOPE
  213. val.push_back((ELEM_TYPE) 0);
  214. }
  215.  
  216. inline InfInt::InfInt(const char* c)
  217. {
  218. //PROFINY_SCOPE
  219. fromString(c);
  220. }
  221.  
  222. inline InfInt::InfInt(const std::string& s)
  223. {
  224. //PROFINY_SCOPE
  225. fromString(s);
  226. }
  227.  
  228. inline InfInt::InfInt(int l) : pos(l >= 0)
  229. {
  230. //PROFINY_SCOPE
  231. bool subtractOne = false;
  232. if (l == INT_MIN)
  233. {
  234. subtractOne = true;
  235. ++l;
  236. }
  237.  
  238. if (!pos)
  239. {
  240. l = -l;
  241. }
  242. do
  243. {
  244. div_t dt = my_div(l, BASE);
  245. val.push_back((ELEM_TYPE) dt.rem);
  246. l = dt.quot;
  247. } while (l > 0);
  248.  
  249. if (subtractOne)
  250. {
  251. --*this;
  252. }
  253. }
  254.  
  255. inline InfInt::InfInt(long l) : pos(l >= 0)
  256. {
  257. //PROFINY_SCOPE
  258. bool subtractOne = false;
  259. if (l == LONG_MIN)
  260. {
  261. subtractOne = true;
  262. ++l;
  263. }
  264.  
  265. if (!pos)
  266. {
  267. l = -l;
  268. }
  269. do
  270. {
  271. ldiv_t dt = my_ldiv(l, BASE);
  272. val.push_back((ELEM_TYPE) dt.rem);
  273. l = dt.quot;
  274. } while (l > 0);
  275.  
  276. if (subtractOne)
  277. {
  278. --*this;
  279. }
  280. }
  281.  
  282. inline InfInt::InfInt(long long l) : pos(l >= 0)
  283. {
  284. //PROFINY_SCOPE
  285. bool subtractOne = false;
  286. if (l == LONG_LONG_MIN)
  287. {
  288. subtractOne = true;
  289. ++l;
  290. }
  291.  
  292. if (!pos)
  293. {
  294. l = -l;
  295. }
  296. do
  297. {
  298. lldiv_t dt = my_lldiv(l, BASE);
  299. val.push_back((ELEM_TYPE) dt.rem);
  300. l = dt.quot;
  301. } while (l > 0);
  302.  
  303. if (subtractOne)
  304. {
  305. --*this;
  306. }
  307. }
  308.  
  309. inline InfInt::InfInt(unsigned int l) : pos(true)
  310. {
  311. //PROFINY_SCOPE
  312. do
  313. {
  314. val.push_back((ELEM_TYPE) (l % BASE));
  315. l = l / BASE;
  316. } while (l > 0);
  317. }
  318.  
  319. inline InfInt::InfInt(unsigned long l) : pos(true)
  320. {
  321. //PROFINY_SCOPE
  322. do
  323. {
  324. val.push_back((ELEM_TYPE) (l % BASE));
  325. l = l / BASE;
  326. } while (l > 0);
  327. }
  328.  
  329. inline InfInt::InfInt(unsigned long long l) : pos(true)
  330. {
  331. //PROFINY_SCOPE
  332. do
  333. {
  334. val.push_back((ELEM_TYPE) (l % BASE));
  335. l = l / BASE;
  336. } while (l > 0);
  337. }
  338.  
  339. inline InfInt::InfInt(const InfInt& l) : pos(l.pos), val(l.val)
  340. {
  341. //PROFINY_SCOPE
  342. }
  343.  
  344. inline const InfInt& InfInt::operator=(const char* c)
  345. {
  346. //PROFINY_SCOPE
  347. fromString(c);
  348. return *this;
  349. }
  350.  
  351. inline const InfInt& InfInt::operator=(const std::string& s)
  352. {
  353. //PROFINY_SCOPE
  354. fromString(s);
  355. return *this;
  356. }
  357.  
  358. inline const InfInt& InfInt::operator=(int l)
  359. {
  360. //PROFINY_SCOPE
  361. bool subtractOne = false;
  362. if (l == INT_MIN)
  363. {
  364. subtractOne = true;
  365. ++l;
  366. }
  367.  
  368. pos = l >= 0;
  369. val.clear();
  370. if (!pos)
  371. {
  372. l = -l;
  373. }
  374. do
  375. {
  376. div_t dt = my_div(l, BASE);
  377. val.push_back((ELEM_TYPE) dt.rem);
  378. l = dt.quot;
  379. } while (l > 0);
  380.  
  381. return subtractOne ? --*this : *this;
  382. }
  383.  
  384. inline const InfInt& InfInt::operator=(long l)
  385. {
  386. //PROFINY_SCOPE
  387. bool subtractOne = false;
  388. if (l == LONG_MIN)
  389. {
  390. subtractOne = true;
  391. ++l;
  392. }
  393.  
  394. pos = l >= 0;
  395. val.clear();
  396. if (!pos)
  397. {
  398. l = -l;
  399. }
  400. do
  401. {
  402. ldiv_t dt = my_ldiv(l, BASE);
  403. val.push_back((ELEM_TYPE) dt.rem);
  404. l = dt.quot;
  405. } while (l > 0);
  406.  
  407. return subtractOne ? --*this : *this;
  408. }
  409.  
  410. inline const InfInt& InfInt::operator=(long long l)
  411. {
  412. //PROFINY_SCOPE
  413. bool subtractOne = false;
  414. if (l == LONG_LONG_MIN)
  415. {
  416. subtractOne = true;
  417. ++l;
  418. }
  419.  
  420. pos = l >= 0;
  421. val.clear();
  422. if (!pos)
  423. {
  424. l = -l;
  425. }
  426. do
  427. {
  428. lldiv_t dt = my_lldiv(l, BASE);
  429. val.push_back((ELEM_TYPE) dt.rem);
  430. l = dt.quot;
  431. } while (l > 0);
  432.  
  433. return subtractOne ? --*this : *this;
  434. }
  435.  
  436. inline const InfInt& InfInt::operator=(unsigned int l)
  437. {
  438. //PROFINY_SCOPE
  439. pos = true;
  440. val.clear();
  441. do
  442. {
  443. val.push_back((ELEM_TYPE) (l % BASE));
  444. l = l / BASE;
  445. } while (l > 0);
  446. return *this;
  447. }
  448.  
  449. inline const InfInt& InfInt::operator=(unsigned long l)
  450. {
  451. //PROFINY_SCOPE
  452. pos = true;
  453. val.clear();
  454. do
  455. {
  456. val.push_back((ELEM_TYPE) (l % BASE));
  457. l = l / BASE;
  458. } while (l > 0);
  459. return *this;
  460. }
  461.  
  462. inline const InfInt& InfInt::operator=(unsigned long long l)
  463. {
  464. //PROFINY_SCOPE
  465. pos = true;
  466. val.clear();
  467. do
  468. {
  469. val.push_back((ELEM_TYPE) (l % BASE));
  470. l = l / BASE;
  471. } while (l > 0);
  472. return *this;
  473. }
  474.  
  475. inline const InfInt& InfInt::operator=(const InfInt& l)
  476. {
  477. //PROFINY_SCOPE
  478. pos = l.pos;
  479. val = l.val;
  480. return *this;
  481. }
  482.  
  483. inline const InfInt& InfInt::operator++()
  484. {
  485. //PROFINY_SCOPE
  486. val[0] += (pos ? 1 : -1);
  487. this->correct(false, true);
  488. return *this;
  489. }
  490.  
  491. inline const InfInt& InfInt::operator--()
  492. {
  493. //PROFINY_SCOPE
  494. val[0] -= (pos ? 1 : -1);
  495. this->correct(false, true);
  496. return *this;
  497. }
  498.  
  499. inline InfInt InfInt::operator++(int)
  500. {
  501. //PROFINY_SCOPE
  502. InfInt result = *this;
  503. val[0] += (pos ? 1 : -1);
  504. this->correct(false, true);
  505. return result;
  506. }
  507.  
  508. inline InfInt InfInt::operator--(int)
  509. {
  510. //PROFINY_SCOPE
  511. InfInt result = *this;
  512. val[0] -= (pos ? 1 : -1);
  513. this->correct(false, true);
  514. return result;
  515. }
  516.  
  517. inline const InfInt& InfInt::operator+=(const InfInt& rhs)
  518. {
  519. //PROFINY_SCOPE
  520. if (rhs.val.size() > val.size())
  521. {
  522. val.resize(rhs.val.size(), 0);
  523. }
  524. for (size_t i = 0; i < val.size(); ++i)
  525. {
  526. val[i] = (pos ? val[i] : -val[i]) + (i < rhs.val.size() ? (rhs.pos ? rhs.val[i] : -rhs.val[i]) : 0);
  527. }
  528. correct();
  529. return *this;
  530. }
  531.  
  532. inline const InfInt& InfInt::operator-=(const InfInt& rhs)
  533. {
  534. //PROFINY_SCOPE
  535. if (rhs.val.size() > val.size())
  536. {
  537. val.resize(rhs.val.size(), 0);
  538. }
  539. for (size_t i = 0; i < val.size(); ++i)
  540. {
  541. val[i] = (pos ? val[i] : -val[i]) - (i < rhs.val.size() ? (rhs.pos ? rhs.val[i] : -rhs.val[i]) : 0);
  542. }
  543. correct();
  544. return *this;
  545. }
  546.  
  547. inline const InfInt& InfInt::operator*=(const InfInt& rhs)
  548. {
  549. //PROFINY_SCOPE
  550. // TODO: optimize (do not use operator*)
  551. *this = *this * rhs;
  552. return *this;
  553. }
  554.  
  555. inline const InfInt& InfInt::operator/=(const InfInt& rhs)
  556. {
  557. //PROFINY_SCOPE
  558. if (rhs == 0)
  559. {
  560. #ifdef INFINT_USE_EXCEPTIONS
  561. throw InfIntException("division by zero");
  562. #else
  563. std::cerr << "Division by zero!" << std::endl;
  564. return *this;
  565. #endif
  566. }
  567. InfInt R, D = (rhs.pos ? rhs : -rhs), N = (pos ? *this : -*this);
  568. bool oldpos = pos;
  569. std::fill(val.begin(), val.end(), 0);
  570. for (int i = (int) N.val.size() - 1; i >= 0; --i)
  571. {
  572. R.val.insert(R.val.begin(), N.val[i]);
  573. R.correct(true);
  574. ELEM_TYPE cnt = dInR(R, D);
  575. R -= D * cnt;
  576. val[i] += cnt;
  577. }
  578. correct();
  579. pos = (val.size() == 1 && val[0] == 0) ? true : (oldpos == rhs.pos);
  580. return *this;
  581. }
  582.  
  583. inline const InfInt& InfInt::operator%=(const InfInt& rhs)
  584. {
  585. //PROFINY_SCOPE
  586. // TODO: optimize (do not use operator%)
  587. *this = *this % rhs;
  588. return *this;
  589. // if (rhs == 0)
  590. // {
  591. //#ifdef INFINT_USE_EXCEPTIONS
  592. // throw InfIntException("division by zero");
  593. //#else
  594. // std::cerr << "Division by zero!" << std::endl;
  595. // return *this;
  596. //#endif
  597. // }
  598. // InfInt D = (rhs.pos ? rhs : -rhs), N = (pos ? *this : -*this);
  599. // bool oldpos = pos;
  600. // val.clear();
  601. // for (int i = (int) N.val.size() - 1; i >= 0; --i)
  602. // {
  603. // val.insert(val.begin(), N.val[i]);
  604. // correct(true);
  605. // *this -= D * dInR(*this, D);
  606. // }
  607. // correct();
  608. // pos = (val.size() == 1 && val[0] == 0) ? true : oldpos;
  609. // return *this;
  610. }
  611.  
  612. inline const InfInt& InfInt::operator*=(ELEM_TYPE rhs)
  613. {
  614. //PROFINY_SCOPE
  615. ELEM_TYPE factor = rhs < 0 ? -rhs : rhs;
  616. bool oldpos = pos;
  617. multiplyByDigit(factor, val);
  618. correct();
  619. pos = (val.size() == 1 && val[0] == 0) ? true : (oldpos == (rhs >= 0));
  620. return *this;
  621. }
  622.  
  623. inline InfInt InfInt::operator-() const
  624. {
  625. //PROFINY_SCOPE
  626. InfInt result = *this;
  627. result.pos = !pos;
  628. return result;
  629. }
  630.  
  631. inline InfInt InfInt::operator+(const InfInt& rhs) const
  632. {
  633. //PROFINY_SCOPE
  634. InfInt result;
  635. result.val.resize(val.size() > rhs.val.size() ? val.size() : rhs.val.size(), 0);
  636. for (size_t i = 0; i < val.size() || i < rhs.val.size(); ++i)
  637. {
  638. result.val[i] = (i < val.size() ? (pos ? val[i] : -val[i]) : 0) + (i < rhs.val.size() ? (rhs.pos ? rhs.val[i] : -rhs.val[i]) : 0);
  639. }
  640. result.correct();
  641. return result;
  642. }
  643.  
  644. inline InfInt InfInt::operator-(const InfInt& rhs) const
  645. {
  646. //PROFINY_SCOPE
  647. InfInt result;
  648. result.val.resize(val.size() > rhs.val.size() ? val.size() : rhs.val.size(), 0);
  649. for (size_t i = 0; i < val.size() || i < rhs.val.size(); ++i)
  650. {
  651. result.val[i] = (i < val.size() ? (pos ? val[i] : -val[i]) : 0) - (i < rhs.val.size() ? (rhs.pos ? rhs.val[i] : -rhs.val[i]) : 0);
  652. }
  653. result.correct();
  654. return result;
  655. }
  656.  
  657. inline InfInt InfInt::operator*(const InfInt& rhs) const
  658. {
  659. //PROFINY_SCOPE
  660. InfInt result;
  661. result.val.resize(val.size() + rhs.val.size(), 0);
  662. PRODUCT_TYPE carry = 0;
  663. size_t digit = 0;
  664. for (;; ++digit)
  665. {
  666. lldiv_t dt = my_lldiv(carry, BASE);
  667. carry = dt.quot;
  668. result.val[digit] = (ELEM_TYPE) dt.rem;
  669.  
  670. bool found = false;
  671. for (size_t i = digit < rhs.val.size() ? 0 : digit - rhs.val.size() + 1; i < val.size() && i <= digit; ++i)
  672. {
  673. PRODUCT_TYPE pval = result.val[digit] + val[i] * (PRODUCT_TYPE) rhs.val[digit - i];
  674. if (pval >= BASE || pval <= -BASE)
  675. {
  676. lldiv_t dt = my_lldiv(pval, BASE);
  677. carry += dt.quot;
  678. pval = dt.rem;
  679. }
  680. result.val[digit] = (ELEM_TYPE) pval;
  681. found = true;
  682. }
  683. if (!found)
  684. {
  685. break;
  686. }
  687. }
  688. for (; carry > 0; ++digit)
  689. {
  690. lldiv_t dt = my_lldiv(carry, BASE);
  691. result.val[digit] = (ELEM_TYPE) dt.rem;
  692. carry = dt.quot;
  693. }
  694. result.correct();
  695. result.pos = (result.val.size() == 1 && result.val[0] == 0) ? true : (pos == rhs.pos);
  696. return result;
  697. }
  698.  
  699. inline InfInt InfInt::operator/(const InfInt& rhs) const
  700. {
  701. //PROFINY_SCOPE
  702. if (rhs == 0)
  703. {
  704. #ifdef INFINT_USE_EXCEPTIONS
  705. throw InfIntException("division by zero");
  706. #else
  707. std::cerr << "Division by zero!" << std::endl;
  708. return 0;
  709. #endif
  710. }
  711. InfInt Q, R, D = (rhs.pos ? rhs : -rhs), N = (pos ? *this : -*this);
  712. Q.val.resize(N.val.size(), 0);
  713. for (int i = (int) N.val.size() - 1; i >= 0; --i)
  714. {
  715. R.val.insert(R.val.begin(), N.val[i]);
  716. R.correct(true);
  717. ELEM_TYPE cnt = dInR(R, D);
  718. R -= D * cnt;
  719. Q.val[i] += cnt;
  720. }
  721. Q.correct();
  722. Q.pos = (Q.val.size() == 1 && Q.val[0] == 0) ? true : (pos == rhs.pos);
  723. return Q;
  724. }
  725.  
  726. inline InfInt InfInt::operator%(const InfInt& rhs) const
  727. {
  728. //PROFINY_SCOPE
  729. if (rhs == 0)
  730. {
  731. #ifdef INFINT_USE_EXCEPTIONS
  732. throw InfIntException("division by zero");
  733. #else
  734. std::cerr << "Division by zero!" << std::endl;
  735. return 0;
  736. #endif
  737. }
  738. InfInt R, D = (rhs.pos ? rhs : -rhs), N = (pos ? *this : -*this);
  739. for (int i = (int) N.val.size() - 1; i >= 0; --i)
  740. {
  741. R.val.insert(R.val.begin(), N.val[i]);
  742. R.correct(true);
  743. R -= D * dInR(R, D);
  744. }
  745. R.correct();
  746. R.pos = (R.val.size() == 1 && R.val[0] == 0) ? true : pos;
  747. return R;
  748. }
  749.  
  750. inline InfInt InfInt::operator*(ELEM_TYPE rhs) const
  751. {
  752. //PROFINY_SCOPE
  753. InfInt result = *this;
  754. ELEM_TYPE factor = rhs < 0 ? -rhs : rhs;
  755. multiplyByDigit(factor, result.val);
  756. result.correct();
  757. result.pos = (result.val.size() == 1 && result.val[0] == 0) ? true : (pos == (rhs >= 0));
  758. return result;
  759. }
  760.  
  761. inline bool InfInt::operator==(const InfInt& rhs) const
  762. {
  763. //PROFINY_SCOPE
  764. if (pos != rhs.pos || val.size() != rhs.val.size())
  765. {
  766. return false;
  767. }
  768. for (int i = (int) val.size() - 1; i >= 0; --i)
  769. {
  770. if (val[i] != rhs.val[i])
  771. {
  772. return false;
  773. }
  774. }
  775. return true;
  776. }
  777.  
  778. inline bool InfInt::operator!=(const InfInt& rhs) const
  779. {
  780. //PROFINY_SCOPE
  781. if (pos != rhs.pos || val.size() != rhs.val.size())
  782. {
  783. return true;
  784. }
  785. for (int i = (int) val.size() - 1; i >= 0; --i)
  786. {
  787. if (val[i] != rhs.val[i])
  788. {
  789. return true;
  790. }
  791. }
  792. return false;
  793. }
  794.  
  795. inline bool InfInt::operator<(const InfInt& rhs) const
  796. {
  797. //PROFINY_SCOPE
  798. if (pos && !rhs.pos)
  799. {
  800. return false;
  801. }
  802. if (!pos && rhs.pos)
  803. {
  804. return true;
  805. }
  806. if (val.size() > rhs.val.size())
  807. {
  808. return pos ? false : true;
  809. }
  810. if (val.size() < rhs.val.size())
  811. {
  812. return pos ? true : false;
  813. }
  814. for (int i = (int) val.size() - 1; i >= 0; --i)
  815. {
  816. if (val[i] < rhs.val[i])
  817. {
  818. return pos ? true : false;
  819. }
  820. if (val[i] > rhs.val[i])
  821. {
  822. return pos ? false : true;
  823. }
  824. }
  825. return false;
  826. }
  827.  
  828. inline bool InfInt::operator<=(const InfInt& rhs) const
  829. {
  830. //PROFINY_SCOPE
  831. if (pos && !rhs.pos)
  832. {
  833. return false;
  834. }
  835. if (!pos && rhs.pos)
  836. {
  837. return true;
  838. }
  839. if (val.size() > rhs.val.size())
  840. {
  841. return pos ? false : true;
  842. }
  843. if (val.size() < rhs.val.size())
  844. {
  845. return pos ? true : false;
  846. }
  847. for (int i = (int) val.size() - 1; i >= 0; --i)
  848. {
  849. if (val[i] < rhs.val[i])
  850. {
  851. return pos ? true : false;
  852. }
  853. if (val[i] > rhs.val[i])
  854. {
  855. return pos ? false : true;
  856. }
  857. }
  858. return true;
  859. }
  860.  
  861. inline bool InfInt::operator>(const InfInt& rhs) const
  862. {
  863. //PROFINY_SCOPE
  864. if (pos && !rhs.pos)
  865. {
  866. return true;
  867. }
  868. if (!pos && rhs.pos)
  869. {
  870. return false;
  871. }
  872. if (val.size() > rhs.val.size())
  873. {
  874. return pos ? true : false;
  875. }
  876. if (val.size() < rhs.val.size())
  877. {
  878. return pos ? false : true;
  879. }
  880. for (int i = (int) val.size() - 1; i >= 0; --i)
  881. {
  882. if (val[i] < rhs.val[i])
  883. {
  884. return pos ? false : true;
  885. }
  886. if (val[i] > rhs.val[i])
  887. {
  888. return pos ? true : false;
  889. }
  890. }
  891. return false;
  892. }
  893.  
  894. inline bool InfInt::operator>=(const InfInt& rhs) const
  895. {
  896. //PROFINY_SCOPE
  897. if (pos && !rhs.pos)
  898. {
  899. return true;
  900. }
  901. if (!pos && rhs.pos)
  902. {
  903. return false;
  904. }
  905. if (val.size() > rhs.val.size())
  906. {
  907. return pos ? true : false;
  908. }
  909. if (val.size() < rhs.val.size())
  910. {
  911. return pos ? false : true;
  912. }
  913. for (int i = (int) val.size() - 1; i >= 0; --i)
  914. {
  915. if (val[i] < rhs.val[i])
  916. {
  917. return pos ? false : true;
  918. }
  919. if (val[i] > rhs.val[i])
  920. {
  921. return pos ? true : false;
  922. }
  923. }
  924. return true;
  925. }
  926.  
  927. inline void InfInt::optimizeSqrtSearchBounds(InfInt& lo, InfInt& hi) const
  928. {
  929. //PROFINY_SCOPE
  930. InfInt hdn = 1;
  931. for (int i = (int) this->numberOfDigits() / 2; i >= 2; --i)
  932. {
  933. hdn *= 10;
  934. }
  935. if (lo < hdn)
  936. {
  937. lo = hdn;
  938. }
  939. hdn *= 100;
  940. if (hi > hdn)
  941. {
  942. hi = hdn;
  943. }
  944. }
  945.  
  946. inline InfInt InfInt::intSqrt() const
  947. {
  948. //PROFINY_SCOPE
  949. if (*this <= 0)
  950. {
  951. #ifdef INFINT_USE_EXCEPTIONS
  952. throw InfIntException("intSqrt called for non-positive integer");
  953. #else
  954. std::cerr << "intSqrt called for non-positive integer: " << *this << std::endl;
  955. return 0;
  956. #endif
  957. }
  958. InfInt hi = *this / 2 + 1, lo = 0, mid, mid2;
  959. optimizeSqrtSearchBounds(lo, hi);
  960. do
  961. {
  962. mid = (hi + lo) / 2; // 8 factor
  963. mid2 = mid * mid; // 1 factor
  964. if (mid2 == *this)
  965. {
  966. lo = mid;
  967. break;
  968. }
  969. else if (mid2 < *this)
  970. {
  971. lo = mid;
  972. }
  973. else
  974. {
  975. hi = mid;
  976. }
  977. } while (lo < hi - 1 && mid2 != *this);
  978. return lo;
  979. }
  980.  
  981. inline char InfInt::digitAt(size_t i) const
  982. {
  983. //PROFINY_SCOPE
  984. if (numberOfDigits() <= i)
  985. {
  986. #ifdef INFINT_USE_EXCEPTIONS
  987. throw InfIntException("invalid digit index");
  988. #else
  989. std::cerr << "Invalid digit index: " << i << std::endl;
  990. return -1;
  991. #endif
  992. }
  993. return (val[i / DIGIT_COUNT] / powersOfTen[i % DIGIT_COUNT]) % 10;
  994. }
  995.  
  996. inline size_t InfInt::numberOfDigits() const
  997. {
  998. //PROFINY_SCOPE
  999. return (val.size() - 1) * DIGIT_COUNT +
  1000. (val.back() > 99999999 ? 9 : (val.back() > 9999999 ? 8 : (val.back() > 999999 ? 7 : (val.back() > 99999 ? 6 :
  1001. (val.back() > 9999 ? 5 : (val.back() > 999 ? 4 : (val.back() > 99 ? 3 : (val.back() > 9 ? 2 : 1))))))));
  1002. }
  1003.  
  1004. inline std::string InfInt::toString() const
  1005. {
  1006. //PROFINY_SCOPE
  1007. std::ostringstream oss;
  1008. oss << *this;
  1009. return oss.str();
  1010. }
  1011.  
  1012. inline size_t InfInt::size() const
  1013. {
  1014. //PROFINY_SCOPE
  1015. return val.size() * sizeof(ELEM_TYPE) + sizeof(bool);
  1016. }
  1017.  
  1018. inline int InfInt::toInt() const
  1019. {
  1020. //PROFINY_SCOPE
  1021. if (*this > INT_MAX || *this < INT_MIN)
  1022. {
  1023. #ifdef INFINT_USE_EXCEPTIONS
  1024. throw InfIntException("out of bounds");
  1025. #else
  1026. std::cerr << "Out of INT bounds: " << *this << std::endl;
  1027. #endif
  1028. }
  1029. int result = 0;
  1030. for (int i = (int) val.size() - 1; i >= 0; --i)
  1031. {
  1032. result = result * BASE + val[i];
  1033. }
  1034. return pos ? result : -result;
  1035. }
  1036.  
  1037. inline long InfInt::toLong() const
  1038. {
  1039. //PROFINY_SCOPE
  1040. if (*this > LONG_MAX || *this < LONG_MIN)
  1041. {
  1042. #ifdef INFINT_USE_EXCEPTIONS
  1043. throw InfIntException("out of bounds");
  1044. #else
  1045. std::cerr << "Out of LONG bounds: " << *this << std::endl;
  1046. #endif
  1047. }
  1048. long result = 0;
  1049. for (int i = (int) val.size() - 1; i >= 0; --i)
  1050. {
  1051. result = result * BASE + val[i];
  1052. }
  1053. return pos ? result : -result;
  1054. }
  1055.  
  1056. inline long long InfInt::toLongLong() const
  1057. {
  1058. //PROFINY_SCOPE
  1059. if (*this > LONG_LONG_MAX || *this < LONG_LONG_MIN)
  1060. {
  1061. #ifdef INFINT_USE_EXCEPTIONS
  1062. throw InfIntException("out of bounds");
  1063. #else
  1064. std::cerr << "Out of LLONG bounds: " << *this << std::endl;
  1065. #endif
  1066. }
  1067. long long result = 0;
  1068. for (int i = (int) val.size() - 1; i >= 0; --i)
  1069. {
  1070. result = result * BASE + val[i];
  1071. }
  1072. return pos ? result : -result;
  1073. }
  1074.  
  1075. inline unsigned int InfInt::toUnsignedInt() const
  1076. {
  1077. //PROFINY_SCOPE
  1078. if (!pos || *this > UINT_MAX)
  1079. {
  1080. #ifdef INFINT_USE_EXCEPTIONS
  1081. throw InfIntException("out of bounds");
  1082. #else
  1083. std::cerr << "Out of UINT bounds: " << *this << std::endl;
  1084. #endif
  1085. }
  1086. unsigned int result = 0;
  1087. for (int i = (int) val.size() - 1; i >= 0; --i)
  1088. {
  1089. result = result * BASE + val[i];
  1090. }
  1091. return result;
  1092. }
  1093.  
  1094. inline unsigned long InfInt::toUnsignedLong() const
  1095. {
  1096. //PROFINY_SCOPE
  1097. if (!pos || *this > ULONG_MAX)
  1098. {
  1099. #ifdef INFINT_USE_EXCEPTIONS
  1100. throw InfIntException("out of bounds");
  1101. #else
  1102. std::cerr << "Out of ULONG bounds: " << *this << std::endl;
  1103. #endif
  1104. }
  1105. unsigned long result = 0;
  1106. for (int i = (int) val.size() - 1; i >= 0; --i)
  1107. {
  1108. result = result * BASE + val[i];
  1109. }
  1110. return result;
  1111. }
  1112.  
  1113. inline unsigned long long InfInt::toUnsignedLongLong() const
  1114. {
  1115. //PROFINY_SCOPE
  1116. if (!pos || *this > ULONG_LONG_MAX)
  1117. {
  1118. #ifdef INFINT_USE_EXCEPTIONS
  1119. throw InfIntException("out of bounds");
  1120. #else
  1121. std::cerr << "Out of ULLONG bounds: " << *this << std::endl;
  1122. #endif
  1123. }
  1124. unsigned long long result = 0;
  1125. for (int i = (int) val.size() - 1; i >= 0; --i)
  1126. {
  1127. result = result * BASE + val[i];
  1128. }
  1129. return result;
  1130. }
  1131.  
  1132. inline void InfInt::truncateToBase()
  1133. {
  1134. //PROFINY_SCOPE
  1135. for (size_t i = 0; i < val.size(); ++i) // truncate each
  1136. {
  1137. if (val[i] >= BASE || val[i] <= -BASE)
  1138. {
  1139. div_t dt = my_div(val[i], BASE);
  1140. val[i] = dt.rem;
  1141. if (i + 1 >= val.size())
  1142. {
  1143. val.push_back(dt.quot);
  1144. }
  1145. else
  1146. {
  1147. val[i + 1] += dt.quot;
  1148. }
  1149. }
  1150. }
  1151. }
  1152.  
  1153. inline bool InfInt::equalizeSigns()
  1154. {
  1155. //PROFINY_SCOPE
  1156. bool isPositive = true;
  1157. int i = (int) ((val.size())) - 1;
  1158. for (; i >= 0; --i)
  1159. {
  1160. if (val[i] != 0)
  1161. {
  1162. isPositive = val[i--] > 0;
  1163. break;
  1164. }
  1165. }
  1166.  
  1167. if (isPositive)
  1168. {
  1169. for (; i >= 0; --i)
  1170. {
  1171. if (val[i] < 0)
  1172. {
  1173. int k = 0, index = i + 1;
  1174. for (; (size_t)(index) < val.size() && val[index] == 0; ++k, ++index)
  1175. ; // count adjacent zeros on left
  1176. //if ((size_t)(index) < val.size() && val[index] > 0)
  1177. { // number on the left is positive
  1178. val[index] -= 1;
  1179. val[i] += BASE;
  1180. for (; k > 0; --k)
  1181. {
  1182. val[i + k] = UPPER_BOUND;
  1183. }
  1184. }
  1185. }
  1186. }
  1187. }
  1188. else
  1189. {
  1190. for (; i >= 0; --i)
  1191. {
  1192. if (val[i] > 0)
  1193. {
  1194. int k = 0, index = i + 1;
  1195. for (; (size_t)(index) < val.size() && val[index] == 0; ++k, ++index)
  1196. ; // count adjacent zeros on right
  1197. //if ((size_t)(index) < val.size() && val[index] < 0)
  1198. { // number on the left is negative
  1199. val[index] += 1;
  1200. val[i] -= BASE;
  1201. for (; k > 0; --k)
  1202. {
  1203. val[i + k] = -UPPER_BOUND;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209.  
  1210. return isPositive;
  1211. }
  1212.  
  1213. inline void InfInt::removeLeadingZeros()
  1214. {
  1215. //PROFINY_SCOPE
  1216. for (int i = (int) (val.size()) - 1; i > 0; --i) // remove leading 0's
  1217. {
  1218. if (val[i] != 0)
  1219. {
  1220. return;
  1221. }
  1222. else
  1223. {
  1224. val.erase(val.begin() + i);
  1225. }
  1226. }
  1227. }
  1228.  
  1229. inline void InfInt::correct(bool justCheckLeadingZeros, bool hasValidSign)
  1230. {
  1231. //PROFINY_SCOPE
  1232. if (!justCheckLeadingZeros)
  1233. {
  1234. truncateToBase();
  1235.  
  1236. if (equalizeSigns())
  1237. {
  1238. pos = ((val.size() == 1 && val[0] == 0) || !hasValidSign) ? true : pos;
  1239. }
  1240. else
  1241. {
  1242. pos = hasValidSign ? !pos : false;
  1243. for (size_t i = 0; i < val.size(); ++i)
  1244. {
  1245. val[i] = abs(val[i]);
  1246. }
  1247. }
  1248. }
  1249.  
  1250. removeLeadingZeros();
  1251. }
  1252.  
  1253. inline void InfInt::fromString(const std::string& s)
  1254. {
  1255. //PROFINY_SCOPE
  1256. pos = true;
  1257. val.clear();
  1258. val.reserve(s.size() / DIGIT_COUNT + 1);
  1259. int i = (int) s.size() - DIGIT_COUNT;
  1260. for (; i >= 0; i -= DIGIT_COUNT)
  1261. {
  1262. val.push_back(atoi(s.substr(i, DIGIT_COUNT).c_str()));
  1263. }
  1264. if (i > -DIGIT_COUNT)
  1265. {
  1266. std::string ss = s.substr(0, i + DIGIT_COUNT);
  1267. if (ss.size() == 1 && ss[0] == '-')
  1268. {
  1269. pos = false;
  1270. }
  1271. else
  1272. {
  1273. val.push_back(atoi(ss.c_str()));
  1274. }
  1275. }
  1276. if (val.back() < 0)
  1277. {
  1278. val.back() = -val.back();
  1279. pos = false;
  1280. }
  1281. correct(true);
  1282. }
  1283.  
  1284. inline ELEM_TYPE InfInt::dInR(const InfInt& R, const InfInt& D)
  1285. {
  1286. //PROFINY_SCOPE
  1287. ELEM_TYPE min = 0, max = UPPER_BOUND;
  1288. while (max > min)
  1289. {
  1290. ELEM_TYPE avg = max + min;
  1291. div_t dt = my_div(avg, 2);
  1292. avg = dt.rem ? (dt.quot + 1) : dt.quot;
  1293. InfInt prod = D * avg;
  1294. if (R == prod)
  1295. {
  1296. return avg;
  1297. }
  1298. else if (R > prod)
  1299. {
  1300. min = avg;
  1301. }
  1302. else
  1303. {
  1304. max = avg - 1;
  1305. }
  1306. }
  1307. return min;
  1308. }
  1309.  
  1310. inline void InfInt::multiplyByDigit(ELEM_TYPE factor, std::vector<ELEM_TYPE>& val)
  1311. {
  1312. //PROFINY_SCOPE
  1313. ELEM_TYPE carry = 0;
  1314. for (size_t i = 0; i < val.size(); ++i)
  1315. {
  1316. PRODUCT_TYPE pval = val[i] * (PRODUCT_TYPE) factor + carry;
  1317. if (pval >= BASE || pval <= -BASE)
  1318. {
  1319. lldiv_t dt = my_lldiv(pval, BASE);
  1320. carry = (ELEM_TYPE) dt.quot;
  1321. pval = dt.rem;
  1322. }
  1323. else
  1324. {
  1325. carry = 0;
  1326. }
  1327. val[i] = (ELEM_TYPE) pval;
  1328. }
  1329. if (carry > 0)
  1330. {
  1331. val.push_back(carry);
  1332. }
  1333. }
  1334.  
  1335. /**************************************************************/
  1336. /******************** NON-MEMBER OPERATORS ********************/
  1337. /**************************************************************/
  1338.  
  1339. inline std::istream& operator>>(std::istream &s, InfInt &n)
  1340. {
  1341. //PROFINY_SCOPE
  1342. std::string str;
  1343. s >> str;
  1344. n.fromString(str);
  1345. return s;
  1346. }
  1347.  
  1348. inline std::ostream& operator<<(std::ostream &s, const InfInt &n)
  1349. {
  1350. //PROFINY_SCOPE
  1351. if (!n.pos)
  1352. {
  1353. s << '-';
  1354. }
  1355. bool first = true;
  1356. for (int i = (int) n.val.size() - 1; i >= 0; --i)
  1357. {
  1358. if (first)
  1359. {
  1360. s << n.val[i];
  1361. first = false;
  1362. }
  1363. else
  1364. {
  1365. s << std::setfill('0') << std::setw(DIGIT_COUNT) << n.val[i];
  1366. }
  1367. }
  1368. return s;
  1369. }
  1370.  
  1371. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement