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