BigInt64.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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){auto it = data.begin();while(it != data.end())*(it++) = 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 void trim(){
  252. while(data[0] == 0 && data.size() != 0){
  253. data.pop_front();
  254. }
  255. }
  256. inline BigInt div(uint64_t d)const{
  257. BigInt ret = *this;
  258. lui carry = 0;
  259. for(auto it = ret.data.begin();it != ret.data.end();it++){
  260. lui temp = (lui)*it;
  261. temp += carry;
  262. carry = temp % d;
  263. *it = (uint64_t)(temp / d);
  264. carry <<= 64;
  265. }
  266. return ret;
  267. }
  268. inline BigInt& div(uint64_t d){
  269. lui carry = 0;
  270. for(auto it = data.begin();it != data.end();it++){
  271. lui temp = (lui)*it;
  272. temp += carry;
  273. carry = temp % d;
  274. *it = (uint64_t)(temp / d);
  275. carry <<= 64;
  276. }
  277. return *this;
  278. }
  279. inline uint64_t mod(uint64_t m)const{
  280. lui carry = 0;
  281. for(auto it = data.begin();it != data.end();it++){
  282. carry <<= 64;
  283. carry = (*it + carry) % m;
  284. }
  285. return carry;
  286. }
  287. inline BigInt& bitshiftLeft(int c){
  288. if(c < 0)return bitshiftRight(-c);
  289. unsigned int sh = c % 64;
  290. unsigned int jmp = c / 64;
  291. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  292. auto it1 = begin();
  293. auto it2 = it1 + jmp;
  294. auto beforeEnd = end() - 1;
  295. while(it2 != beforeEnd){
  296. *it1 = (*it2 << sh);
  297. if(sh)
  298. *it1 |= (*(it2 + 1) >> (64 - sh));
  299. ++it1;++it2;
  300. }
  301. *it1 = (*it2 << sh);
  302. ++it1;++it2;
  303. while(it1 != end()){
  304. *it1 = 0;
  305. ++it1;
  306. }
  307. return *this;
  308. }
  309. inline BigInt& bitshiftRight(int c){
  310. if(c < 0)return bitshiftLeft(-c);
  311. unsigned int sh = c % 64;
  312. unsigned int jmp = c / 64;
  313. if((unsigned int)jmp >= size()){std::fill(begin(),end(),0);return *this;}
  314. auto it1 = rbegin();
  315. auto it2 = it1 + jmp;
  316. auto beforeRend = rend() - 1;
  317. while(it2 != beforeRend){
  318. *it1 = (*it2 >> sh);
  319. if(sh)
  320. *it1 |= (*(it2 + 1) << (64 - sh));
  321. ++it1;++it2;
  322. }
  323. *it1 = (*it2 >> sh);
  324. ++it1;++it2;
  325. while(it1 != rend()){
  326. *it1 = 0;
  327. ++it1;
  328. }
  329. return *this;
  330. }
  331. BigInt operator<<(int c)const{
  332. BigInt ret = *this;
  333. ret.bitshiftLeft(c);
  334. return ret;
  335. }
  336. BigInt operator>>(int c)const{
  337. BigInt ret = *this;
  338. ret.bitshiftRight(c);
  339. return ret;
  340. }
  341. BigInt& operator<<=(int c){
  342. return bitshiftLeft(c);
  343. }
  344. BigInt& operator>>=(int c){
  345. return bitshiftRight(c);
  346. }
  347. int bitDifference(const BigInt& o){
  348. int pos1(-1),pos2(-1);
  349. auto it1 = begin();
  350. auto it2 = o.begin();
  351. while(it1 != end()){
  352. if(*it1 == 0)
  353. pos1 += 64;
  354. else{
  355. pos1 += __builtin_clzll(*it1);
  356. break;
  357. }
  358. ++it1;
  359. }
  360. if(pos1 == -1)pos1 = size() * 64;
  361. while(it2 != end()){
  362. if(*it2 == 0)
  363. pos2 += 64;
  364. else{
  365. pos2 += __builtin_clzll(*it2);
  366. break;
  367. }
  368. ++it2;
  369. }
  370. if(pos1 == -1)pos1 = o.size() * 64;
  371. int sizediff = ((signed int)size()) - ((signed int)o.size());
  372. return pos2 - pos1 + sizediff * 64;
  373. }
  374. inline BigInt& chunkshiftLeft(int c){
  375. if(c < 0)return chunkshiftRight(-c);
  376. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  377. auto it1 = data.begin();
  378. auto it2 = it1 + c;
  379. while(it2 != data.end())*(it1++) = *(it2++);
  380. while(it1 != data.end())*(it1++) = 0;
  381. return *this;
  382. }
  383. inline BigInt& chunkshiftRight(int c){
  384. if(c < 0)return chunkshiftLeft(-c);
  385. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  386. auto it1 = data.rbegin();
  387. auto it2 = it1 + c;
  388. while(it2 != data.rend())*(it1++) = *(it2++);
  389. while(it1 != data.rend())*(it1++) = 0;
  390. return *this;
  391. }
  392. inline BigInt bitshiftLeft(int c)const{
  393. if(c < 0)return bitshiftRight(-c);
  394. BigInt _this = *this;
  395. _this.bitshiftLeft(c);
  396. return _this;
  397. }
  398. inline BigInt bitshiftRight(int c)const{
  399. if(c < 0)return bitshiftLeft(-c);
  400. BigInt _this = *this;
  401. _this.bitshiftRight(c);
  402. return _this;
  403. }
  404. inline BigInt chunkshiftLeft(int c)const{
  405. if(c < 0)return chunkshiftRight(-c);
  406. BigInt _this = *this;
  407. _this.chunkshiftLeft(c);
  408. return _this;
  409. }
  410. inline BigInt chunkshiftRight(int c)const{
  411. if(c < 0)return chunkshiftLeft(-c);
  412. BigInt _this = *this;
  413. _this.chunkshiftRight(c);
  414. return _this;
  415. }
  416. inline BigInt add(const BigInt& o){
  417. BigInt ret = *this;
  418. ret.adda(o);
  419. return ret;
  420. }
  421. inline BigInt& adda(const BigInt& o){
  422. while(size() < o.size())data.push_front(0);
  423. bool carry = 0;
  424. auto it1 = rbegin();
  425. auto it2 = o.rbegin();
  426. while(it1 != rend() && it2 != o.rend()){
  427. carry = __builtin_uaddll_overflow(*it1, *it2 + carry, (unsigned long long*)(&(*it1)));
  428. carry |= (*it2 + carry) == 0 && *it2;
  429. it1++;
  430. it2++;
  431. }
  432. while(it1 != rend() && carry){
  433. carry = __builtin_uaddll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  434. ++it1;
  435. }
  436. if(carry)data.push_front(1);
  437. return *this;
  438. }
  439. inline BigInt& suba(const BigInt& o){
  440. assert((*this) >= (o));
  441. bool carry = 0;
  442. auto it1 = rbegin();
  443. auto it2 = o.rbegin();
  444. while(it1 != rend() && it2 != o.rend()){
  445. carry = __builtin_usubll_overflow(*it1 - carry, *it2, (unsigned long long*)(&(*it1)));
  446. if(!carry)carry = ((*it1 - carry) == std::numeric_limits<uint64_t>::max());
  447. ++it1;++it2;
  448. }
  449. while(it1 != rend() && carry){
  450. carry = __builtin_usubll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  451. ++it1;
  452. }
  453. return *this;
  454. }
  455. inline BigInt moda(){
  456. }
  457. inline BigInt mult(const BigInt& o)const{
  458. BigInt result(size() + o.size() + 1,0);
  459. BigInt temp(size() + o.size() + 1,0);
  460. int p = 0;
  461. for(auto it1 = rbegin();it1 != rend();it1++){
  462. auto it = temp.rbegin();
  463. lui carry = 0;
  464. for(auto it2 = o.rbegin();it2 != o.rend();it2++){
  465. lui prod = ((lui)*it1) * (*it2);
  466. prod += carry;
  467. *(it++) = (uint64_t)prod;
  468. carry = (prod >> 64);
  469. }
  470. if(carry)(*it) += carry;
  471. temp.chunkshiftLeft(p++);
  472. result.adda(temp);
  473. temp.setZero();
  474. }
  475. result.trim();
  476. return result;
  477. }
  478. inline std::string rawString(){
  479. std::string s = "";
  480. bool flag;
  481. for(unsigned int i = 0;i < size();i++){
  482. if(data[i])flag = true;
  483. if(flag);
  484. s += std::to_string(data[i]);
  485. if(i < size() - 1)s += "|";
  486. }
  487. return s;
  488. }
  489. inline std::string bitString(){
  490. auto it = begin();
  491. std::string ret = "";
  492. std::cout << size() << "; " << std::flush;
  493. while(it != end()){
  494. std::bitset<64> bits(*it);
  495. for(unsigned int i = 0;i < 64;i++){
  496. ret += chars.at(((*it) & (1ULL << (64 - i))) != 0);
  497. }
  498. ++it;
  499. }
  500. return ret;
  501. }
  502. inline std::string toString(){
  503. std::deque<char> c_str;
  504. const uint64_t q = 1000000000000000000ULL;
  505. //c_str.reserve(size() * 19);
  506. BigInt diver = *this;
  507. while(!diver.isZero()){
  508. std::string frac = std::to_string(diver.mod(q));
  509. int a = 0;
  510. for(auto it = frac.rbegin();it != frac.rend();it++){
  511. c_str.push_front(*it);
  512. a++;
  513. }
  514. while(a < 18){
  515. c_str.push_front('0');
  516. a++;
  517. }
  518. diver.div(q);
  519. }
  520. auto it = c_str.begin();
  521. while(*(it) == '0')++it;
  522. return std::string(it, c_str.end());
  523. }
  524. inline std::string toString(unsigned int base){
  525. std::vector<char> c_str;
  526. c_str.reserve(size() * (unsigned int)(64.0 * std::log(2) / std::log((double)base)));
  527. BigInt diver = *this;
  528. while(!diver.isZero()){
  529. c_str.push_back(chars.at(diver.mod(base)));
  530. diver.div(base);
  531. }
  532. return std::string(c_str.rbegin(), c_str.rend());
  533. }
  534. };
  535. namespace std{
  536. template<>
  537. struct hash<BigInt>{
  538. size_t operator()(const BigInt& o){
  539. size_t ret = 0;
  540. std::for_each(o.begin(), o.end(), [&ret](const uint64_t& ui){ret ^= ui;});
  541. return ret;
  542. }
  543. };
  544. }
  545. #endif //BIGINT64_HPP