fork download
  1. #include <algorithm>
  2. #include <array>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11.  
  12. using namespace std;
  13.  
  14. class RPG;
  15. class SimpleRPG;
  16. class RBG;
  17. class SimpleRBG;
  18. class HA;
  19. class SHA1;
  20. class Sieve;
  21. class PT;
  22. class MRBPT;
  23. class BigMath;
  24. class BigInt;
  25. class BitField;
  26. class BitInputStream;
  27. template <class X> class PrimitiveBitInputStream;
  28. template <class X> class Pool;
  29. template <class X> class ExArray;
  30. template <class X> class PrimitiveExArray;
  31. template <class X> class ClassObjectExArray;
  32.  
  33. class RPG {
  34. public:
  35. virtual BigInt generatePrime(const unsigned int& len) = 0;
  36. protected:
  37. static bool findSmallPrimeFactor(const BigInt& num);
  38. };
  39.  
  40. class SimpleRPG : public RPG {
  41. public:
  42. SimpleRPG();
  43. virtual BigInt generatePrime(const unsigned int& len);
  44. protected:
  45. shared_ptr<RBG> rbg;
  46. shared_ptr<PT> pt;
  47. SimpleRPG(const SimpleRPG& simple_rpg);
  48. SimpleRPG& operator =(const SimpleRPG& simple_rpg);
  49. };
  50.  
  51. class RBG {
  52. public:
  53. virtual bool generateBit() = 0;
  54. };
  55.  
  56. class SimpleRBG : public RBG {
  57. public:
  58. SimpleRBG();
  59. virtual bool generateBit();
  60. protected:
  61. shared_ptr<BitField> hash;
  62. unsigned int pos;
  63. SimpleRBG(const SimpleRBG& simple_rbg);
  64. SimpleRBG& operator =(const SimpleRBG& simple_rbg);
  65. };
  66.  
  67. class HA {
  68. public:
  69. virtual shared_ptr<BitField> computeHash(const shared_ptr<BitInputStream>& msg) = 0;
  70. };
  71.  
  72. class SHA1 : public HA {
  73. public:
  74. SHA1();
  75. virtual shared_ptr<BitField> computeHash(const shared_ptr<BitInputStream>& msg);
  76. protected:
  77. typedef unsigned long (*FUNCTION)(const unsigned long& x, const unsigned long& y, const unsigned long& z);
  78. SHA1(const SHA1& sha1);
  79. SHA1& operator =(const SHA1& sha1);
  80. static const FUNCTION FUNCTIONS[4];
  81. static const unsigned long K[4];
  82. static const unsigned long INITIAL_HASH_VALUES[5];
  83. static unsigned long ch(const unsigned long& x, const unsigned long& y, const unsigned long& z);
  84. static unsigned long parity(const unsigned long& x, const unsigned long& y, const unsigned long& z);
  85. static unsigned long maj(const unsigned long& x, const unsigned long& y, const unsigned long& z);
  86. };
  87.  
  88. class Sieve {
  89. public:
  90. unsigned int getNextPrime(const unsigned int& prev_num);
  91. static Sieve instance;
  92. protected:
  93. shared_ptr<PrimitiveExArray<bool> > tab;
  94. Sieve();
  95. };
  96.  
  97. class PT {
  98. public:
  99. virtual bool isPrime(const BigInt& p) = 0;
  100. };
  101.  
  102. class MRBPT : public PT {
  103. public:
  104. MRBPT();
  105. virtual bool isPrime(const BigInt& p);
  106. protected:
  107. MRBPT(const MRBPT& mrbpt);
  108. MRBPT& operator =(const MRBPT& mrbpt);
  109. };
  110.  
  111. class BigMath {
  112. public:
  113. static BigInt power(const BigInt& base, const BigInt& exp);
  114. static BigInt power(const BigInt& base, const BigInt& exp, const BigInt& nrm);
  115. static BigInt gcd(const BigInt& a, const BigInt& b);
  116. static BigInt lcm(const BigInt& a, const BigInt& b);
  117. static BigInt inverse(const BigInt& num, const BigInt& nrm);
  118. protected:
  119. BigMath();
  120. };
  121.  
  122. template <class X>
  123. class ExArray {
  124. public:
  125. virtual X& operator [](const unsigned int& i) = 0;
  126. virtual X& at(const unsigned int& i) = 0;
  127. virtual X& last() = 0;
  128. virtual unsigned int getSize() const = 0;
  129. virtual bool isEmpty() const = 0;
  130. virtual void push(const X& x) = 0;
  131. virtual void pop() = 0;
  132. virtual void clear() = 0;
  133. virtual void resize(const unsigned int& sz) = 0;
  134. };
  135.  
  136. template <class X>
  137. class PrimitiveExArray : public ExArray<X> {
  138. public:
  139. PrimitiveExArray(const unsigned int& sz = 0);
  140. PrimitiveExArray(const PrimitiveExArray& a);
  141. virtual ~PrimitiveExArray();
  142. PrimitiveExArray& operator =(const PrimitiveExArray& a);
  143. virtual X& operator [](const unsigned int& i);
  144. virtual X& at(const unsigned int& i);
  145. virtual X& last();
  146. virtual unsigned int getSize() const;
  147. virtual bool isEmpty() const;
  148. virtual void push(const X& x);
  149. virtual void pop();
  150. virtual void clear();
  151. virtual void resize(const unsigned int& sz);
  152. protected:
  153. X* buf;
  154. unsigned int buf_sz;
  155. unsigned int sz;
  156. };
  157.  
  158. template <class X>
  159. class ClassObjectExArray : public ExArray<X> {
  160. public:
  161. ClassObjectExArray(const unsigned int& sz = 0);
  162. ClassObjectExArray(const ClassObjectExArray& a);
  163. virtual ~ClassObjectExArray();
  164. ClassObjectExArray& operator =(const ClassObjectExArray& a);
  165. virtual X& operator [](const unsigned int& i);
  166. virtual X& at(const unsigned int& i);
  167. virtual X& last();
  168. virtual unsigned int getSize() const;
  169. virtual bool isEmpty() const;
  170. virtual void push(const X& x);
  171. virtual void pop();
  172. virtual void clear();
  173. virtual void resize(const unsigned int& sz);
  174. protected:
  175. X* buf;
  176. unsigned int buf_sz;
  177. unsigned int sz;
  178. };
  179.  
  180. template <class X>
  181. class Pool {
  182. public:
  183. class Ptr {
  184. public:
  185. Ptr(Pool* pool);
  186. Ptr(const Ptr& ptr);
  187. virtual ~Ptr();
  188. Ptr& operator =(const Ptr& ptr);
  189. X& operator *() const;
  190. X* operator ->() const;
  191. X* get() const;
  192. protected:
  193. Pool* pool;
  194. pair<X*, unsigned int*> x_cnt;
  195. void release();
  196. };
  197. virtual ~Pool();
  198. protected:
  199. PrimitiveExArray<pair<X*, unsigned int*> > xa;
  200. pair<X*, unsigned int*> get();
  201. void putBack(const pair<X*, unsigned int*>& x_cnt);
  202. friend Ptr;
  203. };
  204.  
  205. class BigInt {
  206. public:
  207. BigInt();
  208. BigInt(const int& num);
  209. BigInt(const string& str, const unsigned int& base = 10);
  210. BigInt(const int& sign, const BitField& fld);
  211. BigInt(const BigInt& num);
  212. BigInt operator -() const;
  213. BigInt operator +(const BigInt& ade) const;
  214. BigInt operator -(const BigInt& sub) const;
  215. BigInt operator *(const BigInt& mer) const;
  216. BigInt operator /(const BigInt& dsr) const;
  217. BigInt operator %(const BigInt& nrm) const;
  218. BigInt operator <<(const size_t& num) const;
  219. BigInt operator >>(const size_t& num) const;
  220. BigInt& operator =(const BigInt& num);
  221. BigInt& operator +=(const BigInt& ade);
  222. BigInt& operator -=(const BigInt& sub);
  223. BigInt& operator *=(const BigInt& mer);
  224. BigInt& operator /=(const BigInt& dsr);
  225. BigInt& operator %=(const BigInt& nrm);
  226. BigInt& operator <<=(const unsigned int& num);
  227. BigInt& operator >>=(const unsigned int& num);
  228. BigInt& operator ++();
  229. BigInt operator ++(int);
  230. BigInt& operator --();
  231. BigInt operator --(int);
  232. bool operator ==(const BigInt& num) const;
  233. bool operator <(const BigInt& num) const;
  234. bool operator >(const BigInt& num) const;
  235. bool operator !=(const BigInt& num) const;
  236. bool operator <=(const BigInt& num) const;
  237. bool operator >=(const BigInt& num) const;
  238. BigInt abs() const;
  239. bool isZero() const;
  240. int getSign() const;
  241. bool getLSBit() const;
  242. unsigned long getLSLong() const;
  243. void setInt(const int& num);
  244. string toString(const unsigned int& base = 10) const;
  245. unsigned int getLength() const;
  246. protected:
  247. class BFPtr : public Pool<BitField>::Ptr {
  248. public:
  249. BFPtr();
  250. BFPtr(const unsigned long& num);
  251. BFPtr(const BitField& fld);
  252. protected:
  253. static Pool<BitField> pool;
  254. };
  255. int sign;
  256. BFPtr fld;
  257. BigInt(const int& sign, const BFPtr& fld);
  258. static const unsigned int LONG_LEN;
  259. static BFPtr add(const BFPtr& age, const BFPtr& ade);
  260. static BFPtr subtract(const BFPtr& min, const BFPtr& sub);
  261. static BFPtr multiply(const BFPtr& mca, const BFPtr& mer);
  262. static pair<BFPtr, BFPtr> divide(const BFPtr& dde, const BFPtr& dsr);
  263. static unsigned int digitToNumber(const char& dig);
  264. static char numberToDigit(const unsigned int& num);
  265. };
  266.  
  267. class BitField {
  268. public:
  269. class LongInputStream {
  270. public:
  271. LongInputStream(const BitField* fld);
  272. bool isEOF() const;
  273. unsigned long getLong();
  274. protected:
  275. const BitField* fld;
  276. unsigned int end;
  277. unsigned int cur;
  278. unsigned int low_len;
  279. unsigned int high_len;
  280. unsigned long low_mask;
  281. unsigned long high_mask;
  282. unsigned long rem;
  283. unsigned long num;
  284. };
  285. class LongOutputStream {
  286. public:
  287. LongOutputStream(BitField* fld);
  288. virtual ~LongOutputStream();
  289. void putLong(const unsigned long& num);
  290. void flush();
  291. protected:
  292. BitField* fld;
  293. unsigned int cur;
  294. unsigned int low_len;
  295. unsigned int high_len;
  296. unsigned long low_mask;
  297. unsigned long high_mask;
  298. unsigned long high_buf;
  299. bool bufed;
  300. };
  301. class ReverseLongInputStream {
  302. public:
  303. ReverseLongInputStream(const BitField* fld);
  304. bool isEOF() const;
  305. unsigned long getLong();
  306. protected:
  307. const BitField* fld;
  308. unsigned int end;
  309. unsigned int cur;
  310. unsigned int low_len;
  311. unsigned int high_len;
  312. unsigned long low_mask;
  313. unsigned long high_mask;
  314. unsigned long rem;
  315. unsigned long num;
  316. };
  317. class ReverseLongOutputStream {
  318. public:
  319. ReverseLongOutputStream(BitField* fld);
  320. virtual ~ReverseLongOutputStream();
  321. void putLong(const unsigned long& num);
  322. void flush();
  323. protected:
  324. BitField* fld;
  325. unsigned int low_len;
  326. unsigned int high_len;
  327. unsigned long low_mask;
  328. unsigned long high_mask;
  329. unsigned long low_buf;
  330. bool bufed;
  331. };
  332. BitField();
  333. BitField(const unsigned long& num);
  334. BitField(const BitField& fld);
  335. virtual ~BitField();
  336. BitField& operator =(const BitField& fld);
  337. unsigned int getLength() const;
  338. bool isEmpty() const;
  339. bool isZero() const;
  340. bool getBit(const unsigned int& pos) const;
  341. bool getLSBit() const;
  342. bool getMSBit() const;
  343. unsigned long getLong(const unsigned int& ls_pos) const;
  344. unsigned long getLSLong() const;
  345. void setBit(const unsigned int& pos, const bool& bit);
  346. void pushMS(const bool& bit);
  347. template <class X> void pushMS(const X& x);
  348. void pushLS(const bool& bit);
  349. template <class X> void pushLS(const X& x);
  350. void setLong(const unsigned long& num);
  351. void shift(const int& num);
  352. void trim();
  353. void clear();
  354. int compare(const BitField& fld) const;
  355. string toString() const;
  356. bool isPowerOf2() const;
  357. BitField getSubfield(const unsigned int& ls_pos, const unsigned int& len) const;
  358. protected:
  359. unsigned char* buf;
  360. unsigned int buf_sz;
  361. int beg;
  362. unsigned int len;
  363. void extendIfNecessary(const int& len);
  364. friend LongInputStream;
  365. friend LongOutputStream;
  366. friend ReverseLongInputStream;
  367. friend ReverseLongOutputStream;
  368. };
  369.  
  370. class BitInputStream {
  371. public:
  372. virtual bool getBit() = 0;
  373. virtual bool isEOF() = 0;
  374. };
  375.  
  376. template <class X>
  377. class PrimitiveBitInputStream : public BitInputStream {
  378. public:
  379. PrimitiveBitInputStream(const X& x);
  380. virtual bool getBit();
  381. virtual bool isEOF();
  382. protected:
  383. X x;
  384. unsigned int pos;
  385. PrimitiveBitInputStream(const PrimitiveBitInputStream& sbin);
  386. PrimitiveBitInputStream& operator =(const PrimitiveBitInputStream& sbin);
  387. };
  388.  
  389. template <class X> X rotateLeft(const X& x, const unsigned int& n, const unsigned int& w = (sizeof(X) << 3));
  390.  
  391. bool RPG::findSmallPrimeFactor(const BigInt& num) {
  392. bool res = false;
  393. unsigned int end = (num.getLength() * num.getLength()) >> 6;
  394. for (unsigned int prime = 3; prime < end; prime = Sieve::instance.getNextPrime(prime)) {
  395. if ((num % prime).isZero()) {
  396. res = true;
  397. break;
  398. }
  399. }
  400. return res;
  401. }
  402.  
  403. SimpleRPG::SimpleRPG() : rbg(new SimpleRBG), pt(new MRBPT) {}
  404.  
  405. BigInt SimpleRPG::generatePrime(const unsigned int& len) {
  406. BigInt num;
  407. bool found = false;
  408. while (!found) {
  409. BitField fld;
  410. fld.pushMS(true);
  411. for (unsigned int i = 0; i < len - 2; i++)
  412. fld.pushMS(this->rbg->generateBit());
  413. fld.pushMS(true);
  414. for (num = BigInt(1, fld); num.getLength() == len; num += 2) {
  415. if (!findSmallPrimeFactor(num) && this->pt->isPrime(num)) {
  416. found = true;
  417. break;
  418. }
  419. }
  420. }
  421. return num;
  422. }
  423.  
  424. SimpleRBG::SimpleRBG() : pos(160) {
  425. unsigned long long t = time(nullptr);
  426. unsigned long long c = clock();
  427. srand((t >> 32) ^ (t & 0xffffffff) ^ (c >> 32) ^ (c & 0xffffffff));
  428. }
  429.  
  430. bool SimpleRBG::generateBit() {
  431. if (this->pos >= 160) {
  432. shared_ptr<HA> ha(new SHA1);
  433. this->hash = ha->computeHash(shared_ptr<BitInputStream>(new PrimitiveBitInputStream<int>(rand())));
  434. this->pos = 0;
  435. }
  436. bool res = this->hash->getBit(32 * (this->pos >> 5) + (32 - 1 - (this->pos & 0x1F)));
  437. this->pos++;
  438. return res;
  439. }
  440.  
  441. const SHA1::FUNCTION SHA1::FUNCTIONS[4] = {
  442. ch,
  443. parity,
  444. maj,
  445. parity,
  446. };
  447.  
  448. const unsigned long SHA1::K[4] = {
  449. 0x5a827999,
  450. 0x6ed9eba1,
  451. 0x8f1bbcdc,
  452. 0xca62c1d6,
  453. };
  454.  
  455. const unsigned long SHA1::INITIAL_HASH_VALUES[5] = {
  456. 0x67452301,
  457. 0xefcdab89,
  458. 0x98badcfe,
  459. 0x10325476,
  460. 0xc3d2e1f0,
  461. };
  462.  
  463. SHA1::SHA1() {}
  464.  
  465. shared_ptr<BitField> SHA1::computeHash(const shared_ptr<BitInputStream>& msg) {
  466. shared_ptr<array<unsigned long, 5> > hash(new array<unsigned long, 5>);
  467. copy(INITIAL_HASH_VALUES, INITIAL_HASH_VALUES + 5, &hash->front());
  468. shared_ptr<array<unsigned long, 16> > block(new array<unsigned long, 16>);
  469. shared_ptr<array<unsigned long, 80> > sche(new array<unsigned long, 80>);
  470. unsigned long long len = 0;
  471. bool put_end = false;
  472. bool put_len = false;
  473. while (!put_len) {
  474. block->fill(0);
  475. unsigned int block_pos = 0;
  476. for (; block_pos < 512 && !msg->isEOF(); block_pos++) {
  477. block->at(block_pos >> 5) |= (msg->getBit() == true ? 1 : 0) << (32 - 1 - (block_pos & 0x1f));
  478. len++;
  479. }
  480. if (block_pos < 512) {
  481. if (!put_end) {
  482. block->at(block_pos >> 5) |= 1 << (32 - 1 - (block_pos & 0x1f));
  483. block_pos++;
  484. put_end = true;
  485. }
  486. if (block_pos + 64 <= 512) {
  487. block->at(14) = len >> 32;
  488. block->at(15) = len & 0xffffffff;
  489. put_len = true;
  490. }
  491. }
  492. for (unsigned int i = 0; i < 80; i++) {
  493. if (i < 16) sche->at(i) = block->at(i);
  494. else sche->at(i) = rotateLeft(
  495. sche->at(i - 3) ^
  496. sche->at(i - 8) ^
  497. sche->at(i - 14) ^
  498. sche->at(i - 16), 1);
  499. }
  500. unsigned long a = hash->at(0);
  501. unsigned long b = hash->at(1);
  502. unsigned long c = hash->at(2);
  503. unsigned long d = hash->at(3);
  504. unsigned long e = hash->at(4);
  505. for (unsigned int i = 0; i < 80; i++) {
  506. unsigned long t =
  507. rotateLeft(a, 5) +
  508. FUNCTIONS[i / 20](b, c, d) +
  509. e +
  510. K[i / 20] +
  511. sche->at(i);
  512. e = d;
  513. d = c;
  514. c = rotateLeft(b, 30);
  515. b = a;
  516. a = t;
  517. }
  518. hash->at(0) += a;
  519. hash->at(1) += b;
  520. hash->at(2) += c;
  521. hash->at(3) += d;
  522. hash->at(4) += e;
  523. }
  524. shared_ptr<BitField> res(new BitField);
  525. for (unsigned int i = 0; i < 5; i++) res->pushMS(hash->at(i));
  526. return res;
  527. }
  528.  
  529. unsigned long SHA1::ch(const unsigned long& x, const unsigned long& y, const unsigned long& z) {
  530. return (x & y) ^ (~x & z);
  531. }
  532.  
  533. unsigned long SHA1::parity(const unsigned long& x, const unsigned long& y, const unsigned long& z) {
  534. return x ^ y ^ z;
  535. }
  536.  
  537. unsigned long SHA1::maj(const unsigned long& x, const unsigned long& y, const unsigned long& z) {
  538. return (x & y) ^ (x & z) ^ (y & z);
  539. }
  540.  
  541. unsigned int Sieve::getNextPrime(const unsigned int& prev_num) {
  542. unsigned int res;
  543. for (;;) {
  544. {
  545. unsigned int i = prev_num + 1;
  546. for (; i < this->tab->getSize(); i++) {
  547. if (this->tab->at(i) == true) {
  548. res = i;
  549. break;
  550. }
  551. }
  552. if (i < this->tab->getSize()) break;
  553. }
  554. unsigned int new_sz = this->tab->getSize() << 1;
  555. if (new_sz == 0) new_sz = 1;
  556. for (unsigned int i = this->tab->getSize(); i < new_sz; i++)
  557. this->tab->push(true);
  558. for (unsigned int i = 0; i < this->tab->getSize(); i++) {
  559. if (this->tab->at(i) == true) {
  560. for (unsigned int j = i + i; j < this->tab->getSize(); j += i)
  561. this->tab->at(j) = false;
  562. }
  563. }
  564. }
  565. return res;
  566. }
  567.  
  568. Sieve Sieve::instance;
  569.  
  570. Sieve::Sieve() : tab(new PrimitiveExArray<bool>) {
  571. this->tab->push(false);
  572. this->tab->push(false);
  573. }
  574.  
  575. MRBPT::MRBPT() {}
  576.  
  577. bool MRBPT::isPrime(const BigInt& p) {
  578. bool res = true;
  579. if (p == 1) res = false;
  580. else if (p == 2) res = true;
  581. else if (p.getLSBit() == false) res = false;
  582. else {
  583. BigInt o = p - 1;
  584. BigInt q = o;
  585. BigInt k(1);
  586. while (q.getLSBit() == false) {
  587. q >>= 1;
  588. k++;
  589. }
  590. BigInt cnt(3072 / p.getLength());
  591. if (cnt == 0) cnt = 1;
  592. BigInt a(2);
  593. BigInt gap(p / cnt);
  594. if (gap.isZero()) gap = 1;
  595. for (; a < p; a += gap) {
  596. BigInt b = BigMath::power(a, q, p);
  597. if (b != 1 && b != o) {
  598. BigInt i(1);
  599. for (; i < k; i++) {
  600. b = (b * b) % p;
  601. if (b == o) break;
  602. }
  603. if (i == k) {
  604. res = false;
  605. break;
  606. }
  607. }
  608. }
  609. }
  610. return res;
  611. }
  612.  
  613. BigInt BigMath::power(const BigInt& base, const BigInt& exp) {
  614. BigInt res;
  615. if (base.isZero()) res = BigInt(0);
  616. else if (exp.isZero()) res = BigInt(1);
  617. else if (exp < 0) res = BigInt(1) / power(base, -exp);
  618. else {
  619. res.setInt(1);
  620. BigInt coef = base;
  621. for (BigInt exp2 = exp;;) {
  622. if (exp2.getLSBit()) res *= coef;
  623. exp2 >>= 1;
  624. if (exp2.isZero()) break;
  625. coef *= coef;
  626. }
  627. }
  628. return res;
  629. }
  630.  
  631. BigInt BigMath::power(const BigInt& base, const BigInt& exp, const BigInt& nrm) {
  632. BigInt res;
  633. if (base.isZero()) res = BigInt(0);
  634. else if (exp.isZero()) res = BigInt(1);
  635. else if (exp < 0) res = (BigInt(1) / power(base, -exp)) % nrm;
  636. else {
  637. res.setInt(1);
  638. BigInt coef = base % nrm;
  639. for (BigInt exp2 = exp;;) {
  640. if (exp2.getLSBit()) res = (res * coef) % nrm;
  641. exp2 >>= 1;
  642. if (exp2.isZero()) break;
  643. coef = (coef * coef) % nrm;
  644. }
  645. }
  646. return res;
  647. }
  648.  
  649. BigInt BigMath::gcd(const BigInt& a, const BigInt& b) {
  650. BigInt c(a.abs()), d(b.abs()), r;
  651. if (c < d) swap(c, d);
  652. for (;;) {
  653. r = c % d;
  654. if (r == 0) break;
  655. c = d;
  656. d = r;
  657. };
  658. if (d.getSign() != a.getSign()) d = -d;
  659. return d;
  660. }
  661.  
  662. BigInt BigMath::lcm(const BigInt& a, const BigInt& b) {
  663. return a * b / gcd(a, b);
  664. }
  665.  
  666. BigInt BigMath::inverse(const BigInt& num, const BigInt& nrm) {
  667. BigInt a(num.abs()), b(nrm.abs()), r, q, c, d(1), e(0);
  668. if (a < b) {
  669. swap(a, b);
  670. swap(d, e);
  671. }
  672. for (;;) {
  673. q = a / b;
  674. r = a % b;
  675. if (r == 0) break;
  676. BigInt t = e * q;
  677. c = d - t;
  678. d = e;
  679. e = c;
  680. a = b;
  681. b = r;
  682. };
  683. if (c.getSign() != nrm.getSign()) c += nrm;
  684. return c;
  685. }
  686.  
  687. class MColPtr : public Pool<PrimitiveExArray<unsigned long> >::Ptr {
  688. public:
  689. MColPtr();
  690. protected:
  691. static Pool<PrimitiveExArray<unsigned long> > pool;
  692. };
  693.  
  694. class MTabPtr : public Pool<ClassObjectExArray<MColPtr> >::Ptr {
  695. public:
  696. MTabPtr();
  697. protected:
  698. static Pool<ClassObjectExArray<MColPtr> > pool;
  699. };
  700.  
  701. BigInt::BigInt() : sign(0) {}
  702.  
  703. BigInt::BigInt(const int& num) {
  704. this->sign = num == 0 ? 0 : (num > 0 ? 1 : -1);
  705. this->fld->setLong(num < 0 ? -num : num);
  706. }
  707.  
  708. BigInt::BigInt(const string& str, const unsigned int& base) {
  709. this->sign = 1;
  710. this->fld->setLong(0);
  711. BFPtr base_fld(base);
  712. auto iter = str.begin();
  713. if (*iter == '+') {
  714. this->sign = 1;
  715. iter++;
  716. }
  717. else if (*iter == '-') {
  718. this->sign = -1;
  719. iter++;
  720. }
  721. for (; iter != str.end(); iter++) {
  722. if (iter != str.begin()) {
  723. this->fld = multiply(this->fld, base_fld);
  724. }
  725. unsigned int num = digitToNumber(*iter);
  726. if (num >= base) throw string("値が範囲を逸脱している。");
  727. this->fld = add(this->fld, BFPtr(num));
  728. }
  729. if (this->fld->isZero()) this->sign = 0;
  730. }
  731.  
  732. BigInt::BigInt(const int& sign, const BitField& fld) {
  733. this->sign = fld.isZero() ? 0 : sign;
  734. *this->fld = fld;
  735. }
  736.  
  737. BigInt::BigInt(const BigInt& num) : sign(num.sign), fld(*num.fld) {}
  738.  
  739. BigInt BigInt::operator -() const {
  740. return BigInt(-this->sign, BFPtr(*this->fld));
  741. }
  742.  
  743. BigInt BigInt::operator +(const BigInt& ade) const {
  744. BigInt res;
  745. if (this->sign == 0)
  746. res = ade;
  747. else if (ade.sign == 0)
  748. res = *this;
  749. else if (this->sign == ade.sign)
  750. res = BigInt(this->sign, add(this->fld, ade.fld));
  751. else {
  752. int cmp_res = this->fld->compare(*ade.fld);
  753. if (cmp_res == 0) res.setInt(0);
  754. else {
  755. int sign;
  756. BFPtr gre_fld, less_fld;
  757. if (cmp_res > 0) {
  758. sign = this->sign;
  759. gre_fld = this->fld;
  760. less_fld = ade.fld;
  761. }
  762. else {
  763. sign = ade.sign;
  764. gre_fld = ade.fld;
  765. less_fld = this->fld;
  766. }
  767. res = BigInt(sign, subtract(gre_fld, less_fld));
  768. }
  769. }
  770. return res;
  771. }
  772.  
  773. BigInt BigInt::operator -(const BigInt& sub) const {
  774. return operator +(-sub);
  775. }
  776.  
  777. BigInt BigInt::operator *(const BigInt& mer) const {
  778. return BigInt(this->sign * mer.sign, multiply(this->fld, mer.fld));
  779. }
  780.  
  781. BigInt BigInt::operator /(const BigInt& dsr) const {
  782. return BigInt(this->sign * dsr.sign, divide(this->fld, dsr.fld).first);
  783. }
  784.  
  785. BigInt BigInt::operator %(const BigInt& nrm) const {
  786. return BigInt(this->sign, divide(this->fld, nrm.fld).second);
  787. }
  788.  
  789. BigInt BigInt::operator <<(const unsigned int& num) const {
  790. BFPtr fld(*this->fld);
  791. fld->shift(-num);
  792. return BigInt(this->sign, fld);
  793. }
  794.  
  795. BigInt BigInt::operator >>(const unsigned int& num) const {
  796. BFPtr fld(*this->fld);
  797. fld->shift(num);
  798. return BigInt(this->sign, fld);
  799. }
  800.  
  801. BigInt& BigInt::operator =(const BigInt& num) {
  802. if (&num != this) {
  803. this->sign = num.sign;
  804. *this->fld = *num.fld;
  805. }
  806. return *this;
  807. }
  808.  
  809. BigInt& BigInt::operator +=(const BigInt& add) {
  810. return *this = *this + add;
  811. }
  812.  
  813. BigInt& BigInt::operator -=(const BigInt& sub) {
  814. return *this = *this - sub;
  815. }
  816.  
  817. BigInt& BigInt::operator *=(const BigInt& mer) {
  818. return *this = *this * mer;
  819. }
  820.  
  821. BigInt& BigInt::operator /=(const BigInt& dsr) {
  822. return *this = *this / dsr;
  823. }
  824.  
  825. BigInt& BigInt::operator %=(const BigInt& nrm) {
  826. return *this = *this % nrm;
  827. }
  828.  
  829. BigInt& BigInt::operator <<=(const size_t& num) {
  830. return *this = *this << num;
  831. }
  832.  
  833. BigInt& BigInt::operator >>=(const size_t& num) {
  834. return *this = *this >> num;
  835. }
  836.  
  837. BigInt& BigInt::operator ++() {
  838. return *this += 1;
  839. }
  840.  
  841. BigInt BigInt::operator ++(int) {
  842. BigInt res = *this;
  843. *this += 1;
  844. return res;
  845. }
  846.  
  847. BigInt& BigInt::operator --() {
  848. return *this -= 1;
  849. }
  850.  
  851. BigInt BigInt::operator --(int) {
  852. BigInt res = *this;
  853. *this -= 1;
  854. return res;
  855. }
  856.  
  857. bool BigInt::operator ==(const BigInt& num) const {
  858. if (&num == this) return true;
  859. return this->sign == num.sign && this->fld->compare(*num.fld) == 0;
  860. }
  861.  
  862. bool BigInt::operator <(const BigInt& num) const {
  863. if (&num == this) return false;
  864. int cmp_res = this->fld->compare(*num.fld);
  865. return (this->sign >= 0 && num.sign >= 0 && cmp_res < 0) ||
  866. (this->sign < 0 && num.sign >= 0) ||
  867. (this->sign < 0 && num.sign < 0 && cmp_res > 0);
  868. }
  869.  
  870. bool BigInt::operator >(const BigInt& num) const {
  871. if (&num == this) return false;
  872. int cmp_res = this->fld->compare(*num.fld);
  873. return (this->sign >= 0 && num.sign >= 0 && cmp_res > 0) ||
  874. (this->sign >= 0 && num.sign < 0) ||
  875. (this->sign < 0 && num.sign < 0 && cmp_res < 0);
  876. }
  877.  
  878. bool BigInt::operator !=(const BigInt& num) const {
  879. return !operator ==(num);
  880. }
  881.  
  882. bool BigInt::operator <=(const BigInt& num) const {
  883. return !operator >(num);
  884. }
  885.  
  886. bool BigInt::operator >=(const BigInt& num) const {
  887. return !operator <(num);
  888. }
  889.  
  890. BigInt BigInt::abs() const {
  891. return BigInt(1, BFPtr(*this->fld));
  892. }
  893.  
  894. bool BigInt::isZero() const {
  895. return this->fld->isZero();
  896. }
  897.  
  898. int BigInt::getSign() const {
  899. return this->sign;
  900. }
  901.  
  902. bool BigInt::getLSBit() const {
  903. return this->fld->getLSBit();
  904. }
  905.  
  906. unsigned long BigInt::getLSLong() const {
  907. return this->fld->getLSLong();
  908. }
  909.  
  910. void BigInt::setInt(const int& num) {
  911. this->sign = num == 0 ? 0 : (num > 0 ? 1 : -1);
  912. this->fld->setLong(num < 0 ? -num : num);
  913. }
  914.  
  915. string BigInt::toString(const unsigned int& base) const {
  916. string res = "";
  917. pair<BFPtr, BFPtr> quo_rem(BFPtr(*this->fld), BFPtr());
  918. BFPtr base_fld;
  919. base_fld->setLong(base);
  920. while (!quo_rem.first->isZero()) {
  921. quo_rem = divide(quo_rem.first, base_fld);
  922. res += numberToDigit(quo_rem.second->getLSLong());
  923. }
  924. if (res.empty()) res += '0';
  925. if (this->sign < 0) res += '-';
  926. reverse(res.begin(), res.end());
  927. size_t pos = 0;
  928. for (; pos < res.length() - 1; pos++) if (res[pos] != '0') break;
  929. return res.substr(pos, res.length() - pos);
  930. }
  931.  
  932. unsigned int BigInt::getLength() const {
  933. return this->fld->getLength();
  934. }
  935.  
  936. BigInt::BFPtr::BFPtr() : Pool<BitField>::Ptr(&pool) {
  937. this->x_cnt.first->clear();
  938. }
  939.  
  940. BigInt::BFPtr::BFPtr(const unsigned long& num) : Pool<BitField>::Ptr(&pool) {
  941. this->x_cnt.first->setLong(num);
  942. }
  943.  
  944. BigInt::BFPtr::BFPtr(const BitField& fld) : Pool<BitField>::Ptr(&pool) {
  945. *this->x_cnt.first = fld;
  946. }
  947.  
  948. Pool<BitField> BigInt::BFPtr::pool;
  949.  
  950. BigInt::BigInt(const int& sign, const BFPtr& fld) {
  951. this->sign = fld->isZero() ? 0 : sign;
  952. this->fld = fld;
  953. }
  954.  
  955. const unsigned int BigInt::LONG_LEN = sizeof(unsigned long) << 3;
  956.  
  957. BigInt::BFPtr BigInt::add(const BFPtr& age, const BFPtr& ade) {
  958. BFPtr sum;
  959. BitField::LongOutputStream sum_out(sum.get());
  960. BitField::LongInputStream age_in(age.get());
  961. BitField::LongInputStream ade_in(ade.get());
  962. bool carry = false;
  963. while (!age_in.isEOF() || !ade_in.isEOF()) {
  964. unsigned long age_num = !age_in.isEOF() ? age_in.getLong() : 0;
  965. unsigned long ade_num = !ade_in.isEOF() ? ade_in.getLong() : 0;
  966. unsigned long sum_num = (age_num & ((1 << (LONG_LEN - 1)) - 1)) + (ade_num & ((1 << (LONG_LEN - 1)) - 1)) + (carry ? 1 : 0);
  967. int ms_num = (age_num >> (LONG_LEN - 1)) + (ade_num >> (LONG_LEN - 1)) + (sum_num >> (LONG_LEN - 1));
  968. carry = ms_num > 1;
  969. if (carry) ms_num -= 2;
  970. sum_num = (sum_num & ((1 << (LONG_LEN - 1)) - 1)) | (ms_num << (LONG_LEN - 1));
  971. sum_out.putLong(sum_num);
  972. }
  973. sum_out.flush();
  974. if (carry) sum->pushMS(true);
  975. if (sum->isEmpty()) sum->pushMS(false);
  976. else sum->trim();
  977. return sum;
  978. }
  979.  
  980. BigInt::BFPtr BigInt::subtract(const BFPtr& min, const BFPtr& sub) {
  981. BFPtr dif;
  982. BitField::LongOutputStream dif_out(dif.get());
  983. BitField::LongInputStream min_in(min.get());
  984. BitField::LongInputStream sub_in(sub.get());
  985. bool borrow = false;
  986. while (!min_in.isEOF() || !sub_in.isEOF()) {
  987. unsigned int min_num = !min_in.isEOF() ? min_in.getLong() : 0;
  988. unsigned int sub_num = !sub_in.isEOF() ? sub_in.getLong() : 0;
  989. unsigned int dif_num = (min_num | (1 << (LONG_LEN - 1))) - (sub_num & ((1 << (LONG_LEN - 1)) - 1)) - (borrow ? 1 : 0);
  990. int ms_num = (min_num >> (LONG_LEN - 1)) - (sub_num >> (LONG_LEN - 1)) - (1 - (dif_num >> (LONG_LEN - 1)));
  991. borrow = ms_num < 0;
  992. if (borrow) ms_num += 2;
  993. dif_num = (dif_num & ((1 << (LONG_LEN - 1)) - 1)) | (ms_num << (LONG_LEN - 1));
  994. dif_out.putLong(dif_num);
  995. }
  996. dif_out.flush();
  997. if (borrow) throw string("被減数が減数より小さい。");
  998. if (dif->isEmpty()) dif->pushMS(false);
  999. else dif->trim();
  1000. return dif;
  1001. }
  1002.  
  1003. BigInt::BFPtr BigInt::multiply(const BFPtr& mca, const BFPtr& mer) {
  1004. BFPtr pro;
  1005. if (mca->isPowerOf2()) {
  1006. *pro = *mer;
  1007. pro->shift(-(mca->getLength() - 1));
  1008. }
  1009. else if (mer->isPowerOf2()) {
  1010. *pro = *mca;
  1011. pro->shift(-(mer->getLength() - 1));
  1012. }
  1013. else {
  1014. BitField::LongOutputStream pro_out(pro.get());
  1015. BitField::LongInputStream mer_in(mer.get());
  1016. MTabPtr tab;
  1017. for (unsigned int base_col = 0; !mer_in.isEOF(); base_col++) {
  1018. unsigned long mer_num = mer_in.getLong();
  1019. BitField::LongInputStream mca_in(mca.get());
  1020. for (unsigned int col = base_col; !mca_in.isEOF(); col++) {
  1021. unsigned long mca_num = mca_in.getLong();
  1022. unsigned long long pro_num = (unsigned long long)mer_num * mca_num;
  1023. unsigned long rem_num = pro_num & (((unsigned long long)1 << LONG_LEN) - 1);
  1024. unsigned long carry_num = pro_num >> LONG_LEN;
  1025. if (tab->getSize() <= col) tab->push(MColPtr());
  1026. tab->at(col)->push(rem_num);
  1027. if (carry_num != 0) {
  1028. if (tab->getSize() <= col + 1) tab->push(MColPtr());
  1029. tab->at(col + 1)->push(carry_num);
  1030. }
  1031. }
  1032. }
  1033. for (unsigned int col = 0; col < tab->getSize(); col++) {
  1034. unsigned long age_num = 0;
  1035. unsigned long carry_num = 0;
  1036. for (unsigned int i = 0; i < tab->at(col)->getSize(); i++) {
  1037. unsigned long ade_num = tab->at(col)->at(i);
  1038. unsigned long sum_num = (age_num & ((1 << (LONG_LEN - 1)) - 1)) + (ade_num & ((1 << (LONG_LEN - 1)) - 1));
  1039. int ms_num = (age_num >> (LONG_LEN - 1)) + (ade_num >> (LONG_LEN - 1)) + (sum_num >> (LONG_LEN - 1));
  1040. if (ms_num > 1) {
  1041. carry_num++;
  1042. ms_num -= 2;
  1043. }
  1044. age_num = (sum_num & ((1 << (LONG_LEN - 1)) - 1)) | (ms_num << (LONG_LEN - 1));
  1045. }
  1046. pro_out.putLong(age_num);
  1047. if (carry_num != 0) {
  1048. if (tab->getSize() <= col + 1) tab->push(MColPtr());
  1049. tab->at(col + 1)->push(carry_num);
  1050. }
  1051. }
  1052. pro_out.flush();
  1053. }
  1054. if (pro->isEmpty()) pro->pushMS(false);
  1055. else pro->trim();
  1056. return pro;
  1057. }
  1058.  
  1059. pair<BigInt::BFPtr, BigInt::BFPtr> BigInt::divide(const BFPtr& dde, const BFPtr& dsr) {
  1060. if (dsr->isZero()) throw string("除数がゼロ。");
  1061. pair<BFPtr, BFPtr> quo_rem;
  1062. int cmp_res = dde->compare(*dsr);
  1063. if (cmp_res == 0) {
  1064. quo_rem.first->setLong(1);
  1065. quo_rem.second->setLong(0);
  1066. }
  1067. else if (cmp_res < 0) {
  1068. quo_rem.first->setLong(0);
  1069. *quo_rem.second = *dde;
  1070. }
  1071. else if (dsr->isPowerOf2()) {
  1072. *quo_rem.first = *dde;
  1073. quo_rem.first->shift(dsr->getLength() - 1);
  1074. *quo_rem.second = dde->getSubfield(0, dsr->getLength() - 1);
  1075. }
  1076. else {
  1077. BitField::ReverseLongOutputStream quo_out(quo_rem.first.get());
  1078. unsigned int k = (32 - (dsr->getLength() & (32 - 1))) & (32 - 1);
  1079. BFPtr k_dde(*dde);
  1080. k_dde->shift(-k);
  1081. BFPtr k_dsr(*dsr);
  1082. k_dsr->shift(-k);
  1083. BitField::ReverseLongInputStream dde_in(k_dde.get());
  1084. BitField::ReverseLongInputStream dsr_in(k_dsr.get());
  1085. unsigned long dsr_ms_num = dsr_in.getLong();
  1086. while (!dde_in.isEOF()) {
  1087. BitField::ReverseLongOutputStream rem_out(quo_rem.second.get());
  1088. rem_out.putLong(dde_in.getLong());
  1089. rem_out.flush();
  1090. quo_rem.second->trim();
  1091. int cmp_res = quo_rem.second->compare(*k_dsr);
  1092. if (cmp_res == 0) {
  1093. quo_out.putLong(1);
  1094. quo_rem.second->clear();
  1095. }
  1096. else if (cmp_res < 0) quo_out.putLong(0);
  1097. else {
  1098. BitField::ReverseLongInputStream rem_in(quo_rem.second.get());
  1099. unsigned long long rem_ms_num = rem_in.getLong();
  1100. if (rem_ms_num < dsr_ms_num)
  1101. rem_ms_num = (rem_ms_num << 32) | rem_in.getLong();
  1102. unsigned long long quo_num = rem_ms_num / dsr_ms_num;
  1103. if (quo_num >= ((unsigned long long)1 << 32))
  1104. quo_num = ((unsigned long long)1 << 32) - 1;
  1105. BFPtr pro = multiply(k_dsr, BFPtr(quo_num));
  1106. while (quo_rem.second->compare(*pro) < 0) {
  1107. quo_num--;
  1108. quo_rem.second = add(quo_rem.second, k_dsr);
  1109. }
  1110. quo_out.putLong(quo_num);
  1111. quo_rem.second = subtract(quo_rem.second, pro);
  1112. }
  1113. }
  1114. quo_out.flush();
  1115. quo_rem.second->shift(k);
  1116. }
  1117. if (quo_rem.first->isEmpty()) quo_rem.first->pushMS(false);
  1118. else quo_rem.first->trim();
  1119. if (quo_rem.second->isEmpty()) quo_rem.second->pushMS(false);
  1120. else quo_rem.second->trim();
  1121. return quo_rem;
  1122. }
  1123.  
  1124. unsigned int BigInt::digitToNumber(const char& dig) {
  1125. unsigned int res;
  1126. if (dig >= '0' && dig <= '9') res = dig - '0';
  1127. else if (dig >= 'A' && dig <= 'Z') res = 10 + dig - 'A';
  1128. else if (dig >= 'a' && dig <= 'z') res = 10 + dig - 'a';
  1129. else throw string("文字が数ではない。");
  1130. return res;
  1131. }
  1132.  
  1133. char BigInt::numberToDigit(const unsigned int& num) {
  1134. char res;
  1135. if (num >= 0 && num <= 9) res = '0' + num;
  1136. else if (num >= 10 && num <= 36) res = 'a' + (num - 10);
  1137. else throw string("数が範囲を逸脱している。");
  1138. return res;
  1139. }
  1140.  
  1141. MColPtr::MColPtr() : Pool<PrimitiveExArray<unsigned long> >::Ptr(&pool) {
  1142. this->x_cnt.first->clear();
  1143. }
  1144.  
  1145. Pool<PrimitiveExArray<unsigned long> > MColPtr::pool;
  1146.  
  1147. MTabPtr::MTabPtr() : Pool<ClassObjectExArray<MColPtr> >::Ptr(&pool) {
  1148. this->x_cnt.first->clear();
  1149. }
  1150.  
  1151. Pool<ClassObjectExArray<MColPtr> > MTabPtr::pool;
  1152.  
  1153. BitField::LongInputStream::LongInputStream(const BitField* fld) {
  1154. this->fld = fld;
  1155. this->rem = this->fld->len;
  1156. if (this->rem != 0) {
  1157. this->high_len = this->fld->beg & (32 - 1);
  1158. this->low_len = 32 - this->high_len;
  1159. this->high_mask = (1 << this->high_len) - 1;
  1160. this->low_mask = ~this->high_mask;
  1161. this->end = this->fld->buf_sz >> 2;
  1162. this->cur = this->fld->beg >> 5;
  1163. this->num = ((unsigned long*)this->fld->buf)[this->cur];
  1164. }
  1165. }
  1166.  
  1167. bool BitField::LongInputStream::isEOF() const {
  1168. return this->rem == 0;
  1169. }
  1170.  
  1171. unsigned long BitField::LongInputStream::getLong() {
  1172. unsigned int res = 0;
  1173. res |= (this->num & this->low_mask) >> this->high_len;
  1174. if (this->rem >= this->low_len) {
  1175. this->rem -= this->low_len;
  1176. if (this->rem != 0) {
  1177. this->cur++;
  1178. if (this->cur == this->end) this->cur = 0;
  1179. this->num = ((unsigned long*)this->fld->buf)[this->cur];
  1180. if (this->high_len != 0) {
  1181. res |= (this->num & this->high_mask) << this->low_len;
  1182. if (this->rem >= this->high_len)
  1183. this->rem -= this->high_len;
  1184. else {
  1185. res &= (1 << (this->low_len + this->rem)) - 1;
  1186. this->rem = 0;
  1187. }
  1188. }
  1189. }
  1190. }
  1191. else {
  1192. res &= (1 << this->rem) - 1;
  1193. this->rem = 0;
  1194. }
  1195. return res;
  1196. }
  1197.  
  1198. BitField::LongOutputStream::LongOutputStream(BitField* fld) {
  1199. this->fld = fld;
  1200. unsigned int fld_end = this->fld->beg + this->fld->len;
  1201. unsigned int buf_len = this->fld->buf_sz << 3;
  1202. unsigned int act_fld_end;
  1203. if (fld_end <= buf_len) act_fld_end = fld_end;
  1204. else act_fld_end = fld_end - buf_len;
  1205. this->high_len = act_fld_end & (32 - 1);
  1206. this->low_len = 32 - this->high_len;
  1207. this->high_mask = (1 << this->high_len) - 1;
  1208. this->low_mask = ~this->high_mask;
  1209. this->cur = act_fld_end >> 5;
  1210. unsigned int end = this->fld->buf_sz >> 2;
  1211. if (this->fld->len != 0 && this->cur != end)
  1212. this->high_buf = ((unsigned long*)this->fld->buf)[this->cur] & this->high_mask;
  1213. else this->high_buf = 0;
  1214. this->bufed = false;
  1215. }
  1216.  
  1217. BitField::LongOutputStream::~LongOutputStream() {
  1218. flush();
  1219. }
  1220.  
  1221. void BitField::LongOutputStream::putLong(const unsigned long& num) {
  1222. this->fld->extendIfNecessary(32);
  1223. unsigned int end = this->fld->buf_sz >> 2;
  1224. if (this->cur == end) this->cur = 0;
  1225. ((unsigned long*)this->fld->buf)[this->cur] = this->high_buf | (num << this->high_len);
  1226. if (this->high_len != 0) {
  1227. this->high_buf = num >> this->low_len;
  1228. this->bufed = true;
  1229. }
  1230. this->cur++;
  1231. }
  1232.  
  1233. void BitField::LongOutputStream::flush() {
  1234. if (this->bufed) {
  1235. unsigned int fld_end = this->fld->beg + this->fld->len;
  1236. unsigned int buf_len = this->fld->buf_sz << 3;
  1237. unsigned int act_fld_end;
  1238. if (fld_end <= buf_len) act_fld_end = fld_end;
  1239. else act_fld_end = fld_end - buf_len;
  1240. unsigned int cur = act_fld_end >> 5;
  1241. if (cur == 0) cur = (this->fld->buf_sz >> 2) - 1;
  1242. else cur--;
  1243. ((unsigned long*)this->fld->buf)[cur] =
  1244. this->high_buf | (((unsigned long*)this->fld->buf)[cur] & this->low_mask);
  1245. this->bufed = false;
  1246. }
  1247. }
  1248.  
  1249. BitField::ReverseLongInputStream::ReverseLongInputStream(const BitField* fld) {
  1250. this->fld = fld;
  1251. this->rem = this->fld->len;
  1252. if (this->rem != 0) {
  1253. this->end = this->fld->buf_sz >> 2;
  1254. this->high_len = this->fld->beg & (32 - 1);
  1255. this->low_len = 32 - this->high_len;
  1256. this->high_mask = (1 << this->high_len) - 1;
  1257. this->low_mask = ~this->high_mask;
  1258. unsigned int fld_end = this->fld->beg + this->fld->len;
  1259. unsigned int buf_len = this->fld->buf_sz << 3;
  1260. unsigned int act_fld_end;
  1261. if (fld_end <= buf_len) act_fld_end = fld_end;
  1262. else act_fld_end = fld_end - buf_len;
  1263. unsigned int last_len = act_fld_end & (32 - 1);
  1264. if (last_len <= this->high_len) this->cur = act_fld_end >> 5;
  1265. else this->cur = (act_fld_end + (32 - 1)) >> 5;
  1266. if (this->cur != this->end)
  1267. this->num = ((unsigned long*)this->fld->buf)[this->cur];
  1268. }
  1269. }
  1270.  
  1271. bool BitField::ReverseLongInputStream::isEOF() const {
  1272. return this->rem == 0;
  1273. }
  1274.  
  1275. unsigned long BitField::ReverseLongInputStream::getLong() {
  1276. unsigned int res = 0;
  1277. unsigned int len = this->rem & (32 - 1);
  1278. if (len == 0) len = 32;
  1279. if (len > this->low_len) {
  1280. res |= this->num & this->high_mask;
  1281. unsigned int high_len = len - this->low_len;
  1282. if (high_len < this->high_len)
  1283. res &= (1 << high_len) - 1;
  1284. res <<= this->low_len;
  1285. }
  1286. if (this->cur == 0) this->cur = this->end;
  1287. this->cur--;
  1288. this->num = ((unsigned long*)this->fld->buf)[this->cur];
  1289. res |= (this->num & this->low_mask) >> this->high_len;
  1290. if (len < this->low_len) res &= (1 << len) - 1;
  1291. this->rem -= len;
  1292. return res;
  1293. }
  1294.  
  1295. BitField::ReverseLongOutputStream::ReverseLongOutputStream(BitField* fld) {
  1296. this->fld = fld;
  1297. this->high_len = this->fld->beg & (32 - 1);
  1298. this->low_len = 32 - this->high_len;
  1299. this->high_mask = (1 << this->high_len) - 1;
  1300. this->low_mask = ~this->high_mask;
  1301. unsigned int cur = this->fld->beg >> 5;
  1302. unsigned int end = this->fld->buf_sz >> 2;
  1303. if (this->fld->len != 0 && cur != end)
  1304. this->low_buf = ((unsigned long*)this->fld->buf)[cur] & this->low_mask;
  1305. else this->low_buf = 0;
  1306. this->bufed = false;
  1307. }
  1308.  
  1309. BitField::ReverseLongOutputStream::~ReverseLongOutputStream() {
  1310. flush();
  1311. }
  1312.  
  1313. void BitField::ReverseLongOutputStream::putLong(const unsigned long& num) {
  1314. this->fld->extendIfNecessary(-32);
  1315. unsigned int cur = (this->fld->beg >> 5) + 1;
  1316. unsigned int end = this->fld->buf_sz >> 2;
  1317. if (cur == end) cur = 0;
  1318. if (this->high_len != 0) {
  1319. ((unsigned long*)this->fld->buf)[cur] = (num >> this->low_len) | this->low_buf;
  1320. this->low_buf = num << this->high_len;
  1321. }
  1322. else {
  1323. ((unsigned long*)this->fld->buf)[cur] = this->low_buf;
  1324. this->low_buf = num;
  1325. }
  1326. this->bufed = true;
  1327. }
  1328.  
  1329. void BitField::ReverseLongOutputStream::flush() {
  1330. if (this->bufed) {
  1331. unsigned int cur = this->fld->beg >> 5;
  1332. ((unsigned long*)this->fld->buf)[cur] =
  1333. (((unsigned long*)this->fld->buf)[cur] & this->high_mask) | this->low_buf;
  1334. this->bufed = false;
  1335. }
  1336. }
  1337.  
  1338. BitField::BitField() : buf(nullptr), buf_sz(0), beg(0), len(0) {}
  1339.  
  1340. BitField::BitField(const unsigned long& num) : buf(nullptr), buf_sz(0), beg(0), len(0) {
  1341. extendIfNecessary(32);
  1342. *(unsigned long*)this->buf = num;
  1343. trim();
  1344. }
  1345.  
  1346. BitField::BitField(const BitField& fld) : buf(nullptr), buf_sz(0), beg(0), len(0) {
  1347. if (fld.len != 0) {
  1348. this->buf_sz = fld.buf_sz;
  1349. this->buf = new unsigned char[this->buf_sz];
  1350. memcpy(this->buf, fld.buf, fld.buf_sz);
  1351. this->beg = fld.beg;
  1352. this->len = fld.len;
  1353. }
  1354. }
  1355.  
  1356. BitField::~BitField() {
  1357. if (this->buf) delete[] this->buf;
  1358. }
  1359.  
  1360. BitField& BitField::operator =(const BitField& fld) {
  1361. if (&fld != this) {
  1362. this->len = 0;
  1363. if (fld.len != 0) {
  1364. this->buf_sz = fld.buf_sz;
  1365. this->buf = new unsigned char[this->buf_sz];
  1366. memcpy(this->buf, fld.buf, fld.buf_sz);
  1367. this->beg = fld.beg;
  1368. this->len = fld.len;
  1369. }
  1370. }
  1371. return *this;
  1372. }
  1373.  
  1374. unsigned int BitField::getLength() const {
  1375. return this->len;
  1376. }
  1377.  
  1378. bool BitField::isEmpty() const {
  1379. return this->len == 0;
  1380. }
  1381.  
  1382. bool BitField::isZero() const {
  1383. bool res = true;
  1384. for (unsigned int pos = 0; pos < this->len; pos++) {
  1385. if (getBit(pos) == true) {
  1386. res = false;
  1387. break;
  1388. }
  1389. }
  1390. return res;
  1391. }
  1392.  
  1393. bool BitField::getBit(const unsigned int& pos) const {
  1394. unsigned int abs_pos = this->beg + pos;
  1395. unsigned int buf_len = this->buf_sz << 3;
  1396. if (abs_pos >= buf_len) abs_pos -= buf_len;
  1397. return ((this->buf[abs_pos >> 3] >> (abs_pos & (8 - 1))) & 1) == 1;
  1398. }
  1399.  
  1400. bool BitField::getLSBit() const {
  1401. return getBit(0);
  1402. }
  1403.  
  1404. bool BitField::getMSBit() const {
  1405. return getBit(this->len - 1);
  1406. }
  1407.  
  1408. unsigned long BitField::getLong(const unsigned int& ls_pos) const {
  1409. unsigned long res = 0;
  1410. for (unsigned int pos = 0; pos < 32 && ls_pos + pos < this->len; pos++)
  1411. res |= (getBit(ls_pos + pos) == true ? 1 : 0) << pos;
  1412. return res;
  1413. }
  1414.  
  1415. unsigned long BitField::getLSLong() const {
  1416. unsigned long res = 0;
  1417. for (unsigned int pos = 0; pos < 32 && pos < this->len; pos++)
  1418. res |= (getBit(pos) == true ? 1 : 0) << pos;
  1419. return res;
  1420. }
  1421.  
  1422. void BitField::setBit(const unsigned int& pos, const bool& bit) {
  1423. unsigned int abs_pos = this->beg + pos;
  1424. unsigned int buf_len = this->buf_sz << 3;
  1425. if (abs_pos >= buf_len) abs_pos -= buf_len;
  1426. if (bit) this->buf[abs_pos >> 3] |= 1 << (abs_pos & (8 - 1));
  1427. else this->buf[abs_pos >> 3] &= ~(1 << (abs_pos & (8 - 1)));
  1428. }
  1429.  
  1430. void BitField::pushMS(const bool& bit) {
  1431. extendIfNecessary(1);
  1432. setBit(this->len - 1, bit);
  1433. }
  1434.  
  1435. template <class X> void BitField::pushMS(const X& x) {
  1436. for (unsigned int pos = 0; pos < (sizeof(X) << 3); pos++)
  1437. pushMS(((x >> pos) & 1) == 1);
  1438. }
  1439.  
  1440. void BitField::pushLS(const bool& bit) {
  1441. extendIfNecessary(-1);
  1442. setBit(0, bit);
  1443. }
  1444.  
  1445. template <class X> void BitField::pushLS(const X& x) {
  1446. for (unsigned int pos = 0; pos < (sizeof(X) << 3); pos++)
  1447. pushLS(((x >> ((sizeof(X) << 3) - 1 - pos)) & 1) == 1);
  1448. }
  1449.  
  1450. void BitField::setLong(const unsigned long& num) {
  1451. clear();
  1452. extendIfNecessary(32);
  1453. *(unsigned int*)this->buf = num;
  1454. trim();
  1455. }
  1456.  
  1457. void BitField::shift(const int& num) {
  1458. if (num < 0) {
  1459. extendIfNecessary(num);
  1460. for (unsigned int pos = 0; pos < -num; pos++)
  1461. setBit(pos, false);
  1462. }
  1463. else if (num > 0) {
  1464. unsigned int act_num;
  1465. if (num <= this->len) act_num = num;
  1466. else act_num = this->len;
  1467. this->beg += act_num;
  1468. unsigned int buf_len = this->buf_sz << 3;
  1469. if (this->beg >= buf_len) this->beg -= buf_len;
  1470. this->len -= act_num;
  1471. }
  1472. }
  1473.  
  1474. void BitField::trim() {
  1475. while (this->len > 1 && getMSBit() == false) this->len--;
  1476. }
  1477.  
  1478. void BitField::clear() {
  1479. this->beg = 0;
  1480. this->len = 0;
  1481. }
  1482.  
  1483. int BitField::compare(const BitField& fld) const {
  1484. int res = 0;
  1485. if (&fld != this) {
  1486. for (unsigned int next_pos = this->len < fld.len ? fld.len : this->len; next_pos > 0; next_pos--) {
  1487. unsigned int pos = next_pos - 1;
  1488. bool bit1 = pos < this->len ? getBit(pos) : false;
  1489. bool bit2 = pos < fld.len ? fld.getBit(pos) : false;
  1490. if (bit1 == false && bit2 == true) {
  1491. res = -1;
  1492. break;
  1493. }
  1494. else if (bit1 == true && bit2 == false) {
  1495. res = 1;
  1496. break;
  1497. }
  1498. }
  1499. }
  1500. return res;
  1501. }
  1502.  
  1503. string BitField::toString() const {
  1504. string res = "";
  1505. for (unsigned int pos = 0; pos < this->len; pos++)
  1506. res += getBit(pos) == true ? '1' : '0';
  1507. return res;
  1508. }
  1509.  
  1510. bool BitField::isPowerOf2() const {
  1511. unsigned int cnt = 0;
  1512. for (unsigned int pos = 0; pos < this->len; pos++) {
  1513. if (getBit(pos) == true) {
  1514. cnt++;
  1515. if (cnt > 1) break;
  1516. }
  1517. }
  1518. return cnt == 1;
  1519. }
  1520.  
  1521. BitField BitField::getSubfield(const unsigned int& ls_pos, const unsigned int& len) const {
  1522. BitField res;
  1523. for (unsigned int pos = 0; pos < len; pos++)
  1524. res.pushMS(getBit(ls_pos + pos));
  1525. return res;
  1526. }
  1527.  
  1528. void BitField::extendIfNecessary(const int& len) {
  1529. unsigned int abs_len = len < 0 ? -len : len;
  1530. unsigned int need_sz = ((((this->len + abs_len + (8 - 1)) >> 3) + (4 - 1)) >> 2) << 2;
  1531. if (need_sz > this->buf_sz) {
  1532. unsigned int new_buf_sz = need_sz << 1;
  1533. unsigned char* new_buf = new unsigned char[new_buf_sz];
  1534. unsigned int new_beg = 0;
  1535. if (this->len != 0) {
  1536. unsigned int fld_beg = this->beg >> 3;
  1537. unsigned int fld_end = (this->beg + this->len + (8 - 1)) >> 3;
  1538. if (fld_end <= this->buf_sz) {
  1539. unsigned int fld_sz = fld_end - fld_beg;
  1540. memcpy(new_buf + fld_beg, this->buf + fld_beg, fld_sz);
  1541. new_beg = this->beg;
  1542. }
  1543. else {
  1544. unsigned int low_sz = this->buf_sz - fld_beg;
  1545. unsigned int new_low_beg = new_buf_sz - low_sz;
  1546. memcpy(new_buf + new_low_beg, this->buf + fld_beg, low_sz);
  1547. unsigned int high_sz = fld_end - this->buf_sz;
  1548. memcpy(new_buf, this->buf, high_sz);
  1549. unsigned int buf_len = this->buf_sz << 3;
  1550. unsigned int low_len = buf_len - this->beg;
  1551. unsigned int new_buf_len = new_buf_sz << 3;
  1552. new_beg = new_buf_len - low_len;
  1553. }
  1554. delete[] this->buf;
  1555. }
  1556. this->buf_sz = new_buf_sz;
  1557. this->buf = new_buf;
  1558. this->beg = new_beg;
  1559. }
  1560. if (len < 0) {
  1561. this->beg += len;
  1562. int buf_len = this->buf_sz << 3;
  1563. if (this->beg < 0) this->beg += buf_len;
  1564. }
  1565. this->len += abs_len;
  1566. }
  1567.  
  1568. template <class X>
  1569. PrimitiveBitInputStream<X>::PrimitiveBitInputStream(const X& x) : x(x), pos(0) {}
  1570.  
  1571. template <class X>
  1572. bool PrimitiveBitInputStream<X>::getBit() {
  1573. bool res = ((this->x >> ((sizeof(X) << 3) - 1 - this->pos)) & 1) == 1;
  1574. this->pos++;
  1575. return res;
  1576. }
  1577.  
  1578. template <class X>
  1579. bool PrimitiveBitInputStream<X>::isEOF() {
  1580. return this->pos >= (sizeof(X) << 3);
  1581. }
  1582.  
  1583. template <class X>
  1584. Pool<X>::Ptr::Ptr(Pool* pool) : pool(pool) {
  1585. this->x_cnt = this->pool->get();
  1586. if (this->x_cnt.second) (*this->x_cnt.second)++;
  1587. }
  1588.  
  1589. template <class X>
  1590. Pool<X>::Ptr::Ptr(const Ptr& ptr) {
  1591. this->pool = ptr.pool;
  1592. this->x_cnt = ptr.x_cnt;
  1593. if (this->x_cnt.second) (*this->x_cnt.second)++;
  1594. }
  1595.  
  1596. template <class X>
  1597. Pool<X>::Ptr::~Ptr() {
  1598. release();
  1599. }
  1600.  
  1601. template <class X>
  1602. typename Pool<X>::Ptr& Pool<X>::Ptr::operator =(const Ptr& ptr) {
  1603. if (&ptr != this) {
  1604. release();
  1605. this->x_cnt = ptr.x_cnt;
  1606. if (this->x_cnt.second) (*this->x_cnt.second)++;
  1607. }
  1608. return *this;
  1609. }
  1610.  
  1611. template <class X>
  1612. X& Pool<X>::Ptr::operator *() const {
  1613. return *this->x_cnt.first;
  1614. }
  1615.  
  1616. template <class X>
  1617. X* Pool<X>::Ptr::operator ->() const {
  1618. return this->x_cnt.first;
  1619. }
  1620.  
  1621. template <class X>
  1622. X* Pool<X>::Ptr::get() const {
  1623. return this->x_cnt.first;
  1624. }
  1625.  
  1626. template <class X>
  1627. void Pool<X>::Ptr::release() {
  1628. if (this->x_cnt.second) {
  1629. (*this->x_cnt.second)--;
  1630. if (*this->x_cnt.second == 0)
  1631. pool->putBack(this->x_cnt);
  1632. }
  1633. }
  1634.  
  1635. template <class X>
  1636. Pool<X>::~Pool() {
  1637. for (unsigned int i = 0; i < this->xa.getSize(); i++) {
  1638. delete this->xa.at(i).first;
  1639. delete this->xa.at(i).second;
  1640. }
  1641. }
  1642.  
  1643. template <class X>
  1644. pair<X*, unsigned int*> Pool<X>::get() {
  1645. if (this->xa.isEmpty()) this->xa.push(make_pair(new X, new unsigned int(0)));
  1646. pair<X*, unsigned int*> res = this->xa.last();
  1647. this->xa.pop();
  1648. return res;
  1649. }
  1650.  
  1651. template <class X>
  1652. void Pool<X>::putBack(const pair<X*, unsigned int*>& x_cnt) {
  1653. this->xa.push(x_cnt);
  1654. }
  1655.  
  1656. template <class X>
  1657. PrimitiveExArray<X>::PrimitiveExArray(const unsigned int& sz) : buf(nullptr), buf_sz(0), sz(0) {
  1658. resize(sz);
  1659. }
  1660.  
  1661. template <class X>
  1662. PrimitiveExArray<X>::PrimitiveExArray(const PrimitiveExArray& a) : buf(nullptr), buf_sz(0), sz(0) {
  1663. if (a.sz != 0) {
  1664. if (this->buf) delete[] this->buf;
  1665. this->buf_sz = a.buf_sz;
  1666. this->buf = new X[this->buf_sz];
  1667. this->sz = a.sz;
  1668. memcpy(this->buf, a.buf, sizeof(X) * this->sz);
  1669. }
  1670. }
  1671.  
  1672. template <class X>
  1673. PrimitiveExArray<X>::~PrimitiveExArray() {
  1674. if (this->buf) delete[] this->buf;
  1675. }
  1676.  
  1677. template <class X>
  1678. PrimitiveExArray<X>& PrimitiveExArray<X>::operator =(const PrimitiveExArray& a) {
  1679. if (&a != this && a.sz != 0) {
  1680. if (this->buf) delete[] this->buf;
  1681. this->buf_sz = a.buf_sz;
  1682. this->buf = new X[this->buf_sz];
  1683. this->sz = a.sz;
  1684. memcpy(this->buf, a.buf, sizeof(X) * this->sz);
  1685. }
  1686. return *this;
  1687. }
  1688.  
  1689. template <class X>
  1690. X& PrimitiveExArray<X>::operator [](const unsigned int& i) {
  1691. return this->buf[i];
  1692. }
  1693.  
  1694. template <class X>
  1695. X& PrimitiveExArray<X>::at(const unsigned int& i) {
  1696. return this->buf[i];
  1697. }
  1698.  
  1699. template <class X>
  1700. X& PrimitiveExArray<X>::last() {
  1701. return this->buf[this->sz - 1];
  1702. }
  1703.  
  1704. template <class X>
  1705. unsigned int PrimitiveExArray<X>::getSize() const {
  1706. return this->sz;
  1707. }
  1708.  
  1709. template <class X>
  1710. bool PrimitiveExArray<X>::isEmpty() const {
  1711. return this->sz == 0;
  1712. }
  1713.  
  1714. template <class X>
  1715. void PrimitiveExArray<X>::push(const X& x) {
  1716. resize(this->sz + 1);
  1717. this->buf[this->sz - 1] = x;
  1718. }
  1719.  
  1720. template <class X>
  1721. void PrimitiveExArray<X>::pop() {
  1722. this->sz--;
  1723. }
  1724.  
  1725. template <class X>
  1726. void PrimitiveExArray<X>::clear() {
  1727. this->sz = 0;
  1728. }
  1729.  
  1730. template <class X>
  1731. void PrimitiveExArray<X>::resize(const unsigned int& sz) {
  1732. if (sz > this->buf_sz) {
  1733. unsigned int new_buf_sz = sz << 1;
  1734. X* new_buf = new X[new_buf_sz];
  1735. if (this->buf_sz != 0) {
  1736. memcpy(new_buf, this->buf, sizeof(X) * this->sz);
  1737. delete[] this->buf;
  1738. }
  1739. this->buf = new_buf;
  1740. this->buf_sz = new_buf_sz;
  1741. }
  1742. this->sz = sz;
  1743. }
  1744.  
  1745. template <class X>
  1746. ClassObjectExArray<X>::ClassObjectExArray(const unsigned int& sz) : buf(nullptr), buf_sz(0), sz(0) {
  1747. resize(sz);
  1748. }
  1749.  
  1750. template <class X>
  1751. ClassObjectExArray<X>::ClassObjectExArray(const ClassObjectExArray& a) : buf(nullptr), buf_sz(0), sz(0) {
  1752. if (a.sz != 0) {
  1753. if (this->buf) delete[] this->buf;
  1754. this->buf_sz = a.buf_sz;
  1755. this->buf = new X[this->buf_sz];
  1756. this->sz = a.sz;
  1757. copy(a.buf, a.buf + a.sz, this->buf);
  1758. }
  1759. }
  1760.  
  1761. template <class X>
  1762. ClassObjectExArray<X>& ClassObjectExArray<X>::operator =(const ClassObjectExArray& a) {
  1763. if (&a != this && a.sz != 0) {
  1764. if (this->buf) delete[] this->buf;
  1765. this->buf_sz = a.buf_sz;
  1766. this->buf = new X[this->buf_sz];
  1767. this->sz = a.sz;
  1768. copy(a.buf, a.buf + a.sz, this->buf);
  1769. }
  1770. return *this;
  1771. }
  1772.  
  1773. template <class X>
  1774. ClassObjectExArray<X>::~ClassObjectExArray() {
  1775. if (this->buf) delete[] this->buf;
  1776. }
  1777.  
  1778. template <class X>
  1779. X& ClassObjectExArray<X>::operator [](const unsigned int& i) {
  1780. return this->buf[i];
  1781. }
  1782.  
  1783. template <class X>
  1784. X& ClassObjectExArray<X>::at(const unsigned int& i) {
  1785. return this->buf[i];
  1786. }
  1787.  
  1788. template <class X>
  1789. X& ClassObjectExArray<X>::last() {
  1790. return this->buf[this->sz - 1];
  1791. }
  1792.  
  1793. template <class X>
  1794. unsigned int ClassObjectExArray<X>::getSize() const {
  1795. return this->sz;
  1796. }
  1797.  
  1798. template <class X>
  1799. bool ClassObjectExArray<X>::isEmpty() const {
  1800. return this->sz == 0;
  1801. }
  1802.  
  1803. template <class X>
  1804. void ClassObjectExArray<X>::push(const X& x) {
  1805. resize(this->sz + 1);
  1806. this->buf[this->sz - 1] = x;
  1807. }
  1808.  
  1809. template <class X>
  1810. void ClassObjectExArray<X>::pop() {
  1811. this->buf[this->sz - 1] = X();
  1812. this->sz--;
  1813. }
  1814.  
  1815. template <class X>
  1816. void ClassObjectExArray<X>::clear() {
  1817. for (unsigned int i = 0; i < this->sz; i++) this->buf[i] = X();
  1818. this->sz = 0;
  1819. }
  1820.  
  1821. template <class X>
  1822. void ClassObjectExArray<X>::resize(const unsigned int& sz) {
  1823. if (sz > this->buf_sz) {
  1824. unsigned int new_buf_sz = sz << 1;
  1825. X* new_buf = new X[new_buf_sz];
  1826. if (this->buf_sz != 0) {
  1827. copy(this->buf, this->buf + this->sz, new_buf);
  1828. delete[] this->buf;
  1829. }
  1830. this->buf = new_buf;
  1831. this->buf_sz = new_buf_sz;
  1832. }
  1833. else if (sz < this->sz) for (unsigned int i = sz; i < this->sz; i++) this->buf[i] = X();
  1834. this->sz = sz;
  1835. }
  1836.  
  1837. template <class X> X rotateLeft(const X& x, const unsigned int& n, const unsigned int& w) {
  1838. return (x << n) | (x >> (w - n));
  1839. }
  1840.  
  1841. int main() {
  1842. try {
  1843. clock_t begin = clock();
  1844.  
  1845. shared_ptr<RPG> rpg(new SimpleRPG());
  1846. cout << rpg->generatePrime(256).toString() << endl;
  1847.  
  1848. clock_t end = clock();
  1849. clock_t elapsed = end - begin;
  1850. cout << (elapsed / CLOCKS_PER_SEC) << "." << setfill('0') << setw(3) << ((elapsed % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC) << " secs" << endl;
  1851. }
  1852. catch (const string& msg) {
  1853. cerr << msg << endl;
  1854. return 1;
  1855. }
  1856. return 0;
  1857. }
  1858.  
Success #stdin #stdout 0.5s 8280KB
stdin
Standard input is empty
stdout
78551015427409677302030381126230618631368806539704749539929326001219980457393
0.500 secs