BigInt64.hpp 13 KB

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