BigInt64.hpp 19 KB

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