BigInt64.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. template<typename InputIterator>
  55. inline BigInt(InputIterator begin, InputIterator end) : data(begin, end), signum(1){}
  56. template<typename RNG>
  57. inline BigInt(RNG& rng, size_t length) : data(length, 0), signum(1){std::generate(data.begin(),data.end(), [&rng](){return rng();});}
  58. std::deque<uint64_t>::iterator begin(){return data.begin();}
  59. std::deque<uint64_t>::iterator end(){return data.end();}
  60. std::deque<uint64_t>::reverse_iterator rbegin(){return data.rbegin();}
  61. std::deque<uint64_t>::reverse_iterator rend(){return data.rend();}
  62. std::deque<uint64_t>::const_iterator begin()const{return data.begin();}
  63. std::deque<uint64_t>::const_iterator end()const{return data.end();}
  64. std::deque<uint64_t>::const_reverse_iterator rbegin()const{return data.rbegin();}
  65. std::deque<uint64_t>::const_reverse_iterator rend()const{return data.rend();}
  66. BigInt& operator=(const BigInt& o){
  67. data = o.data;
  68. signum = o.signum;
  69. return *this;
  70. }
  71. BigInt& operator=(BigInt&& o){
  72. data = std::move(o.data);
  73. signum = o.signum;
  74. return *this;
  75. }
  76. uint64_t& operator[](size_t i){return data[i];}
  77. const uint64_t& operator[](size_t i)const{return data[i];}
  78. size_t size()const{return data.size();}
  79. auto cbegin()const{return data.cbegin();}
  80. auto cend()const{return data.cend();}
  81. auto crbegin()const{return data.crbegin();}
  82. auto crend()const{return data.crend();}
  83. inline bool operator<(const BigInt& o)const{
  84. if(signum > o.signum){
  85. return false;
  86. }
  87. if(signum < o.signum){
  88. return true;
  89. }
  90. const_iterator it1 = begin();
  91. const_iterator it2 = o.begin();
  92. if(size() > o.size()){
  93. it1 += size() - o.size();
  94. auto _t = begin();
  95. while(_t != it1){
  96. if(*_t)return false;
  97. ++_t;
  98. }
  99. }
  100. else if(size() < o.size()){
  101. it2 += o.size() - size();
  102. auto _t = o.begin();
  103. while(_t != it2){
  104. if(*_t)return true;
  105. ++_t;
  106. }
  107. }
  108. while(it1 != end() && it2 != o.end()){
  109. if(*(it1) < *(it2))return true;
  110. if(*(it1) > *(it2))return false;
  111. ++it1;++it2;
  112. }
  113. return false;
  114. }
  115. inline bool operator>(const BigInt& o)const{
  116. if(signum < o.signum){
  117. return false;
  118. }
  119. if(signum > o.signum){
  120. return true;
  121. }
  122. const_iterator it1 = begin();
  123. const_iterator it2 = o.begin();
  124. if(size() > o.size()){
  125. it1 += size() - o.size();
  126. auto _t = begin();
  127. while(_t != it1){
  128. if(*_t)return true;
  129. ++_t;
  130. }
  131. }
  132. else if(size() < o.size()){
  133. it2 += o.size() - size();
  134. auto _t = o.begin();
  135. while(_t != it2){
  136. if(*_t)return false;
  137. ++_t;
  138. }
  139. }
  140. while(it1 != end() && it2 != o.end()){
  141. if(*(it1) > *(it2))return true;
  142. if(*(it1) < *(it2))return false;
  143. ++it1;++it2;
  144. }
  145. return false;
  146. }
  147. inline bool operator<=(const BigInt& o)const{
  148. if(signum > o.signum){
  149. return false;
  150. }
  151. if(signum < o.signum){
  152. return true;
  153. }
  154. const_iterator it1 = begin();
  155. const_iterator it2 = o.begin();
  156. if(size() > o.size()){
  157. it1 += size() - o.size();
  158. auto _t = begin();
  159. while(_t != it1){
  160. if(*_t)return true;
  161. ++_t;
  162. }
  163. }
  164. else if(size() < o.size()){
  165. it2 += o.size() - size();
  166. auto _t = o.begin();
  167. while(_t != it2){
  168. if(*_t)return false;
  169. ++_t;
  170. }
  171. }
  172. while(it1 != end() && it2 != o.end()){
  173. if(*(it1) < *(it2))return true;
  174. if(*(it1) > *(it2))return false;
  175. ++it1;++it2;
  176. }
  177. return true;
  178. }
  179. inline bool operator>=(const BigInt& o)const{
  180. if(signum < o.signum){
  181. return false;
  182. }
  183. if(signum > o.signum){
  184. return true;
  185. }
  186. const_iterator it1 = begin();
  187. const_iterator it2 = o.begin();
  188. if(size() > o.size()){
  189. it1 += size() - o.size();
  190. auto _t = begin();
  191. while(_t != it1){
  192. if(*_t)return true;
  193. ++_t;
  194. }
  195. }
  196. else if(size() < o.size()){
  197. it2 += o.size() - size();
  198. auto _t = o.begin();
  199. while(_t != it2){
  200. if(*_t)return false;
  201. ++_t;
  202. }
  203. }
  204. while(it1 != end() && it2 != o.end()){
  205. if(*(it1) > *(it2))return true;
  206. if(*(it1) < *(it2))return false;
  207. ++it1;++it2;
  208. }
  209. return true;
  210. }
  211. inline bool operator==(const BigInt& o)const{
  212. if(signum < o.signum){
  213. return false;
  214. }
  215. if(signum > o.signum){
  216. return false;
  217. }
  218. const_iterator it1 = begin();
  219. const_iterator it2 = o.begin();
  220. if(size() > o.size()){
  221. it1 += size() - o.size();
  222. auto _t = begin();
  223. while(_t != it1){
  224. if(*_t)return false;
  225. ++_t;
  226. }
  227. }
  228. else if(size() < o.size()){
  229. it2 += o.size() - size();
  230. auto _t = o.begin();
  231. while(_t != it2){
  232. if(*_t)return false;
  233. ++_t;
  234. }
  235. }
  236. while(it1 != end() && it2 != o.end()){
  237. if(*(it1) != *(it2))return false;
  238. ++it1;++it2;
  239. }
  240. return true;
  241. }
  242. inline bool isZero()const{
  243. for(auto it = data.begin();it != data.end();it++){
  244. if(*it)return false;
  245. }
  246. return true;
  247. }
  248. inline void setZero(){
  249. for(auto it = begin();it != end();it++)*it = 0;
  250. }
  251. inline BigInt& trim(){
  252. while(data[0] == 0 && data.size() > 1){
  253. data.pop_front();
  254. }
  255. return *this;
  256. }
  257. inline BigInt div(uint64_t d)const{
  258. BigInt ret = *this;
  259. lui carry = 0;
  260. for(auto it = ret.data.begin();it != ret.data.end();it++){
  261. lui temp = (lui)*it;
  262. temp += carry;
  263. carry = temp % d;
  264. *it = (uint64_t)(temp / d);
  265. carry <<= 64;
  266. }
  267. return ret;
  268. }
  269. inline BigInt& div(uint64_t d){
  270. lui carry = 0;
  271. for(auto it = data.begin();it != data.end();it++){
  272. lui temp = (lui)*it;
  273. temp += carry;
  274. carry = temp % d;
  275. *it = (uint64_t)(temp / d);
  276. carry <<= 64;
  277. }
  278. return *this;
  279. }
  280. inline uint64_t mod(uint64_t m)const{
  281. lui carry = 0;
  282. for(auto it = data.begin();it != data.end();it++){
  283. carry <<= 64;
  284. carry = (*it + carry) % m;
  285. }
  286. return carry;
  287. }
  288. inline BigInt& bitshiftLeft(int c){
  289. if(c < 0)return bitshiftRight(-c);
  290. unsigned int sh = c % 64;
  291. unsigned int jmp = c / 64;
  292. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  293. auto it1 = begin();
  294. auto it2 = it1 + jmp;
  295. auto beforeEnd = end() - 1;
  296. while(it2 != beforeEnd){
  297. *it1 = (*it2 << sh);
  298. if(sh)
  299. *it1 |= (*(it2 + 1) >> (64 - sh));
  300. ++it1;++it2;
  301. }
  302. *it1 = (*it2 << sh);
  303. ++it1;++it2;
  304. while(it1 != end()){
  305. *it1 = 0;
  306. ++it1;
  307. }
  308. return *this;
  309. }
  310. inline BigInt& bitshiftRight(int c){
  311. if(c < 0)return bitshiftLeft(-c);
  312. unsigned int sh = c % 64;
  313. unsigned int jmp = c / 64;
  314. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  315. auto it1 = rbegin();
  316. auto it2 = it1 + jmp;
  317. auto beforeRend = rend() - 1;
  318. while(it2 != beforeRend){
  319. *it1 = (*it2 >> sh);
  320. if(sh)
  321. *it1 |= (*(it2 + 1) << (64 - sh));
  322. ++it1;++it2;
  323. }
  324. *it1 = (*it2 >> sh);
  325. ++it1;++it2;
  326. while(it1 != rend()){
  327. *it1 = 0;
  328. ++it1;
  329. }
  330. return *this;
  331. }
  332. BigInt operator<<(int c)const{
  333. BigInt ret = *this;
  334. ret.bitshiftLeft(c);
  335. return ret;
  336. }
  337. BigInt operator>>(int c)const{
  338. BigInt ret = *this;
  339. ret.bitshiftRight(c);
  340. return ret;
  341. }
  342. BigInt& operator<<=(int c){
  343. return bitshiftLeft(c);
  344. }
  345. BigInt& operator>>=(int c){
  346. return bitshiftRight(c);
  347. }
  348. BigInt& operator&=(const BigInt& o){
  349. auto it1 = rbegin();
  350. auto it2 = o.rbegin();
  351. while(it1 != rend() && it2 != o.rend())
  352. *(it1++) &= *(it2++);
  353. return *this;
  354. }
  355. BigInt& operator|=(const BigInt& o){
  356. auto it1 = rbegin();
  357. auto it2 = o.rbegin();
  358. while(it1 != rend() && it2 != o.rend())
  359. *(it1++) |= *(it2++);
  360. return *this;
  361. }
  362. BigInt& operator^=(const BigInt& o){
  363. auto it1 = rbegin();
  364. auto it2 = o.rbegin();
  365. while(it1 != rend() && it2 != o.rend())
  366. *(it1++) ^= *(it2++);
  367. return *this;
  368. }
  369. BigInt operator&(const BigInt& o)const{
  370. BigInt ret(*this);
  371. ret &= o;
  372. return ret;
  373. }
  374. BigInt operator|(const BigInt& o)const{
  375. BigInt ret(*this);
  376. ret |= o;
  377. return ret;
  378. }
  379. BigInt operator^(const BigInt& o)const{
  380. BigInt ret(*this);
  381. ret ^= o;
  382. return ret;
  383. }
  384. int bitDifference(const BigInt& o){
  385. int pos1(0),pos2(0);
  386. auto it1 = begin();
  387. auto it2 = o.begin();
  388. bool bitfound = false;
  389. while(it1 != end()){
  390. if(*it1 == 0)
  391. pos1 += 64;
  392. else{
  393. pos1 += __builtin_clzll(*it1);
  394. bitfound = true;
  395. break;
  396. }
  397. ++it1;
  398. }
  399. if(!bitfound)pos1 = size() * 64;
  400. bitfound = false;
  401. while(it2 != o.end()){
  402. if(*it2 == 0)
  403. pos2 += 64;
  404. else{
  405. pos2 += __builtin_clzll(*it2);
  406. bitfound = true;
  407. break;
  408. }
  409. ++it2;
  410. }
  411. if(!bitfound)pos2 = o.size() * 64;
  412. int sizediff = ((signed int)size()) - ((signed int)o.size());
  413. return pos2 - pos1 + sizediff * 64;
  414. }
  415. inline BigInt& chunkshiftLeft(int c){
  416. if(c < 0)return chunkshiftRight(-c);
  417. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  418. auto it1 = data.begin();
  419. auto it2 = it1 + c;
  420. while(it2 != data.end())*(it1++) = *(it2++);
  421. while(it1 != data.end())*(it1++) = 0;
  422. return *this;
  423. }
  424. inline BigInt& chunkshiftRight(int c){
  425. if(c < 0)return chunkshiftLeft(-c);
  426. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  427. auto it1 = data.rbegin();
  428. auto it2 = it1 + c;
  429. while(it2 != data.rend())*(it1++) = *(it2++);
  430. while(it1 != data.rend())*(it1++) = 0;
  431. return *this;
  432. }
  433. inline BigInt bitshiftLeft(int c)const{
  434. if(c < 0)return bitshiftRight(-c);
  435. BigInt _this = *this;
  436. _this.bitshiftLeft(c);
  437. return _this;
  438. }
  439. inline BigInt bitshiftRight(int c)const{
  440. if(c < 0)return bitshiftLeft(-c);
  441. BigInt _this = *this;
  442. _this.bitshiftRight(c);
  443. return _this;
  444. }
  445. inline BigInt chunkshiftLeft(int c)const{
  446. if(c < 0)return chunkshiftRight(-c);
  447. BigInt _this = *this;
  448. _this.chunkshiftLeft(c);
  449. return _this;
  450. }
  451. inline BigInt chunkshiftRight(int c)const{
  452. if(c < 0)return chunkshiftLeft(-c);
  453. BigInt _this = *this;
  454. _this.chunkshiftRight(c);
  455. return _this;
  456. }
  457. inline BigInt add(const BigInt& o){
  458. BigInt ret = *this;
  459. ret.adda(o);
  460. return ret;
  461. }
  462. inline BigInt& adda(const BigInt& o){
  463. while(size() < o.size())data.push_front(0);
  464. bool carry = 0;
  465. auto it1 = rbegin();
  466. auto it2 = o.rbegin();
  467. while(it1 != rend() && it2 != o.rend()){
  468. carry = __builtin_uaddll_overflow(*it1, *it2 + carry, (unsigned long long*)(&(*it1)));
  469. carry |= (*it2 + carry) == 0 && *it2;
  470. it1++;
  471. it2++;
  472. }
  473. while(it1 != rend() && carry){
  474. carry = __builtin_uaddll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  475. ++it1;
  476. }
  477. if(carry)data.push_front(1);
  478. return *this;
  479. }
  480. inline BigInt& suba(const BigInt& o){
  481. assert((*this) >= (o));
  482. bool carry = 0;
  483. auto it1 = rbegin();
  484. auto it2 = o.rbegin();
  485. while(it1 != rend() && it2 != o.rend()){
  486. carry = __builtin_usubll_overflow(*it1 - carry, *it2, (unsigned long long*)(&(*it1)));
  487. if(!carry)carry = ((*it1 - carry) == std::numeric_limits<uint64_t>::max());
  488. ++it1;++it2;
  489. }
  490. while(it1 != rend() && carry){
  491. carry = __builtin_usubll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  492. ++it1;
  493. }
  494. return *this;
  495. }
  496. inline BigInt& moda(const BigInt& o){
  497. if(o > *this)return *this;
  498. while(true){
  499. BigInt mo = o;
  500. int bd = bitDifference(o);
  501. mo.bitshiftLeft(bd);
  502. if(mo > *this)mo.bitshiftRight(1);
  503. this->suba(mo);
  504. if(*this < o)break;
  505. }
  506. return *this;
  507. }
  508. inline BigInt mult(const BigInt& o)const{
  509. BigInt result(size() + o.size() + 1,0);
  510. BigInt temp(size() + o.size() + 1,0);
  511. int p = 0;
  512. for(auto it1 = rbegin();it1 != rend();it1++){
  513. auto it = temp.rbegin();
  514. lui carry = 0;
  515. for(auto it2 = o.rbegin();it2 != o.rend();it2++){
  516. lui prod = ((lui)*it1) * (*it2);
  517. prod += carry;
  518. *(it++) = (uint64_t)prod;
  519. carry = (prod >> 64);
  520. }
  521. if(carry)(*it) += carry;
  522. temp.chunkshiftLeft(p++);
  523. result.adda(temp);
  524. temp.setZero();
  525. }
  526. result.trim();
  527. return result;
  528. }
  529. inline std::string rawString(){
  530. std::string s = "";
  531. bool flag;
  532. for(unsigned int i = 0;i < size();i++){
  533. if(data[i])flag = true;
  534. if(flag);
  535. s += std::to_string(data[i]);
  536. if(i < size() - 1)s += "|";
  537. }
  538. return s;
  539. }
  540. inline std::string bitString(){
  541. auto it = begin();
  542. std::string ret = "";
  543. std::cout << size() << "; " << std::flush;
  544. while(it != end()){
  545. std::bitset<64> bits(*it);
  546. for(unsigned int i = 0;i < 64;i++){
  547. ret += chars.at(((*it) & (1ULL << (64 - i))) != 0);
  548. }
  549. ++it;
  550. }
  551. return ret;
  552. }
  553. inline std::string toString(){
  554. std::deque<char> c_str;
  555. const uint64_t q = 1000000000000000000ULL;
  556. //c_str.reserve(size() * 19);
  557. BigInt diver = *this;
  558. while(!diver.isZero()){
  559. std::string frac = std::to_string(diver.mod(q));
  560. int a = 0;
  561. for(auto it = frac.rbegin();it != frac.rend();it++){
  562. c_str.push_front(*it);
  563. a++;
  564. }
  565. while(a < 18){
  566. c_str.push_front('0');
  567. a++;
  568. }
  569. diver.div(q);
  570. }
  571. auto it = c_str.begin();
  572. while(*(it) == '0')++it;
  573. return std::string(it, c_str.end());
  574. }
  575. inline std::string toString(unsigned int base){
  576. if(isZero())return std::string("0");
  577. std::vector<char> c_str;
  578. c_str.reserve(size() * (unsigned int)(64.0 * std::log(2) / std::log((double)base)));
  579. BigInt diver = *this;
  580. while(!diver.isZero()){
  581. c_str.push_back(chars.at(diver.mod(base)));
  582. diver.div(base);
  583. }
  584. return std::string(c_str.rbegin(), c_str.rend());
  585. }
  586. };
  587. namespace std{
  588. template<>
  589. struct hash<BigInt>{
  590. size_t operator()(const BigInt& o){
  591. size_t ret = 0;
  592. std::for_each(o.begin(), o.end(), [&ret](const uint64_t& ui){ret ^= ui;});
  593. return ret;
  594. }
  595. };
  596. }
  597. #endif //BIGINT64_HPP