fork download
  1. #include <algorithm>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <string>
  8. #include <utility>
  9.  
  10. using namespace std;
  11.  
  12. class BigInt;
  13. class BitField;
  14.  
  15. template <class X>
  16. class Pool {
  17. public:
  18. class Ptr {
  19. public:
  20. Ptr();
  21. Ptr(const Ptr& ptr);
  22. virtual ~Ptr();
  23. Ptr& operator =(const Ptr& ptr);
  24. X& operator *() const;
  25. X* operator ->() const;
  26. X* get() const;
  27. protected:
  28. X* x;
  29. unsigned int* cnt;
  30. void release();
  31. };
  32. protected:
  33. struct Entry {
  34. X* x;
  35. unsigned int* cnt;
  36. };
  37. Entry* buf;
  38. unsigned int buf_sz;
  39. unsigned int cnt;
  40. Pool();
  41. virtual ~Pool();
  42. Entry get();
  43. void putBack(const Entry& ent);
  44. static Pool instance;
  45. friend Ptr;
  46. };
  47.  
  48. extern bool primalityTestMillerRabin(const BigInt& p, const unsigned int& cnt = 100);
  49.  
  50. class BigInt {
  51. public:
  52. BigInt(const int& num = 0);
  53. BigInt(const string& str, const unsigned int& base = 10);
  54. BigInt(const BigInt& num);
  55. BigInt operator -() const;
  56. BigInt operator +(const BigInt& ade) const;
  57. BigInt operator -(const BigInt& sub) const;
  58. BigInt operator *(const BigInt& mer) const;
  59. BigInt operator /(const BigInt& dsr) const;
  60. BigInt operator %(const BigInt& nrm) const;
  61. BigInt operator <<(const size_t& wid) const;
  62. BigInt operator >>(const size_t& wid) const;
  63. BigInt& operator =(const BigInt& num);
  64. BigInt& operator +=(const BigInt& ade);
  65. BigInt& operator -=(const BigInt& sub);
  66. BigInt& operator *=(const BigInt& mer);
  67. BigInt& operator /=(const BigInt& dsr);
  68. BigInt& operator %=(const BigInt& nrm);
  69. BigInt& operator <<=(const unsigned int& wid);
  70. BigInt& operator >>=(const unsigned int& wid);
  71. BigInt& operator ++();
  72. BigInt operator ++(int);
  73. BigInt& operator --();
  74. BigInt operator --(int);
  75. bool operator ==(const BigInt& num) const;
  76. bool operator <(const BigInt& num) const;
  77. bool operator >(const BigInt& num) const;
  78. bool operator !=(const BigInt& num) const;
  79. bool operator <=(const BigInt& num) const;
  80. bool operator >=(const BigInt& num) const;
  81. BigInt power(const BigInt& exp) const;
  82. BigInt power(const BigInt& exp, const BigInt& nrm) const;
  83. BigInt gcd(const BigInt& num) const;
  84. BigInt lcm(const BigInt& num) const;
  85. BigInt inverse(const BigInt& num) const;
  86. BigInt abs() const;
  87. bool isZero() const;
  88. int getSign() const;
  89. bool getLSBit() const;
  90. unsigned int getLSInt() const;
  91. void setInt(const int& num);
  92. string toString(const unsigned int& base = 10) const;
  93. protected:
  94. class BFPtr : public Pool<BitField>::Ptr {
  95. public:
  96. BFPtr();
  97. BFPtr(const unsigned int& num);
  98. BFPtr(const BitField& fld);
  99. };
  100. int sign;
  101. BFPtr fld;
  102. BigInt(const int& sign, const BFPtr& fld);
  103. static const unsigned int INT_WID;
  104. static BFPtr add(const BFPtr& age, const BFPtr& ade);
  105. static BFPtr subtract(const BFPtr& min, const BFPtr& sub);
  106. static BFPtr multiply(const BFPtr& mca, const BFPtr& mer);
  107. static pair<BFPtr, BFPtr> divide(const BFPtr& dde, const BFPtr& dsr);
  108. static unsigned int charToFigure(const char& ch);
  109. static char figureToChar(const unsigned int& fig);
  110. };
  111.  
  112. class BitField {
  113. public:
  114. class IntInputStream {
  115. public:
  116. IntInputStream(const BitField* fld);
  117. bool isEOF() const;
  118. unsigned int getInt();
  119. protected:
  120. const BitField* fld;
  121. unsigned int end;
  122. unsigned int cur;
  123. unsigned int last;
  124. unsigned int low_wid;
  125. unsigned int high_wid;
  126. unsigned int low_mask;
  127. unsigned int high_mask;
  128. unsigned int rem;
  129. unsigned int num;
  130. };
  131. class IntOutputStream {
  132. public:
  133. IntOutputStream(BitField* fld);
  134. virtual ~IntOutputStream();
  135. void putInt(const unsigned int& num);
  136. void flush();
  137. protected:
  138. BitField* fld;
  139. unsigned int end;
  140. unsigned int cur;
  141. unsigned int low_wid;
  142. unsigned int high_wid;
  143. unsigned int low_mask;
  144. unsigned int high_mask;
  145. unsigned int high;
  146. };
  147. BitField();
  148. BitField(const unsigned int& num);
  149. BitField(const BitField& fld);
  150. virtual ~BitField();
  151. BitField& operator =(const BitField& fld);
  152. unsigned int getWidth() const;
  153. bool isEmpty() const;
  154. bool isZero() const;
  155. bool getBit(const unsigned int& pos) const;
  156. bool getLSBit() const;
  157. bool getMSBit() const;
  158. unsigned int getLSInt() const;
  159. void setBit(const unsigned int& pos, const bool& bit);
  160. void pushMSBit(const bool& bit);
  161. void pushLSBit(const bool& bit);
  162. void setInt(const unsigned int& num);
  163. void shift(const int& wid);
  164. void trim();
  165. void clear();
  166. int compare(const BitField& fld) const;
  167. string toString() const;
  168. protected:
  169. unsigned char* buf;
  170. unsigned int buf_sz;
  171. int beg;
  172. unsigned int wid;
  173. bool extendIfNecessary(const int& wid);
  174. friend IntInputStream;
  175. friend IntOutputStream;
  176. };
  177.  
  178. #define ASSERT(pred) assert(#pred, pred);
  179.  
  180. extern void assert(const char* pred_str, const bool& pred);
  181.  
  182. template <class X>
  183. Pool<X>::Ptr::Ptr() {
  184. Entry ent = Pool<X>::instance.get();
  185. this->x = ent.x;
  186. this->cnt = ent.cnt;
  187. if (this->cnt) (*this->cnt)++;
  188. }
  189.  
  190. template <class X>
  191. Pool<X>::Ptr::Ptr(const Ptr& ptr) : x(ptr.x), cnt(ptr.cnt) {
  192. if (this->cnt) (*this->cnt)++;
  193. }
  194.  
  195. template <class X>
  196. Pool<X>::Ptr::~Ptr() {
  197. release();
  198. }
  199.  
  200. template <class X>
  201. typename Pool<X>::Ptr& Pool<X>::Ptr::operator =(const Ptr& ptr) {
  202. release();
  203. this->x = ptr.x;
  204. this->cnt = ptr.cnt;
  205. if (this->cnt) (*this->cnt)++;
  206. return *this;
  207. }
  208.  
  209. template <class X>
  210. X& Pool<X>::Ptr::operator *() const {
  211. return *this->x;
  212. }
  213.  
  214. template <class X>
  215. X* Pool<X>::Ptr::operator ->() const {
  216. return this->x;
  217. }
  218.  
  219. template <class X>
  220. X* Pool<X>::Ptr::get() const {
  221. return this->x;
  222. }
  223.  
  224. template <class X>
  225. void Pool<X>::Ptr::release() {
  226. if (this->cnt && --*this->cnt == 0) {
  227. Entry ent;
  228. ent.x = this->x;
  229. ent.cnt = this->cnt;
  230. Pool<X>::instance.putBack(ent);
  231. }
  232. }
  233.  
  234. template <class X>
  235. Pool<X>::Pool() : buf(nullptr), buf_sz(0), cnt(0) {}
  236.  
  237. template <class X>
  238. Pool<X>::~Pool() {
  239. for (int i = 0; i < this->cnt; i++) {
  240. delete this->buf[i].x;
  241. delete this->buf[i].cnt;
  242. }
  243. delete[] this->buf;
  244. }
  245.  
  246. template <class X>
  247. typename Pool<X>::Entry Pool<X>::get() {
  248. if (this->cnt == 0) {
  249. Entry ent;
  250. ent.x = new X;
  251. ent.cnt = new unsigned int(0);
  252. putBack(ent);
  253. }
  254. return this->buf[--this->cnt];
  255. }
  256.  
  257. template <class X>
  258. void Pool<X>::putBack(const Entry& ent) {
  259. unsigned int need_sz = this->cnt + 1;
  260. if (need_sz > this->buf_sz) {
  261. unsigned int new_buf_sz = need_sz * 2;
  262. Entry* new_buf = new Entry[new_buf_sz];
  263. if (this->buf_sz != 0) {
  264. memcpy(new_buf, this->buf, sizeof(Entry) * this->cnt);
  265. delete[] this->buf;
  266. }
  267. this->buf = new_buf;
  268. this->buf_sz = new_buf_sz;
  269. }
  270. this->buf[this->cnt++] = ent;
  271. }
  272.  
  273. template <class X>
  274. Pool<X> Pool<X>::instance;
  275.  
  276. bool primalityTestMillerRabin(const BigInt& p, const unsigned int& cnt) {
  277. bool res = true;
  278. if (p == 1) res = false;
  279. else if (p == 2) res = true;
  280. else if (p.getLSBit() == false) res = false;
  281. else {
  282. BigInt o = p - 1;
  283. BigInt q = o;
  284. BigInt k(1);
  285. while (q.getLSBit() == false) {
  286. q >>= 1;
  287. k++;
  288. }
  289. BigInt a(2);
  290. BigInt gap((o - 2) / cnt);
  291. if (gap.isZero()) gap = 1;
  292. for (; a < p; a += gap) {
  293. BigInt b = a.power(q, p);
  294. if (b != 1 && b != o) {
  295. BigInt i(1);
  296. for (; i < k; i++) {
  297. b = (b * b) % p;
  298. if (b == o) break;
  299. }
  300. if (i == k) {
  301. res = false;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. return res;
  308. }
  309.  
  310. BigInt::BFPtr::BFPtr() {
  311. this->x->clear();
  312. }
  313.  
  314. BigInt::BFPtr::BFPtr(const unsigned int& num) {
  315. this->x->setInt(num);
  316. }
  317.  
  318. BigInt::BFPtr::BFPtr(const BitField& fld) {
  319. *this->x = fld;
  320. }
  321.  
  322. BigInt::BigInt(const int& num) {
  323. this->sign = num >= 0 ? 1 : -1;
  324. this->fld->setInt(this->sign * num);
  325. }
  326.  
  327. BigInt::BigInt(const string& str, const unsigned int& base) {
  328. this->sign = 1;
  329. this->fld->setInt(0);
  330. BFPtr base_fld(base);
  331. string::const_iterator iter = str.begin();
  332. if (*iter == '-') {
  333. this->sign = -1;
  334. iter++;
  335. }
  336. for (; iter != str.end(); iter++) {
  337. if (iter != str.begin()) {
  338. this->fld = multiply(this->fld, base_fld);
  339. }
  340. unsigned int fig = charToFigure(*iter);
  341. if (fig >= base) throw string("値が範囲を逸脱している。");
  342. BFPtr ade(fig);
  343. this->fld = add(this->fld, ade);
  344. }
  345. if (this->fld->isZero()) this->sign = 1;
  346. }
  347.  
  348. BigInt::BigInt(const BigInt& num) {
  349. this->sign = num.fld->isZero() ? 1 : num.sign;
  350. *this->fld = *num.fld;
  351. }
  352.  
  353. BigInt BigInt::operator -() const {
  354. return BigInt(-this->sign, BFPtr(*this->fld));
  355. }
  356.  
  357. BigInt BigInt::operator +(const BigInt& ade) const {
  358. if (this->sign == ade.sign)
  359. return BigInt(this->sign, add(this->fld, ade.fld));
  360. else {
  361. int sign;
  362. BFPtr gre_fld, less_fld;
  363. if (this->fld->compare(*ade.fld) >= 0) {
  364. sign = this->sign;
  365. gre_fld = this->fld;
  366. less_fld = ade.fld;
  367. }
  368. else {
  369. sign = ade.sign;
  370. gre_fld = ade.fld;
  371. less_fld = this->fld;
  372. }
  373. return BigInt(sign, subtract(gre_fld, less_fld));
  374. }
  375. }
  376.  
  377. BigInt BigInt::operator -(const BigInt& sub) const {
  378. return operator +(-sub);
  379. }
  380.  
  381. BigInt BigInt::operator *(const BigInt& mer) const {
  382. return BigInt(this->sign * mer.sign, multiply(this->fld, mer.fld));
  383. }
  384.  
  385. BigInt BigInt::operator /(const BigInt& dsr) const {
  386. return BigInt(this->sign * dsr.sign, divide(this->fld, dsr.fld).first);
  387. }
  388.  
  389. BigInt BigInt::operator %(const BigInt& nrm) const {
  390. return BigInt(this->sign, divide(this->fld, nrm.fld).second);
  391. }
  392.  
  393. BigInt BigInt::operator <<(const unsigned int& wid) const {
  394. BFPtr fld(*this->fld);
  395. fld->shift(-wid);
  396. return BigInt(this->sign, fld);
  397. }
  398.  
  399. BigInt BigInt::operator >>(const unsigned int& wid) const {
  400. BFPtr fld(*this->fld);
  401. fld->shift(wid);
  402. return BigInt(this->sign, fld);
  403. }
  404.  
  405. BigInt& BigInt::operator =(const BigInt& num) {
  406. if (&num != this) {
  407. this->sign = num.fld->isZero() ? 1 : num.sign;
  408. *this->fld = *num.fld;
  409. }
  410. return *this;
  411. }
  412.  
  413. BigInt& BigInt::operator +=(const BigInt& add) {
  414. return *this = *this + add;
  415. }
  416.  
  417. BigInt& BigInt::operator -=(const BigInt& sub) {
  418. return *this = *this - sub;
  419. }
  420.  
  421. BigInt& BigInt::operator *=(const BigInt& mer) {
  422. return *this = *this * mer;
  423. }
  424.  
  425. BigInt& BigInt::operator /=(const BigInt& dsr) {
  426. return *this = *this / dsr;
  427. }
  428.  
  429. BigInt& BigInt::operator %=(const BigInt& nrm) {
  430. return *this = *this % nrm;
  431. }
  432.  
  433. BigInt& BigInt::operator <<=(const size_t& wid) {
  434. return *this = *this << wid;
  435. }
  436.  
  437. BigInt& BigInt::operator >>=(const size_t& wid) {
  438. return *this = *this >> wid;
  439. }
  440.  
  441. BigInt& BigInt::operator ++() {
  442. return *this += 1;
  443. }
  444.  
  445. BigInt BigInt::operator ++(int) {
  446. BigInt res = *this;
  447. *this += 1;
  448. return res;
  449. }
  450.  
  451. BigInt& BigInt::operator --() {
  452. return *this -= 1;
  453. }
  454.  
  455. BigInt BigInt::operator --(int) {
  456. BigInt res = *this;
  457. *this -= 1;
  458. return res;
  459. }
  460.  
  461. bool BigInt::operator ==(const BigInt& num) const {
  462. return this->sign == num.sign && this->fld->compare(*num.fld) == 0;
  463. }
  464.  
  465. bool BigInt::operator <(const BigInt& num) const {
  466. int cmp_res = this->fld->compare(*num.fld);
  467. return (this->sign >= 0 && num.sign >= 0 && cmp_res < 0) ||
  468. (this->sign < 0 && num.sign >= 0) ||
  469. (this->sign < 0 && num.sign < 0 && cmp_res > 0);
  470. }
  471.  
  472. bool BigInt::operator >(const BigInt& num) const {
  473. int cmp_res = this->fld->compare(*num.fld);
  474. return (this->sign >= 0 && num.sign >= 0 && cmp_res > 0) ||
  475. (this->sign >= 0 && num.sign < 0) ||
  476. (this->sign < 0 && num.sign < 0 && cmp_res < 0);
  477. }
  478.  
  479. bool BigInt::operator !=(const BigInt& num) const {
  480. return !operator ==(num);
  481. }
  482.  
  483. bool BigInt::operator <=(const BigInt& num) const {
  484. return !operator >(num);
  485. }
  486.  
  487. bool BigInt::operator >=(const BigInt& num) const {
  488. return !operator <(num);
  489. }
  490.  
  491. BigInt BigInt::power(const BigInt& exp) const {
  492. if (isZero()) return BigInt(0);
  493. if (exp.isZero()) return BigInt(1);
  494. if (exp < 0) return BigInt(1) / power(-exp);
  495. BigInt res(1);
  496. BigInt coef = *this;
  497. for (BigInt exp2 = exp;;) {
  498. if (exp2.getLSBit()) {
  499. res *= coef;
  500. }
  501. exp2 >>= 1;
  502. if (exp2.isZero()) break;
  503. coef *= coef;
  504. }
  505. return res;
  506. }
  507.  
  508. BigInt BigInt::power(const BigInt& exp, const BigInt& nrm) const {
  509. if (isZero()) return BigInt(0);
  510. if (exp.isZero()) return BigInt(1);
  511. if (exp < 0) return (BigInt(1) / power(-exp)) % nrm;
  512. BigInt res(1);
  513. BigInt coef = *this % nrm;
  514. for (BigInt exp2 = exp;;) {
  515. if (exp2.getLSBit()) {
  516. res = (res * coef) % nrm;
  517. }
  518. exp2 >>= 1;
  519. if (exp2.isZero()) break;
  520. coef = (coef * coef) % nrm;
  521. }
  522. return res;
  523. }
  524.  
  525. BigInt BigInt::gcd(const BigInt& num) const {
  526. BigInt a(this->abs()), b(num.abs()), r;
  527. if (a < b) swap(a, b);
  528. for (;;) {
  529. r = a % b;
  530. if (r == 0) break;
  531. a = b;
  532. b = r;
  533. };
  534. if (b.sign != this->sign) b = -b;
  535. return b;
  536. }
  537.  
  538. BigInt BigInt::lcm(const BigInt& num) const {
  539. return *this * num / gcd(num);
  540. }
  541.  
  542. BigInt BigInt::inverse(const BigInt& nrm) const {
  543. BigInt a(this->abs()), b(nrm.abs()), r, q, c, d(1), e(0);
  544. if (a < b) {
  545. swap(a, b);
  546. swap(d, e);
  547. }
  548. for (;;) {
  549. q = a / b;
  550. r = a % b;
  551. if (r == 0) break;
  552. c = d - e * q;
  553. d = e;
  554. e = c;
  555. a = b;
  556. b = r;
  557. };
  558. if (c.sign != nrm.sign) c += nrm;
  559. return c;
  560. }
  561.  
  562. BigInt BigInt::abs() const {
  563. return BigInt(1, BFPtr(*this->fld));
  564. }
  565.  
  566. bool BigInt::isZero() const {
  567. return this->fld->isZero();
  568. }
  569.  
  570. int BigInt::getSign() const {
  571. return this->sign;
  572. }
  573.  
  574. bool BigInt::getLSBit() const {
  575. return this->fld->getLSBit();
  576. }
  577.  
  578. unsigned int BigInt::getLSInt() const {
  579. return this->fld->getLSInt();
  580. }
  581.  
  582. void BigInt::setInt(const int& num) {
  583. this->sign = num >= 0 ? 1 : -1;
  584. this->fld->setInt(this->sign * num);
  585. }
  586.  
  587. string BigInt::toString(const unsigned int& base) const {
  588. string res = "";
  589. pair<BFPtr, BFPtr> quo_rem(BFPtr(*this->fld), BFPtr());
  590. BFPtr base_fld;
  591. base_fld->setInt(base);
  592. while (!quo_rem.first->isZero()) {
  593. quo_rem = divide(quo_rem.first, base_fld);
  594. res += figureToChar(quo_rem.second->getLSInt());
  595. }
  596. if (res.empty()) res += '0';
  597. if (this->sign < 0) res += '-';
  598. reverse(res.begin(), res.end());
  599. size_t pos = 0;
  600. for (; pos < res.length() - 1; pos++) if (res[pos] != '0') break;
  601. return res.substr(pos, res.length() - pos);
  602. }
  603.  
  604. BigInt::BigInt(const int& sign, const BFPtr& fld) {
  605. this->sign = fld->isZero() ? 1 : sign;
  606. this->fld = fld;
  607. }
  608.  
  609. const unsigned int BigInt::INT_WID = sizeof(unsigned int) << 3;
  610.  
  611. BigInt::BFPtr BigInt::add(const BFPtr& age, const BFPtr& ade) {
  612. BFPtr sum;
  613. BitField::IntOutputStream sum_out(sum.get());
  614. BitField::IntInputStream age_in(age.get());
  615. BitField::IntInputStream ade_in(ade.get());
  616. bool carry = false;
  617. while (!age_in.isEOF() || !ade_in.isEOF()) {
  618. unsigned int age_num = !age_in.isEOF() ? age_in.getInt() : 0;
  619. unsigned int ade_num = !ade_in.isEOF() ? ade_in.getInt() : 0;
  620. unsigned int sum_num = (age_num & ~(1 << (INT_WID - 1))) + (ade_num & ~(1 << (INT_WID - 1))) + (carry ? 1 : 0);
  621. int ms_num = (age_num >> (INT_WID - 1)) + (ade_num >> (INT_WID - 1)) + (sum_num >> (INT_WID - 1));
  622. carry = ms_num > 1;
  623. if (carry) ms_num -= 2;
  624. sum_num = (sum_num & ~(1 << (INT_WID - 1))) | (ms_num << (INT_WID - 1));
  625. sum_out.putInt(sum_num);
  626. }
  627. sum_out.flush();
  628. if (carry) sum->pushMSBit(true);
  629. if (sum->isEmpty()) sum->pushMSBit(false);
  630. sum->trim();
  631. return sum;
  632. }
  633.  
  634. BigInt::BFPtr BigInt::subtract(const BFPtr& min, const BFPtr& sub) {
  635. BFPtr dif;
  636. BitField::IntOutputStream dif_out(dif.get());
  637. BitField::IntInputStream min_in(min.get());
  638. BitField::IntInputStream sub_in(sub.get());
  639. bool borrow = false;
  640. while (!min_in.isEOF() || !sub_in.isEOF()) {
  641. unsigned int min_num = !min_in.isEOF() ? min_in.getInt() : 0;
  642. unsigned int sub_num = !sub_in.isEOF() ? sub_in.getInt() : 0;
  643. unsigned int dif_num = (min_num | (1 << (INT_WID - 1))) - (sub_num & ~(1 << (INT_WID - 1))) - (borrow ? 1 : 0);
  644. int ms_num = (min_num >> (INT_WID - 1)) - (sub_num >> (INT_WID - 1)) - (1 - (dif_num >> (INT_WID - 1)));
  645. borrow = ms_num < 0;
  646. if (borrow) ms_num += 2;
  647. dif_num = (dif_num & ~(1 << (INT_WID - 1))) | (ms_num << (INT_WID - 1));
  648. dif_out.putInt(dif_num);
  649. }
  650. dif_out.flush();
  651. if (borrow) throw string("被減数が減数より小さい。");
  652. if (dif->isEmpty()) dif->pushMSBit(false);
  653. dif->trim();
  654. return dif;
  655. }
  656.  
  657. BigInt::BFPtr BigInt::multiply(const BFPtr& mca, const BFPtr& mer) {
  658. BFPtr pro(0);
  659. BFPtr wrk(*mca);
  660. for (unsigned int mer_pos = 0; mer_pos < mer->getWidth(); mer_pos++) {
  661. if (mer->getBit(mer_pos) == true) pro = add(pro, wrk);
  662. wrk->shift(-1);
  663. }
  664. return pro;
  665. }
  666.  
  667. pair<BigInt::BFPtr, BigInt::BFPtr> BigInt::divide(const BFPtr& dde, const BFPtr& dsr) {
  668. if (dsr->isZero()) throw string("除数がゼロ。");
  669. pair<BFPtr, BFPtr> quo_rem;
  670. for (unsigned int dde_next_pos = dde->getWidth(); dde_next_pos > 0; dde_next_pos--) {
  671. unsigned int dde_pos = dde_next_pos - 1;
  672. quo_rem.second->pushLSBit(dde->getBit(dde_pos));
  673. if (quo_rem.second->compare(*dsr) >= 0) {
  674. quo_rem.first->pushLSBit(true);
  675. quo_rem.second = subtract(quo_rem.second, dsr);
  676. }
  677. else quo_rem.first->pushLSBit(false);
  678. }
  679. if (quo_rem.first->isEmpty()) quo_rem.first->pushMSBit(false);
  680. if (quo_rem.second->isEmpty()) quo_rem.second->pushMSBit(false);
  681. return quo_rem;
  682. }
  683.  
  684. unsigned int BigInt::charToFigure(const char& ch) {
  685. unsigned int res;
  686. if (ch >= '0' && ch <= '9') res = ch - '0';
  687. else if (ch >= 'A' && ch <= 'Z') res = 10 + ch - 'A';
  688. else if (ch >= 'a' && ch <= 'z') res = 10 + ch - 'a';
  689. else throw string("文字が数ではない。");
  690. return res;
  691. }
  692.  
  693. char BigInt::figureToChar(const unsigned int& fig) {
  694. char res;
  695. if (fig >= 0 && fig <= 9) res = '0' + fig;
  696. else if (fig >= 10 && fig <= 36) res = 'A' + (fig - 10);
  697. else throw string("値が範囲を逸脱している。");
  698. return res;
  699. }
  700.  
  701. BitField::IntInputStream::IntInputStream(const BitField* fld) {
  702. this->fld = fld;
  703. this->end = fld->buf_sz / sizeof(unsigned int);
  704. this->cur = (this->fld->beg >> 3) / sizeof(unsigned int);
  705. unsigned int fld_end = this->fld->beg + this->fld->wid;
  706. unsigned int buf_wid = this->fld->buf_sz << 3;
  707. unsigned int act_fld_end;
  708. if (fld_end <= buf_wid) act_fld_end = fld_end;
  709. else act_fld_end = fld_end - buf_wid;
  710. this->last = ((act_fld_end - 1) >> 3) / sizeof(unsigned int);
  711. this->high_wid = this->fld->beg & ((sizeof(unsigned int) << 3) - 1);
  712. this->low_wid = (sizeof(unsigned int) << 3) - this->high_wid;
  713. this->high_mask = (1 << this->high_wid) - 1;
  714. this->low_mask = ~this->high_mask;
  715. this->rem = this->fld->wid;
  716. if (this->fld->wid != 0) this->num = ((unsigned int*)this->fld->buf)[this->cur];
  717. }
  718.  
  719. bool BitField::IntInputStream::isEOF() const {
  720. return this->rem == 0;
  721. }
  722.  
  723. unsigned int BitField::IntInputStream::getInt() {
  724. unsigned int res = 0;
  725. res |= (this->num & this->low_mask) >> this->high_wid;
  726. if (this->rem >= this->low_wid) {
  727. this->rem -= this->low_wid;
  728. if (this->rem != 0) {
  729. this->cur++;
  730. if (this->cur == this->end) this->cur = 0;
  731. this->num = ((unsigned int*)this->fld->buf)[this->cur];
  732. if (this->high_wid != 0) {
  733. res |= (this->num & this->high_mask) << this->low_wid;
  734. if (this->rem >= this->high_wid)
  735. this->rem -= this->high_wid;
  736. else {
  737. res &= (1 << (this->low_wid + this->rem)) - 1;
  738. this->rem = 0;
  739. }
  740. }
  741. }
  742. }
  743. else {
  744. res &= (1 << this->rem) - 1;
  745. this->rem = 0;
  746. }
  747. return res;
  748. }
  749.  
  750. BitField::IntOutputStream::IntOutputStream(BitField* fld) {
  751. this->fld = fld;
  752. this->end = this->fld->buf_sz / sizeof(unsigned int);
  753. unsigned int fld_end = this->fld->beg + this->fld->wid;
  754. unsigned int buf_wid = this->fld->buf_sz << 3;
  755. unsigned int act_fld_end;
  756. if (fld_end <= buf_wid) act_fld_end = fld_end;
  757. else act_fld_end = fld_end - buf_wid;
  758. this->cur = (act_fld_end >> 3) / sizeof(unsigned int);
  759. this->high_wid = act_fld_end & ((sizeof(unsigned int) << 3) - 1);
  760. this->low_wid = (sizeof(unsigned int) << 3) - this->high_wid;
  761. this->high_mask = (1 << this->high_wid) - 1;
  762. this->low_mask = ~this->high_mask;
  763. if (this->fld->wid != 0)
  764. this->high = ((unsigned int*)this->fld->buf)[this->cur] & this->high_mask;
  765. else this->high = 0;
  766. }
  767.  
  768. BitField::IntOutputStream::~IntOutputStream() {
  769. flush();
  770. }
  771.  
  772. void BitField::IntOutputStream::putInt(const unsigned int& num) {
  773. this->fld->extendIfNecessary(sizeof(unsigned int) << 3);
  774. ((unsigned int*)this->fld->buf)[this->cur] = this->high | ((num << this->high_wid) & this->low_mask);
  775. this->cur++;
  776. if (this->high_wid != 0)
  777. this->high = num >> this->low_wid;
  778. }
  779.  
  780. void BitField::IntOutputStream::flush() {
  781. if (this->high_wid != 0)
  782. ((unsigned int*)this->fld->buf)[this->cur] = this->high | (((unsigned int*)this->fld->buf)[this->cur] & this->low_mask);
  783. }
  784.  
  785. BitField::BitField() : buf(nullptr), buf_sz(0), beg(0), wid(0) {}
  786.  
  787. BitField::BitField(const unsigned int& num) : buf(nullptr), buf_sz(0), beg(0), wid(0) {
  788. extendIfNecessary(sizeof(unsigned int) << 3);
  789. *(unsigned int*)this->buf = num;
  790. trim();
  791. }
  792.  
  793. BitField::BitField(const BitField& fld) : buf(nullptr), buf_sz(0), beg(0), wid(0) {
  794. if (fld.wid != 0) {
  795. this->buf_sz = fld.buf_sz;
  796. this->buf = new unsigned char[this->buf_sz];
  797. memcpy(this->buf, fld.buf, fld.buf_sz);
  798. this->beg = fld.beg;
  799. this->wid = fld.wid;
  800. }
  801. }
  802.  
  803. BitField::~BitField() {
  804. if (this->buf) delete[] this->buf;
  805. }
  806.  
  807. BitField& BitField::operator =(const BitField& fld) {
  808. if (&fld != this) {
  809. this->wid = 0;
  810. if (fld.wid != 0) {
  811. this->buf_sz = fld.buf_sz;
  812. this->buf = new unsigned char[this->buf_sz];
  813. memcpy(this->buf, fld.buf, fld.buf_sz);
  814. this->beg = fld.beg;
  815. this->wid = fld.wid;
  816. }
  817. }
  818. return *this;
  819. }
  820.  
  821. unsigned int BitField::getWidth() const {
  822. return this->wid;
  823. }
  824.  
  825. bool BitField::isEmpty() const {
  826. return this->wid == 0;
  827. }
  828.  
  829. bool BitField::isZero() const {
  830. bool res = true;
  831. for (unsigned int pos = 0; pos < this->wid; pos++) {
  832. if (getBit(pos) == true) {
  833. res = false;
  834. break;
  835. }
  836. }
  837. return res;
  838. }
  839.  
  840. bool BitField::getBit(const unsigned int& pos) const {
  841. unsigned int abs_pos = this->beg + pos;
  842. unsigned int buf_wid = this->buf_sz << 3;
  843. if (abs_pos >= buf_wid) abs_pos -= buf_wid;
  844. return ((this->buf[abs_pos >> 3] >> (abs_pos & 0x7)) & 1) == 1;
  845. }
  846.  
  847. bool BitField::getLSBit() const {
  848. return getBit(0);
  849. }
  850.  
  851. bool BitField::getMSBit() const {
  852. return getBit(this->wid - 1);
  853. }
  854.  
  855. unsigned int BitField::getLSInt() const {
  856. unsigned int res = 0;
  857. unsigned int int_wid = sizeof(unsigned int) << 3;
  858. for (unsigned int pos = 0; pos < int_wid && pos < this->wid; pos++)
  859. res |= (getBit(pos) == true ? 1 : 0) << pos;
  860. return res;
  861. }
  862.  
  863. void BitField::setBit(const unsigned int& pos, const bool& bit) {
  864. unsigned int abs_pos = this->beg + pos;
  865. unsigned int buf_wid = this->buf_sz << 3;
  866. if (abs_pos >= buf_wid) abs_pos -= buf_wid;
  867. if (bit) this->buf[abs_pos >> 3] |= 1 << (abs_pos & 0x7);
  868. else this->buf[abs_pos >> 3] &= ~(1 << (abs_pos & 0x7));
  869. }
  870.  
  871. void BitField::pushMSBit(const bool& bit) {
  872. extendIfNecessary(1);
  873. setBit(this->wid - 1, bit);
  874. }
  875.  
  876. void BitField::pushLSBit(const bool& bit) {
  877. extendIfNecessary(-1);
  878. setBit(0, bit);
  879. }
  880.  
  881. void BitField::setInt(const unsigned int& num) {
  882. clear();
  883. extendIfNecessary(sizeof(unsigned int) << 3);
  884. *(unsigned int*)this->buf = num;
  885. trim();
  886. }
  887.  
  888. void BitField::shift(const int& wid) {
  889. if (wid < 0) {
  890. extendIfNecessary(wid);
  891. for (unsigned int pos = 0; pos < -wid; pos++)
  892. setBit(pos, false);
  893. }
  894. else {
  895. this->beg += wid;
  896. unsigned int buf_wid = this->buf_sz << 3;
  897. if (this->beg >= buf_wid) this->beg -= buf_wid;
  898. this->wid -= wid;
  899. }
  900. }
  901.  
  902. void BitField::trim() {
  903. while (this->wid != 0 && getMSBit() == false) this->wid--;
  904. }
  905.  
  906. void BitField::clear() {
  907. this->beg = 0;
  908. this->wid = 0;
  909. }
  910.  
  911. int BitField::compare(const BitField& fld) const {
  912. int res = 0;
  913. for (unsigned int next_pos = this->wid < fld.wid ? fld.wid : this->wid; next_pos > 0; next_pos--) {
  914. unsigned int pos = next_pos - 1;
  915. bool bit1 = pos < this->wid ? getBit(pos) : false;
  916. bool bit2 = pos < fld.wid ? fld.getBit(pos) : false;
  917. if (bit1 == false && bit2 == true) {
  918. res = -1;
  919. break;
  920. }
  921. else if (bit1 == true && bit2 == false) {
  922. res = 1;
  923. break;
  924. }
  925. }
  926. return res;
  927. }
  928.  
  929. string BitField::toString() const {
  930. string res = "";
  931. for (unsigned int pos = 0; pos < this->wid; pos++)
  932. res += getBit(pos) == true ? '1' : '0';
  933. return res;
  934. }
  935.  
  936. bool BitField::extendIfNecessary(const int& wid) {
  937. bool res = false;
  938. unsigned int abs_wid = wid < 0 ? -wid : wid;
  939. unsigned int need_sz = (this->wid + abs_wid + 7) >> 3;
  940. if (need_sz > this->buf_sz) {
  941. unsigned int new_buf_sz = need_sz << 1;
  942. new_buf_sz = ((new_buf_sz + 3) >> 2) << 2;
  943. unsigned char* new_buf = new unsigned char[new_buf_sz];
  944. unsigned int new_beg = 0;
  945. if (this->wid != 0) {
  946. unsigned int fld_beg = this->beg >> 3;
  947. unsigned int fld_end = (this->beg + this->wid + 7) >> 3;
  948. if (fld_end <= this->buf_sz) {
  949. unsigned int fld_sz = fld_end - fld_beg;
  950. memcpy(new_buf + fld_beg, this->buf + fld_beg, fld_sz);
  951. new_beg = this->beg;
  952. }
  953. else {
  954. unsigned int low_sz = this->buf_sz - fld_beg;
  955. unsigned int new_low_beg = new_buf_sz - low_sz;
  956. memcpy(new_buf + new_low_beg, this->buf + fld_beg, low_sz);
  957. unsigned int high_sz = fld_end - this->buf_sz;
  958. memcpy(new_buf, this->buf, high_sz);
  959. unsigned int buf_wid = this->buf_sz << 3;
  960. unsigned int low_wid = buf_wid - this->beg;
  961. unsigned int new_buf_wid = new_buf_sz << 3;
  962. new_beg = new_buf_wid - low_wid;
  963. }
  964. delete[] this->buf;
  965. res = true;
  966. }
  967. this->buf_sz = new_buf_sz;
  968. this->buf = new_buf;
  969. this->beg = new_beg;
  970. }
  971. if (wid < 0) {
  972. this->beg += wid;
  973. int buf_wid = this->buf_sz << 3;
  974. if (this->beg < 0) this->beg += buf_wid;
  975. }
  976. this->wid += abs_wid;
  977. return res;
  978. }
  979.  
  980. void assert(const char* pred_str, const bool& pred) {
  981. if (pred) {
  982. cout << "アサート成功: " << pred_str << endl;
  983. }
  984. else {
  985. cerr << "アサート失敗: " << pred_str << endl;
  986. exit(1);
  987. }
  988. }
  989.  
  990. int main() {
  991. try {
  992. clock_t begin = clock();
  993.  
  994. ASSERT(BigInt(0).getSign() == 1)
  995. ASSERT(BigInt(0).getLSInt() == 0)
  996. ASSERT(BigInt(1).getSign() == 1)
  997. ASSERT(BigInt(1).getLSInt() == 1)
  998. ASSERT(BigInt(123).getSign() == 1)
  999. ASSERT(BigInt(123).getLSInt() == 123)
  1000. ASSERT(BigInt(1234567890).getSign() == 1)
  1001. ASSERT(BigInt(1234567890).getLSInt() == 1234567890)
  1002. ASSERT(BigInt(-0).getSign() == 1)
  1003. ASSERT(BigInt(-0).getLSInt() == 0)
  1004. ASSERT(BigInt(-1).getSign() == -1)
  1005. ASSERT(BigInt(-1).getLSInt() == 1)
  1006. ASSERT(BigInt(-123).getSign() == -1)
  1007. ASSERT(BigInt(-123).getLSInt() == 123)
  1008. ASSERT(BigInt(-1234567890).getSign() == -1)
  1009. ASSERT(BigInt(-1234567890).getLSInt() == 1234567890)
  1010.  
  1011. ASSERT(BigInt("0").toString() == "0")
  1012. ASSERT(BigInt("-0").toString() == "0")
  1013. ASSERT(BigInt("1").toString() == "1")
  1014. ASSERT(BigInt("-1").toString() == "-1")
  1015. ASSERT(BigInt("341").toString() == "341")
  1016. ASSERT(BigInt("-341").toString() == "-341")
  1017. ASSERT(BigInt("123342").toString() == "123342")
  1018. ASSERT(BigInt("-123342").toString() == "-123342")
  1019. ASSERT(BigInt("9825724079207842808027478042").toString() == "9825724079207842808027478042")
  1020. ASSERT(BigInt("-9825724079207842808027478042").toString() == "-9825724079207842808027478042")
  1021.  
  1022. ASSERT(-BigInt("0") == BigInt("0"))
  1023. ASSERT(-BigInt("1") == BigInt("-1"))
  1024. ASSERT(-BigInt("123") == BigInt("-123"))
  1025. ASSERT(-BigInt("98765432109876543210") == BigInt("-98765432109876543210"))
  1026. ASSERT(BigInt("107777896329") + BigInt("744936075025") == BigInt("1540169976256") - BigInt("687456004902"))
  1027. ASSERT(BigInt("51") + BigInt("65") == BigInt("116"))
  1028. ASSERT(BigInt("51") + BigInt("-65") == BigInt("-14"))
  1029. ASSERT(BigInt("-51") + BigInt("65") == BigInt("14"))
  1030. ASSERT(BigInt("-51") + BigInt("-65") == BigInt("-116"))
  1031. ASSERT(BigInt("51") - BigInt("65") == BigInt("-14"))
  1032. ASSERT(BigInt("51") - BigInt("-65") == BigInt("116"))
  1033. ASSERT(BigInt("-51") - BigInt("65") == BigInt("-116"))
  1034. ASSERT(BigInt("-51") - BigInt("-65") == BigInt("14"))
  1035. ASSERT(BigInt("2") * BigInt("11111") == BigInt("66666") / BigInt("3"))
  1036. ASSERT(BigInt("37") * BigInt("10") == BigInt("370"))
  1037. ASSERT(BigInt("37") * BigInt("-10") == BigInt("-370"))
  1038. ASSERT(BigInt("-37") * BigInt("10") == BigInt("-370"))
  1039. ASSERT(BigInt("-37") * BigInt("-10") == BigInt("370"))
  1040. ASSERT(BigInt("37") / BigInt("10") == BigInt("3"))
  1041. ASSERT(BigInt("37") / BigInt("-10") == BigInt("-3"))
  1042. ASSERT(BigInt("-37") / BigInt("10") == BigInt("-3"))
  1043. ASSERT(BigInt("-37") / BigInt("-10") == BigInt("3"))
  1044. ASSERT(BigInt("55555555123") % BigInt("10000") == BigInt("5123"))
  1045. ASSERT(BigInt("37") % BigInt("10") == BigInt("7"))
  1046. ASSERT(BigInt("37") % BigInt("-10") == BigInt("7"))
  1047. ASSERT(BigInt("-37") % BigInt("10") == BigInt("-7"))
  1048. ASSERT(BigInt("-37") % BigInt("-10") == BigInt("-7"))
  1049. ASSERT(BigInt("12345678901234567890") == BigInt("12345678901234567890"))
  1050. ASSERT(BigInt("999999999") < BigInt("1000000000"))
  1051. ASSERT(BigInt("1000000001") > BigInt("1000000000"))
  1052. ASSERT(BigInt("789AbCdEf", 16) == BigInt("32374509039"))
  1053. ASSERT(BigInt("789AbCdEf", 16) - BigInt("32374509000") == BigInt("39"))
  1054. ASSERT(BigInt("1A", 16) << 1 == BigInt("34", 16))
  1055. ASSERT(BigInt("1A", 16) >> 2 == BigInt("6", 16))
  1056.  
  1057. {
  1058. BigInt a, b;
  1059. a.setInt(12);
  1060. a = a;
  1061. ASSERT(a == BigInt(12))
  1062. a.setInt(34);
  1063. a = a;
  1064. ASSERT(a == BigInt(34))
  1065. b = a;
  1066. ASSERT(a == BigInt(34))
  1067. ASSERT(b == BigInt(34))
  1068. a.setInt(56);
  1069. ASSERT(a == BigInt(56))
  1070. ASSERT(b == BigInt(34))
  1071. b.setInt(78);
  1072. ASSERT(a == BigInt(56))
  1073. ASSERT(b == BigInt(78))
  1074. }
  1075.  
  1076. ASSERT(BigInt("0").power(BigInt("0")) == BigInt("0"))
  1077. ASSERT(BigInt("1").power(BigInt("0")) == BigInt("1"))
  1078. ASSERT(BigInt("2").power(BigInt("0")) == BigInt("1"))
  1079. ASSERT(BigInt("5222256474787565634234256467876").power(BigInt("0")) == BigInt("1"))
  1080. ASSERT(BigInt("0").power(BigInt("1")) == BigInt("0"))
  1081. ASSERT(BigInt("1").power(BigInt("1")) == BigInt("1"))
  1082. ASSERT(BigInt("2").power(BigInt("1")) == BigInt("2"))
  1083. ASSERT(BigInt("5222256474787565634234256467876").power(BigInt("1")) == BigInt("5222256474787565634234256467876"))
  1084. ASSERT(BigInt("0").power(BigInt("4")) == BigInt("0"))
  1085. ASSERT(BigInt("0").power(BigInt("4"), BigInt("10")) == BigInt("0"))
  1086. ASSERT(BigInt("0").power(BigInt("4"), BigInt("-10")) == BigInt("0"))
  1087. ASSERT(BigInt("1").power(BigInt("4")) == BigInt("1"))
  1088. ASSERT(BigInt("1").power(BigInt("4"), BigInt("10")) == BigInt("1"))
  1089. ASSERT(BigInt("1").power(BigInt("4"), BigInt("-10")) == BigInt("1"))
  1090. ASSERT(BigInt("2").power(BigInt("4")) == BigInt("16"))
  1091. ASSERT(BigInt("2").power(BigInt("4"), BigInt("10")) == BigInt("6"))
  1092. ASSERT(BigInt("2").power(BigInt("4"), BigInt("-10")) == BigInt("6"))
  1093. ASSERT(BigInt("-0").power(BigInt("4")) == BigInt("0"))
  1094. ASSERT(BigInt("-0").power(BigInt("4"), BigInt("10")) == BigInt("0"))
  1095. ASSERT(BigInt("-0").power(BigInt("4"), BigInt("-10")) == BigInt("0"))
  1096. ASSERT(BigInt("-1").power(BigInt("4")) == BigInt("1"))
  1097. ASSERT(BigInt("-1").power(BigInt("4"), BigInt("10")) == BigInt("1"))
  1098. ASSERT(BigInt("-1").power(BigInt("4"), BigInt("-10")) == BigInt("1"))
  1099. ASSERT(BigInt("-2").power(BigInt("4")) == BigInt("16"))
  1100. ASSERT(BigInt("-2").power(BigInt("4"), BigInt("10")) == BigInt("6"))
  1101. ASSERT(BigInt("-2").power(BigInt("4"), BigInt("-10")) == BigInt("6"))
  1102. ASSERT(BigInt("-3").power(BigInt("3")) == BigInt("-27"))
  1103. ASSERT(BigInt("-3").power(BigInt("3"), BigInt("10")) == BigInt("-7"))
  1104. ASSERT(BigInt("-3").power(BigInt("3"), BigInt("-10")) == BigInt("-7"))
  1105. ASSERT(BigInt("0").power(BigInt("-5")) == BigInt("0"))
  1106. ASSERT(BigInt("1").power(BigInt("-5")) == BigInt("1"))
  1107. ASSERT(BigInt("2").power(BigInt("-5")) == BigInt("0"))
  1108. ASSERT(BigInt("0").power(BigInt("-5"), BigInt("10")) == BigInt("0"))
  1109. ASSERT(BigInt("1").power(BigInt("-5"), BigInt("10")) == BigInt("1"))
  1110. ASSERT(BigInt("2").power(BigInt("-5"), BigInt("10")) == BigInt("0"))
  1111. ASSERT(BigInt("0").power(BigInt("-5"), BigInt("-10")) == BigInt("0"))
  1112. ASSERT(BigInt("1").power(BigInt("-5"), BigInt("-10")) == BigInt("1"))
  1113. ASSERT(BigInt("2").power(BigInt("-5"), BigInt("-10")) == BigInt("0"))
  1114. ASSERT(BigInt("12").power(BigInt("34")) == BigInt("4922235242952026704037113243122008064"))
  1115. ASSERT(BigInt("12").power(BigInt("34"), BigInt("100")) == BigInt("64"))
  1116. ASSERT(BigInt("2").power(BigInt("4099"), BigInt("100")) == BigInt("88"))
  1117.  
  1118. ASSERT(primalityTestMillerRabin(BigInt("0")) == false)
  1119. ASSERT(primalityTestMillerRabin(BigInt("1")) == false)
  1120. ASSERT(primalityTestMillerRabin(BigInt("2")) == true)
  1121. ASSERT(primalityTestMillerRabin(BigInt("3")) == true)
  1122. ASSERT(primalityTestMillerRabin(BigInt("4")) == false)
  1123. ASSERT(primalityTestMillerRabin(BigInt("5")) == true)
  1124. ASSERT(primalityTestMillerRabin(BigInt("6")) == false)
  1125. ASSERT(primalityTestMillerRabin(BigInt("7")) == true)
  1126. ASSERT(primalityTestMillerRabin(BigInt("8")) == false)
  1127. ASSERT(primalityTestMillerRabin(BigInt("9")) == false)
  1128. ASSERT(primalityTestMillerRabin(BigInt("87")) == false)
  1129. ASSERT(primalityTestMillerRabin(BigInt("89")) == true)
  1130. ASSERT(primalityTestMillerRabin(BigInt("329")) == false)
  1131. ASSERT(primalityTestMillerRabin(BigInt("331")) == true)
  1132. ASSERT(primalityTestMillerRabin(BigInt("561")) == false)
  1133. ASSERT(primalityTestMillerRabin(BigInt("563")) == true)
  1134. ASSERT(primalityTestMillerRabin(BigInt("807")) == false)
  1135. ASSERT(primalityTestMillerRabin(BigInt("809")) == true)
  1136. ASSERT(primalityTestMillerRabin(BigInt("2251")) == true)
  1137. ASSERT(primalityTestMillerRabin(BigInt("59561")) == true)
  1138. ASSERT(primalityTestMillerRabin(BigInt("181243")) == true)
  1139. ASSERT(primalityTestMillerRabin(BigInt("14414443")) == true)
  1140. ASSERT(primalityTestMillerRabin(BigInt("10461401779")) == true)
  1141. ASSERT(primalityTestMillerRabin(BigInt("1853028778786433")) == true)
  1142. // ASSERT(primalityTestMillerRabin(BigInt("940461086986004843694934910131104208392131088686023657900173332902199657733778583489")) == true)
  1143.  
  1144. ASSERT(BigInt("8").gcd(BigInt("1")) == BigInt("1"))
  1145. ASSERT(BigInt("8").gcd(BigInt("2")) == BigInt("2"))
  1146. ASSERT(BigInt("8").gcd(BigInt("3")) == BigInt("1"))
  1147. ASSERT(BigInt("8").gcd(BigInt("4")) == BigInt("4"))
  1148. ASSERT(BigInt("8").gcd(BigInt("5")) == BigInt("1"))
  1149. ASSERT(BigInt("8").gcd(BigInt("6")) == BigInt("2"))
  1150. ASSERT(BigInt("8").gcd(BigInt("7")) == BigInt("1"))
  1151. ASSERT(BigInt("8").gcd(BigInt("8")) == BigInt("8"))
  1152. ASSERT(BigInt("8").gcd(BigInt("-12")) == BigInt("4"))
  1153. ASSERT(BigInt("-8").gcd(BigInt("12")) == BigInt("-4"))
  1154. ASSERT(BigInt("-8").gcd(BigInt("-12")) == BigInt("-4"))
  1155. ASSERT(BigInt("5").inverse(BigInt("13")) == BigInt("8"))
  1156. ASSERT(BigInt("43").inverse(BigInt("31")) == BigInt("13"))
  1157. ASSERT(BigInt("5").inverse(BigInt("-13")) == BigInt("-5"))
  1158. ASSERT(BigInt("43").inverse(BigInt("-31")) == BigInt("-18"))
  1159. ASSERT(BigInt("18").lcm(BigInt("24")) == BigInt("72"))
  1160. ASSERT(BigInt("18").lcm(BigInt("27")) == BigInt("54"))
  1161.  
  1162. clock_t end = clock();
  1163. clock_t elapsed = end - begin;
  1164. cout << (elapsed / CLOCKS_PER_SEC) << "." << setfill('0') << setw(3) << ((elapsed % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC) << " secs" << endl;
  1165. }
  1166. catch (const string& msg) {
  1167. cerr << msg << endl;
  1168. return 1;
  1169. }
  1170. return 0;
  1171. }
  1172.  
Success #stdin #stdout 0.36s 5656KB
stdin
Standard input is empty
stdout
アサート成功: BigInt(0).getSign() == 1
アサート成功: BigInt(0).getLSInt() == 0
アサート成功: BigInt(1).getSign() == 1
アサート成功: BigInt(1).getLSInt() == 1
アサート成功: BigInt(123).getSign() == 1
アサート成功: BigInt(123).getLSInt() == 123
アサート成功: BigInt(1234567890).getSign() == 1
アサート成功: BigInt(1234567890).getLSInt() == 1234567890
アサート成功: BigInt(-0).getSign() == 1
アサート成功: BigInt(-0).getLSInt() == 0
アサート成功: BigInt(-1).getSign() == -1
アサート成功: BigInt(-1).getLSInt() == 1
アサート成功: BigInt(-123).getSign() == -1
アサート成功: BigInt(-123).getLSInt() == 123
アサート成功: BigInt(-1234567890).getSign() == -1
アサート成功: BigInt(-1234567890).getLSInt() == 1234567890
アサート成功: BigInt("0").toString() == "0"
アサート成功: BigInt("-0").toString() == "0"
アサート成功: BigInt("1").toString() == "1"
アサート成功: BigInt("-1").toString() == "-1"
アサート成功: BigInt("341").toString() == "341"
アサート成功: BigInt("-341").toString() == "-341"
アサート成功: BigInt("123342").toString() == "123342"
アサート成功: BigInt("-123342").toString() == "-123342"
アサート成功: BigInt("9825724079207842808027478042").toString() == "9825724079207842808027478042"
アサート成功: BigInt("-9825724079207842808027478042").toString() == "-9825724079207842808027478042"
アサート成功: -BigInt("0") == BigInt("0")
アサート成功: -BigInt("1") == BigInt("-1")
アサート成功: -BigInt("123") == BigInt("-123")
アサート成功: -BigInt("98765432109876543210") == BigInt("-98765432109876543210")
アサート成功: BigInt("107777896329") + BigInt("744936075025") == BigInt("1540169976256") - BigInt("687456004902")
アサート成功: BigInt("51") + BigInt("65") == BigInt("116")
アサート成功: BigInt("51") + BigInt("-65") == BigInt("-14")
アサート成功: BigInt("-51") + BigInt("65") == BigInt("14")
アサート成功: BigInt("-51") + BigInt("-65") == BigInt("-116")
アサート成功: BigInt("51") - BigInt("65") == BigInt("-14")
アサート成功: BigInt("51") - BigInt("-65") == BigInt("116")
アサート成功: BigInt("-51") - BigInt("65") == BigInt("-116")
アサート成功: BigInt("-51") - BigInt("-65") == BigInt("14")
アサート成功: BigInt("2") * BigInt("11111") == BigInt("66666") / BigInt("3")
アサート成功: BigInt("37") * BigInt("10") == BigInt("370")
アサート成功: BigInt("37") * BigInt("-10") == BigInt("-370")
アサート成功: BigInt("-37") * BigInt("10") == BigInt("-370")
アサート成功: BigInt("-37") * BigInt("-10") == BigInt("370")
アサート成功: BigInt("37") / BigInt("10") == BigInt("3")
アサート成功: BigInt("37") / BigInt("-10") == BigInt("-3")
アサート成功: BigInt("-37") / BigInt("10") == BigInt("-3")
アサート成功: BigInt("-37") / BigInt("-10") == BigInt("3")
アサート成功: BigInt("55555555123") % BigInt("10000") == BigInt("5123")
アサート成功: BigInt("37") % BigInt("10") == BigInt("7")
アサート成功: BigInt("37") % BigInt("-10") == BigInt("7")
アサート成功: BigInt("-37") % BigInt("10") == BigInt("-7")
アサート成功: BigInt("-37") % BigInt("-10") == BigInt("-7")
アサート成功: BigInt("12345678901234567890") == BigInt("12345678901234567890")
アサート成功: BigInt("999999999") < BigInt("1000000000")
アサート成功: BigInt("1000000001") > BigInt("1000000000")
アサート成功: BigInt("789AbCdEf", 16) == BigInt("32374509039")
アサート成功: BigInt("789AbCdEf", 16) - BigInt("32374509000") == BigInt("39")
アサート成功: BigInt("1A", 16) << 1 == BigInt("34", 16)
アサート成功: BigInt("1A", 16) >> 2 == BigInt("6", 16)
アサート成功: a == BigInt(12)
アサート成功: a == BigInt(34)
アサート成功: a == BigInt(34)
アサート成功: b == BigInt(34)
アサート成功: a == BigInt(56)
アサート成功: b == BigInt(34)
アサート成功: a == BigInt(56)
アサート成功: b == BigInt(78)
アサート成功: BigInt("0").power(BigInt("0")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("0")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("0")) == BigInt("1")
アサート成功: BigInt("5222256474787565634234256467876").power(BigInt("0")) == BigInt("1")
アサート成功: BigInt("0").power(BigInt("1")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("1")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("1")) == BigInt("2")
アサート成功: BigInt("5222256474787565634234256467876").power(BigInt("1")) == BigInt("5222256474787565634234256467876")
アサート成功: BigInt("0").power(BigInt("4")) == BigInt("0")
アサート成功: BigInt("0").power(BigInt("4"), BigInt("10")) == BigInt("0")
アサート成功: BigInt("0").power(BigInt("4"), BigInt("-10")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("4")) == BigInt("1")
アサート成功: BigInt("1").power(BigInt("4"), BigInt("10")) == BigInt("1")
アサート成功: BigInt("1").power(BigInt("4"), BigInt("-10")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("4")) == BigInt("16")
アサート成功: BigInt("2").power(BigInt("4"), BigInt("10")) == BigInt("6")
アサート成功: BigInt("2").power(BigInt("4"), BigInt("-10")) == BigInt("6")
アサート成功: BigInt("-0").power(BigInt("4")) == BigInt("0")
アサート成功: BigInt("-0").power(BigInt("4"), BigInt("10")) == BigInt("0")
アサート成功: BigInt("-0").power(BigInt("4"), BigInt("-10")) == BigInt("0")
アサート成功: BigInt("-1").power(BigInt("4")) == BigInt("1")
アサート成功: BigInt("-1").power(BigInt("4"), BigInt("10")) == BigInt("1")
アサート成功: BigInt("-1").power(BigInt("4"), BigInt("-10")) == BigInt("1")
アサート成功: BigInt("-2").power(BigInt("4")) == BigInt("16")
アサート成功: BigInt("-2").power(BigInt("4"), BigInt("10")) == BigInt("6")
アサート成功: BigInt("-2").power(BigInt("4"), BigInt("-10")) == BigInt("6")
アサート成功: BigInt("-3").power(BigInt("3")) == BigInt("-27")
アサート成功: BigInt("-3").power(BigInt("3"), BigInt("10")) == BigInt("-7")
アサート成功: BigInt("-3").power(BigInt("3"), BigInt("-10")) == BigInt("-7")
アサート成功: BigInt("0").power(BigInt("-5")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("-5")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("-5")) == BigInt("0")
アサート成功: BigInt("0").power(BigInt("-5"), BigInt("10")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("-5"), BigInt("10")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("-5"), BigInt("10")) == BigInt("0")
アサート成功: BigInt("0").power(BigInt("-5"), BigInt("-10")) == BigInt("0")
アサート成功: BigInt("1").power(BigInt("-5"), BigInt("-10")) == BigInt("1")
アサート成功: BigInt("2").power(BigInt("-5"), BigInt("-10")) == BigInt("0")
アサート成功: BigInt("12").power(BigInt("34")) == BigInt("4922235242952026704037113243122008064")
アサート成功: BigInt("12").power(BigInt("34"), BigInt("100")) == BigInt("64")
アサート成功: BigInt("2").power(BigInt("4099"), BigInt("100")) == BigInt("88")
アサート成功: primalityTestMillerRabin(BigInt("0")) == false
アサート成功: primalityTestMillerRabin(BigInt("1")) == false
アサート成功: primalityTestMillerRabin(BigInt("2")) == true
アサート成功: primalityTestMillerRabin(BigInt("3")) == true
アサート成功: primalityTestMillerRabin(BigInt("4")) == false
アサート成功: primalityTestMillerRabin(BigInt("5")) == true
アサート成功: primalityTestMillerRabin(BigInt("6")) == false
アサート成功: primalityTestMillerRabin(BigInt("7")) == true
アサート成功: primalityTestMillerRabin(BigInt("8")) == false
アサート成功: primalityTestMillerRabin(BigInt("9")) == false
アサート成功: primalityTestMillerRabin(BigInt("87")) == false
アサート成功: primalityTestMillerRabin(BigInt("89")) == true
アサート成功: primalityTestMillerRabin(BigInt("329")) == false
アサート成功: primalityTestMillerRabin(BigInt("331")) == true
アサート成功: primalityTestMillerRabin(BigInt("561")) == false
アサート成功: primalityTestMillerRabin(BigInt("563")) == true
アサート成功: primalityTestMillerRabin(BigInt("807")) == false
アサート成功: primalityTestMillerRabin(BigInt("809")) == true
アサート成功: primalityTestMillerRabin(BigInt("2251")) == true
アサート成功: primalityTestMillerRabin(BigInt("59561")) == true
アサート成功: primalityTestMillerRabin(BigInt("181243")) == true
アサート成功: primalityTestMillerRabin(BigInt("14414443")) == true
アサート成功: primalityTestMillerRabin(BigInt("10461401779")) == true
アサート成功: primalityTestMillerRabin(BigInt("1853028778786433")) == true
アサート成功: BigInt("8").gcd(BigInt("1")) == BigInt("1")
アサート成功: BigInt("8").gcd(BigInt("2")) == BigInt("2")
アサート成功: BigInt("8").gcd(BigInt("3")) == BigInt("1")
アサート成功: BigInt("8").gcd(BigInt("4")) == BigInt("4")
アサート成功: BigInt("8").gcd(BigInt("5")) == BigInt("1")
アサート成功: BigInt("8").gcd(BigInt("6")) == BigInt("2")
アサート成功: BigInt("8").gcd(BigInt("7")) == BigInt("1")
アサート成功: BigInt("8").gcd(BigInt("8")) == BigInt("8")
アサート成功: BigInt("8").gcd(BigInt("-12")) == BigInt("4")
アサート成功: BigInt("-8").gcd(BigInt("12")) == BigInt("-4")
アサート成功: BigInt("-8").gcd(BigInt("-12")) == BigInt("-4")
アサート成功: BigInt("5").inverse(BigInt("13")) == BigInt("8")
アサート成功: BigInt("43").inverse(BigInt("31")) == BigInt("13")
アサート成功: BigInt("5").inverse(BigInt("-13")) == BigInt("-5")
アサート成功: BigInt("43").inverse(BigInt("-31")) == BigInt("-18")
アサート成功: BigInt("18").lcm(BigInt("24")) == BigInt("72")
アサート成功: BigInt("18").lcm(BigInt("27")) == BigInt("54")
0.360 secs