BigInt64.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #ifndef BIGINT64_HPP
  2. #define BIGINT64_HPP
  3. #include <cstdint>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <limits>
  10. #include <initializer_list>
  11. #include <deque>
  12. #include <vector>
  13. #include <string>
  14. #include <bitset>
  15. #include <iostream>
  16. const std::vector<char> chars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  17. const __uint128_t _m = ((__uint128_t)1) << 64;
  18. template<typename T>
  19. inline int signum(T t){
  20. if(t < 0)return -1;
  21. if(t >= 0)return 1;
  22. assert(false);
  23. }
  24. inline std::ostream& operator<<(std::ostream& out, __uint128_t o){
  25. if(o == 0)return out << "0";
  26. while(o > 0){
  27. out << std::to_string((int)(o % 10));
  28. o /= 10;
  29. }
  30. return out;
  31. }
  32. struct BigInt{
  33. using size_t = std::size_t;
  34. using ssize_t = std::int64_t;
  35. using uint64_t = std::uint64_t;
  36. using uint32_t = std::uint32_t;
  37. using lui = ::__uint128_t;
  38. using iterator = std::deque<uint64_t>::iterator;
  39. using const_iterator = std::deque<uint64_t>::const_iterator;
  40. using reverse_iterator = std::deque<uint64_t>::reverse_iterator;
  41. using const_reverse_iterator = std::deque<uint64_t>::const_reverse_iterator;
  42. std::deque<uint64_t> data;
  43. int signum;
  44. inline BigInt() : data(1,0),signum(1){}
  45. inline BigInt(size_t _s, uint64_t fill) : data(_s, fill), signum(1){}
  46. inline BigInt(int a) : data(1, std::abs(a)),signum(::signum(a)){}
  47. inline BigInt(unsigned int a) : data(1, a),signum(1){}
  48. inline BigInt(unsigned long long a) : data(1, a), signum(1){}
  49. inline BigInt(long long a) : data(1, std::abs(a)),signum(::signum(a)){}
  50. inline BigInt(const std::initializer_list<uint64_t>& l) : data(l), signum(1){}
  51. inline BigInt(std::initializer_list<uint64_t>&& l) : data(std::move(l)), signum(1){}
  52. inline BigInt(const BigInt& o) : data(o.data), signum(o.signum){}
  53. inline BigInt(BigInt&& o) : data(std::move(o.data)), signum(o.signum){}
  54. inline BigInt(const std::string& o){
  55. signum = 1;
  56. auto it = o.rbegin();
  57. BigInt baseTen(1);
  58. while(it != o.rend()){
  59. if(*it == '-'){
  60. assert(it != o.rbegin());
  61. assert((++it) == o.rend());
  62. signum = -1;
  63. break;
  64. }
  65. BigInt l = baseTen.mult(BigInt(*it - '0'));
  66. adda(l);
  67. baseTen = baseTen.mult(BigInt(10));
  68. ++it;
  69. }
  70. trim();
  71. }
  72. template<typename InputIterator>
  73. inline BigInt(InputIterator begin, InputIterator end) : data(begin, end), signum(1){}
  74. template<typename RNG>
  75. inline BigInt(RNG& rng, size_t length) : data(length, 0), signum(1){std::generate(data.begin(),data.end(), [&rng](){return rng();});}
  76. inline std::deque<uint64_t>::iterator begin(){return data.begin();}
  77. inline std::deque<uint64_t>::iterator end(){return data.end();}
  78. inline std::deque<uint64_t>::reverse_iterator rbegin(){return data.rbegin();}
  79. inline std::deque<uint64_t>::reverse_iterator rend(){return data.rend();}
  80. inline std::deque<uint64_t>::const_iterator begin()const{return data.begin();}
  81. inline std::deque<uint64_t>::const_iterator end()const{return data.end();}
  82. inline std::deque<uint64_t>::const_reverse_iterator rbegin()const{return data.rbegin();}
  83. inline std::deque<uint64_t>::const_reverse_iterator rend()const{return data.rend();}
  84. inline uint64_t& operator[](size_t i){return data[i];}
  85. inline const uint64_t& operator[](size_t i)const{return data[i];}
  86. inline size_t size()const{return data.size();}
  87. inline auto cbegin()const{return data.cbegin();}
  88. inline auto cend()const{return data.cend();}
  89. inline auto crbegin()const{return data.crbegin();}
  90. inline auto crend()const{return data.crend();}
  91. inline BigInt& operator=(const BigInt& o){signum = o.signum;data = o.data;return *this;}
  92. inline BigInt& operator=(BigInt&& o){data = std::move(o.data);signum = o.signum;return *this;}
  93. inline size_t bitscanForward()const{
  94. auto it = begin();
  95. size_t s = 0;
  96. while(*it == 0 && it != end()){
  97. ++it;
  98. s += 64;
  99. }
  100. if(it == end())return s + 64;
  101. return s + __builtin_clzll(*it);
  102. }
  103. inline size_t bitscanReverse()const{
  104. auto it = rbegin();
  105. size_t s = 0;
  106. while(*it == 0 && it != rend()){
  107. ++it;
  108. s += 64;
  109. }
  110. if(it == rend())return s + 64;
  111. return s + __builtin_ctzll(*it);
  112. }
  113. inline bool operator<(const BigInt& o)const{
  114. if(signum > o.signum){
  115. return false;
  116. }
  117. if(signum < o.signum){
  118. return true;
  119. }
  120. const_iterator it1 = begin();
  121. const_iterator it2 = o.begin();
  122. if(size() > o.size()){
  123. it1 += size() - o.size();
  124. auto _t = begin();
  125. while(_t != it1){
  126. if(*_t)return false;
  127. ++_t;
  128. }
  129. }
  130. else if(size() < o.size()){
  131. it2 += o.size() - size();
  132. auto _t = o.begin();
  133. while(_t != it2){
  134. if(*_t)return true;
  135. ++_t;
  136. }
  137. }
  138. while(it1 != end() && it2 != o.end()){
  139. if(*(it1) < *(it2))return true;
  140. if(*(it1) > *(it2))return false;
  141. ++it1;++it2;
  142. }
  143. return false;
  144. }
  145. inline bool operator>(const BigInt& o)const{
  146. if(signum < o.signum){
  147. return false;
  148. }
  149. if(signum > o.signum){
  150. return true;
  151. }
  152. const_iterator it1 = begin();
  153. const_iterator it2 = o.begin();
  154. if(size() > o.size()){
  155. it1 += size() - o.size();
  156. auto _t = begin();
  157. while(_t != it1){
  158. if(*_t)return true;
  159. ++_t;
  160. }
  161. }
  162. else if(size() < o.size()){
  163. it2 += o.size() - size();
  164. auto _t = o.begin();
  165. while(_t != it2){
  166. if(*_t)return false;
  167. ++_t;
  168. }
  169. }
  170. while(it1 != end() && it2 != o.end()){
  171. if(*(it1) > *(it2))return true;
  172. if(*(it1) < *(it2))return false;
  173. ++it1;++it2;
  174. }
  175. return false;
  176. }
  177. inline bool operator<=(const BigInt& o)const{
  178. if(signum > o.signum){
  179. return false;
  180. }
  181. if(signum < o.signum){
  182. return true;
  183. }
  184. const_iterator it1 = begin();
  185. const_iterator it2 = o.begin();
  186. if(size() > o.size()){
  187. it1 += size() - o.size();
  188. auto _t = begin();
  189. while(_t != it1){
  190. if(*_t)return true;
  191. ++_t;
  192. }
  193. }
  194. else if(size() < o.size()){
  195. it2 += o.size() - size();
  196. auto _t = o.begin();
  197. while(_t != it2){
  198. if(*_t)return false;
  199. ++_t;
  200. }
  201. }
  202. while(it1 != end() && it2 != o.end()){
  203. if(*(it1) < *(it2))return true;
  204. if(*(it1) > *(it2))return false;
  205. ++it1;++it2;
  206. }
  207. return true;
  208. }
  209. inline bool operator>=(const BigInt& o)const{
  210. if(signum < o.signum){
  211. return false;
  212. }
  213. if(signum > o.signum){
  214. return true;
  215. }
  216. const_iterator it1 = begin();
  217. const_iterator it2 = o.begin();
  218. if(size() > o.size()){
  219. it1 += size() - o.size();
  220. auto _t = begin();
  221. while(_t != it1){
  222. if(*_t)return true;
  223. ++_t;
  224. }
  225. }
  226. else if(size() < o.size()){
  227. it2 += o.size() - size();
  228. auto _t = o.begin();
  229. while(_t != it2){
  230. if(*_t)return false;
  231. ++_t;
  232. }
  233. }
  234. while(it1 != end() && it2 != o.end()){
  235. if(*(it1) > *(it2))return true;
  236. if(*(it1) < *(it2))return false;
  237. ++it1;++it2;
  238. }
  239. return true;
  240. }
  241. inline bool operator==(const BigInt& o)const{
  242. if(signum < o.signum){
  243. return false;
  244. }
  245. if(signum > o.signum){
  246. return false;
  247. }
  248. const_iterator it1 = begin();
  249. const_iterator it2 = o.begin();
  250. if(size() > o.size()){
  251. it1 += size() - o.size();
  252. auto _t = begin();
  253. while(_t != it1){
  254. if(*_t)return false;
  255. ++_t;
  256. }
  257. }
  258. else if(size() < o.size()){
  259. it2 += o.size() - size();
  260. auto _t = o.begin();
  261. while(_t != it2){
  262. if(*_t)return false;
  263. ++_t;
  264. }
  265. }
  266. while(it1 != end() && it2 != o.end()){
  267. if(*(it1) != *(it2))return false;
  268. ++it1;++it2;
  269. }
  270. return true;
  271. }
  272. inline bool operator==(uint64_t o){
  273. return size() == 1 && *rbegin() == o;
  274. }
  275. inline bool isZero()const{
  276. for(auto it = data.begin();it != data.end();it++){
  277. if(*it)return false;
  278. }
  279. return true;
  280. }
  281. inline void setZero(){
  282. for(auto it = begin();it != end();it++)*it = 0;
  283. }
  284. inline BigInt& trim(){
  285. while(data[0] == 0 && data.size() > 1){
  286. data.pop_front();
  287. }
  288. return *this;
  289. }
  290. inline BigInt div(uint64_t d)const{
  291. BigInt ret = *this;
  292. lui carry = 0;
  293. for(auto it = ret.data.begin();it != ret.data.end();it++){
  294. lui temp = (lui)*it;
  295. temp += carry;
  296. carry = temp % d;
  297. *it = (uint64_t)(temp / d);
  298. carry <<= 64;
  299. }
  300. return ret;
  301. }
  302. inline BigInt& div(uint64_t d){
  303. lui carry = 0;
  304. for(auto it = data.begin();it != data.end();it++){
  305. lui temp = (lui)*it;
  306. temp += carry;
  307. carry = temp % d;
  308. *it = (uint64_t)(temp / d);
  309. carry <<= 64;
  310. }
  311. return *this;
  312. }
  313. inline uint64_t mod(uint64_t m)const{
  314. lui carry = 0;
  315. for(auto it = data.begin();it != data.end();it++){
  316. carry <<= 64;
  317. carry = (*it + carry) % m;
  318. }
  319. return carry;
  320. }
  321. inline BigInt& bitshiftLeft(int c){
  322. if(c < 0)return bitshiftRight(-c);
  323. unsigned int sh = c % 64;
  324. unsigned int jmp = c / 64;
  325. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  326. auto it1 = begin();
  327. auto it2 = it1 + jmp;
  328. auto beforeEnd = end() - 1;
  329. while(it2 != beforeEnd){
  330. *it1 = (*it2 << sh);
  331. if(sh)
  332. *it1 |= (*(it2 + 1) >> (64 - sh));
  333. ++it1;++it2;
  334. }
  335. *it1 = (*it2 << sh);
  336. ++it1;++it2;
  337. while(it1 != end()){
  338. *it1 = 0;
  339. ++it1;
  340. }
  341. return *this;
  342. }
  343. inline BigInt& bitshiftLeft_expand(int c){
  344. if(c < 0)return bitshiftRight(-c);
  345. unsigned int sh = c % 64;
  346. unsigned int jmp = c / 64;
  347. int insert_count = std::max((size_t)0, jmp - bitscanForward() / 64 + 1);
  348. while(insert_count --> 0)
  349. data.push_front(0);
  350. auto it1 = begin();
  351. auto it2 = it1 + jmp;
  352. auto beforeEnd = end() - 1;
  353. while(it2 != beforeEnd){
  354. *it1 = (*it2 << sh);
  355. if(sh)
  356. *it1 |= (*(it2 + 1) >> (64 - sh));
  357. ++it1;++it2;
  358. }
  359. *it1 = (*it2 << sh);
  360. ++it1;++it2;
  361. while(it1 != end()){
  362. *it1 = 0;
  363. ++it1;
  364. }
  365. return *this;
  366. }
  367. inline BigInt& bitshiftRight(int c){
  368. if(c < 0)return bitshiftLeft(-c);
  369. unsigned int sh = c % 64;
  370. unsigned int jmp = c / 64;
  371. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  372. auto it1 = rbegin();
  373. auto it2 = it1 + jmp;
  374. auto beforeRend = rend() - 1;
  375. while(it2 != beforeRend){
  376. *it1 = (*it2 >> sh);
  377. if(sh)
  378. *it1 |= (*(it2 + 1) << (64 - sh));
  379. ++it1;++it2;
  380. }
  381. *it1 = (*it2 >> sh);
  382. ++it1;++it2;
  383. while(it1 != rend()){
  384. *it1 = 0;
  385. ++it1;
  386. }
  387. return *this;
  388. }
  389. inline BigInt operator<<(int c)const{
  390. BigInt ret = *this;
  391. ret.bitshiftLeft(c);
  392. return ret;
  393. }
  394. inline BigInt operator>>(int c)const{
  395. BigInt ret = *this;
  396. ret.bitshiftRight(c);
  397. return ret;
  398. }
  399. inline BigInt& operator<<=(int c){
  400. return bitshiftLeft(c);
  401. }
  402. inline BigInt& operator>>=(int c){
  403. return bitshiftRight(c);
  404. }
  405. inline BigInt& operator&=(const BigInt& o){
  406. auto it1 = rbegin();
  407. auto it2 = o.rbegin();
  408. while(it1 != rend() && it2 != o.rend())
  409. *(it1++) &= *(it2++);
  410. return *this;
  411. }
  412. inline BigInt& operator|=(const BigInt& o){
  413. auto it1 = rbegin();
  414. auto it2 = o.rbegin();
  415. while(it1 != rend() && it2 != o.rend())
  416. *(it1++) |= *(it2++);
  417. return *this;
  418. }
  419. inline BigInt& operator^=(const BigInt& o){
  420. auto it1 = rbegin();
  421. auto it2 = o.rbegin();
  422. while(it1 != rend() && it2 != o.rend())
  423. *(it1++) ^= *(it2++);
  424. return *this;
  425. }
  426. inline BigInt operator&(const BigInt& o)const{
  427. BigInt ret(*this);
  428. ret &= o;
  429. return ret;
  430. }
  431. inline BigInt operator|(const BigInt& o)const{
  432. BigInt ret(*this);
  433. ret |= o;
  434. return ret;
  435. }
  436. inline BigInt operator^(const BigInt& o)const{
  437. BigInt ret(*this);
  438. ret ^= o;
  439. return ret;
  440. }
  441. inline int bitDifference(const BigInt& o){
  442. int pos1(0),pos2(0);
  443. auto it1 = begin();
  444. auto it2 = o.begin();
  445. bool bitfound = false;
  446. while(it1 != end()){
  447. if(*it1 == 0)
  448. pos1 += 64;
  449. else{
  450. pos1 += __builtin_clzll(*it1);
  451. bitfound = true;
  452. break;
  453. }
  454. ++it1;
  455. }
  456. if(!bitfound)pos1 = size() * 64;
  457. bitfound = false;
  458. while(it2 != o.end()){
  459. if(*it2 == 0)
  460. pos2 += 64;
  461. else{
  462. pos2 += __builtin_clzll(*it2);
  463. bitfound = true;
  464. break;
  465. }
  466. ++it2;
  467. }
  468. if(!bitfound)pos2 = o.size() * 64;
  469. int sizediff = ((signed int)size()) - ((signed int)o.size());
  470. return pos2 - pos1 + sizediff * 64;
  471. }
  472. inline BigInt& chunkshiftLeft(int c){
  473. if(c < 0)return chunkshiftRight(-c);
  474. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  475. auto it1 = data.begin();
  476. auto it2 = it1 + c;
  477. while(it2 != data.end())*(it1++) = *(it2++);
  478. while(it1 != data.end())*(it1++) = 0;
  479. return *this;
  480. }
  481. inline BigInt& chunkshiftRight(int c){
  482. if(c < 0)return chunkshiftLeft(-c);
  483. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  484. auto it1 = data.rbegin();
  485. auto it2 = it1 + c;
  486. while(it2 != data.rend())*(it1++) = *(it2++);
  487. while(it1 != data.rend())*(it1++) = 0;
  488. return *this;
  489. }
  490. inline BigInt bitshiftLeft(int c)const{
  491. if(c < 0)return bitshiftRight(-c);
  492. BigInt _this = *this;
  493. _this.bitshiftLeft(c);
  494. return _this;
  495. }
  496. inline BigInt bitshiftRight(int c)const{
  497. if(c < 0)return bitshiftLeft(-c);
  498. BigInt _this = *this;
  499. _this.bitshiftRight(c);
  500. return _this;
  501. }
  502. inline BigInt chunkshiftLeft(int c)const{
  503. if(c < 0)return chunkshiftRight(-c);
  504. BigInt _this = *this;
  505. _this.chunkshiftLeft(c);
  506. return _this;
  507. }
  508. inline BigInt chunkshiftRight(int c)const{
  509. if(c < 0)return chunkshiftLeft(-c);
  510. BigInt _this = *this;
  511. _this.chunkshiftRight(c);
  512. return _this;
  513. }
  514. inline BigInt add(const BigInt& o){
  515. BigInt ret = *this;
  516. ret.adda(o);
  517. return ret;
  518. }
  519. inline BigInt& adda(const BigInt& o){
  520. while(size() < o.size())data.push_front(0);
  521. bool carry = 0;
  522. auto it1 = rbegin();
  523. auto it2 = o.rbegin();
  524. while(it1 != rend() && it2 != o.rend()){
  525. carry = __builtin_uaddll_overflow(*it1, *it2 + carry, (unsigned long long*)(&(*it1)));
  526. carry |= (*it2 + carry) == 0 && *it2;
  527. it1++;
  528. it2++;
  529. }
  530. while(it1 != rend() && carry){
  531. carry = __builtin_uaddll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  532. ++it1;
  533. }
  534. if(carry)data.push_front(1);
  535. return *this;
  536. }
  537. inline BigInt& suba(const BigInt& o){
  538. assert((*this) >= (o));
  539. bool carry = 0;
  540. auto it1 = rbegin();
  541. auto it2 = o.rbegin();
  542. while(it1 != rend() && it2 != o.rend()){
  543. carry = __builtin_usubll_overflow(*it1 - carry, *it2, (unsigned long long*)(&(*it1)));
  544. if(!carry)carry = ((*it1 - carry) == std::numeric_limits<uint64_t>::max());
  545. ++it1;++it2;
  546. }
  547. while(it1 != rend() && carry){
  548. carry = __builtin_usubll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  549. ++it1;
  550. }
  551. return *this;
  552. }
  553. inline BigInt& moda(const BigInt& o){
  554. assert(!o.isZero());
  555. if(o > *this)return *this;
  556. BigInt mo = o;
  557. mo = o;
  558. int bd = bitDifference(o);
  559. mo.bitshiftLeft_expand(bd);
  560. if(mo > *this)mo.bitshiftRight(1);
  561. this->suba(mo);
  562. if(*this < o)return *this;
  563. while(true){
  564. bd = mo.bitDifference(*this);
  565. mo >>= bd;
  566. mo.trim();
  567. if(mo > *this)mo >>= 1;
  568. this->suba(mo);
  569. if(*this < o)return *this;
  570. }
  571. return *this;
  572. }
  573. inline bool even(){
  574. return !(*rbegin() & 1);
  575. }
  576. inline BigInt modPow(BigInt o, const BigInt& mod)const{
  577. BigInt t = *this;
  578. BigInt odd(1);
  579. while(true){
  580. if(o.even()){
  581. t = t.mult(t);
  582. t.trim();
  583. t.moda(mod);
  584. o.div(2);
  585. }
  586. else{
  587. odd = odd.mult(t);
  588. odd.moda(mod);
  589. odd.trim();
  590. t.trim();
  591. t.moda(mod);
  592. o.suba(BigInt(1));
  593. }
  594. //::cout << "Reduced" << std::endl;
  595. o.trim();
  596. if(o == 1){t = t.mult(odd);t.moda(mod);return t;}
  597. }
  598. }
  599. inline BigInt mult(const BigInt& o)const{
  600. BigInt result(size() + o.size() + 1,0);
  601. BigInt temp(size() + o.size() + 1,0);
  602. int p = 0;
  603. for(auto it1 = rbegin();it1 != rend();it1++){
  604. auto it = temp.rbegin();
  605. lui carry = 0;
  606. for(auto it2 = o.rbegin();it2 != o.rend();it2++){
  607. lui prod = ((lui)*it1) * (*it2);
  608. prod += carry;
  609. *(it++) = (uint64_t)prod;
  610. carry = (prod >> 64);
  611. }
  612. if(carry)(*it) += carry;
  613. temp.chunkshiftLeft(p++);
  614. result.adda(temp);
  615. temp.setZero();
  616. }
  617. result.trim();
  618. return result;
  619. }
  620. inline std::string rawString()const{
  621. std::string s = "";
  622. bool flag;
  623. for(unsigned int i = 0;i < size();i++){
  624. if(data[i])flag = true;
  625. if(flag);
  626. s += std::to_string(data[i]);
  627. if(i < size() - 1)s += "|";
  628. }
  629. return s;
  630. }
  631. inline std::string bitString()const{
  632. auto it = begin();
  633. std::string ret = "";
  634. //std::cout << size() << "; " << std::flush;
  635. while(it != end()){
  636. std::bitset<64> bits(*it);
  637. for(unsigned int i = 0;i < 64;i++){
  638. ret += chars.at(((*it) & (1ULL << (64 - i))) != 0);
  639. }
  640. ++it;
  641. }
  642. return ret;
  643. }
  644. inline std::string toString()const{
  645. if(isZero())return std::to_string(0);
  646. std::deque<char> c_str;
  647. const uint64_t q = 1000000000000000000ULL;
  648. BigInt diver = *this;
  649. while(!diver.isZero()){
  650. std::string frac = std::to_string(diver.mod(q));
  651. int a = 0;
  652. for(auto it = frac.rbegin();it != frac.rend();it++){
  653. c_str.push_front(*it);
  654. a++;
  655. }
  656. while(a < 18){
  657. c_str.push_front('0');
  658. a++;
  659. }
  660. diver.div(q);
  661. }
  662. auto it = c_str.begin();
  663. while(*(it) == '0')++it;
  664. return std::string(it, c_str.end());
  665. }
  666. inline std::string toString(unsigned int base)const{
  667. if(isZero())return std::to_string(0);
  668. if(base == 2)return bitString();
  669. if(base == 10)return toString();
  670. std::vector<char> c_str;
  671. c_str.reserve(size() * (unsigned int)(64.0 * std::log(2) / std::log((double)base)));
  672. BigInt diver = *this;
  673. while(!diver.isZero()){
  674. c_str.push_back(chars.at(diver.mod(base)));
  675. diver.div(base);
  676. }
  677. return std::string(c_str.rbegin(), c_str.rend());
  678. }
  679. };
  680. namespace std{
  681. template<>
  682. struct hash<BigInt>{
  683. inline size_t operator()(const BigInt& o)const{
  684. size_t ret = 0;
  685. std::for_each(o.begin(), o.end(), [&ret](const uint64_t& ui){ret ^= ui;});
  686. return ret;
  687. }
  688. };
  689. }
  690. #endif //BIGINT64_HPP