BigInt64.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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() * sizeof(uint64_t)){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. *it1 |= (*(it2 + 1) >> (64 - sh));
  298. ++it1;++it2;
  299. }
  300. *it1 = (*it2 << sh);
  301. ++it1;++it2;
  302. while(it1 != end()){
  303. *it1 = 0;
  304. ++it1;
  305. }
  306. return *this;
  307. }
  308. inline BigInt& bitshiftRight(int c){
  309. if(c < 0)return bitshiftLeft(-c);
  310. unsigned int sh = c % 64;
  311. unsigned int jmp = c / 64;
  312. if((unsigned int)jmp >= size() * sizeof(uint64_t)){std::fill(begin(),end(),0);return *this;}
  313. auto it1 = rbegin();
  314. auto it2 = it1 + jmp;
  315. auto beforeRend = rend() - 1;
  316. while(it2 != beforeRend){
  317. *it1 = (*it2 >> sh);
  318. *it1 |= (*(it2 + 1) << (64 - sh));
  319. ++it1;++it2;
  320. }
  321. *it1 = (*it2 >> sh);
  322. ++it1;++it2;
  323. while(it1 != rend()){
  324. *it1 = 0;
  325. ++it1;
  326. }
  327. return *this;
  328. }
  329. inline BigInt& chunkshiftLeft(int c){
  330. if(c < 0)return chunkshiftRight(-c);
  331. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  332. auto it1 = data.begin();
  333. auto it2 = it1 + c;
  334. while(it2 != data.end())*(it1++) = *(it2++);
  335. while(it1 != data.end())*(it1++) = 0;
  336. return *this;
  337. }
  338. inline BigInt& chunkshiftRight(int c){
  339. if(c < 0)return chunkshiftLeft(-c);
  340. if((unsigned int)c >= size()){std::fill(begin(),end(),0);return *this;}
  341. auto it1 = data.rbegin();
  342. auto it2 = it1 + c;
  343. while(it2 != data.rend())*(it1++) = *(it2++);
  344. while(it1 != data.rend())*(it1++) = 0;
  345. return *this;
  346. }
  347. inline BigInt bitshiftLeft(int c)const{
  348. if(c < 0)return bitshiftRight(-c);
  349. BigInt _this = *this;
  350. _this.bitshiftLeft(c);
  351. return _this;
  352. }
  353. inline BigInt bitshiftRight(int c)const{
  354. if(c < 0)return bitshiftLeft(-c);
  355. BigInt _this = *this;
  356. _this.bitshiftRight(c);
  357. return _this;
  358. }
  359. inline BigInt chunkshiftLeft(int c)const{
  360. if(c < 0)return chunkshiftRight(-c);
  361. BigInt _this = *this;
  362. _this.chunkshiftLeft(c);
  363. return _this;
  364. }
  365. inline BigInt chunkshiftRight(int c)const{
  366. if(c < 0)return chunkshiftLeft(-c);
  367. BigInt _this = *this;
  368. _this.chunkshiftRight(c);
  369. return _this;
  370. }
  371. inline BigInt add(const BigInt& o){
  372. BigInt ret = *this;
  373. ret.adda(o);
  374. return ret;
  375. }
  376. inline BigInt& adda(const BigInt& o){
  377. while(size() < o.size())data.push_front(0);
  378. bool carry = 0;
  379. auto it1 = rbegin();
  380. auto it2 = o.rbegin();
  381. while(it1 != rend() && it2 != o.rend()){
  382. carry = __builtin_uaddll_overflow(*it1, *it2 + carry, (unsigned long long*)(&(*it1)));
  383. carry |= (*it2 + carry) == 0 && *it2;
  384. it1++;
  385. it2++;
  386. }
  387. while(it1 != rend() && carry){
  388. carry = __builtin_uaddll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  389. ++it1;
  390. }
  391. if(carry)data.push_front(1);
  392. return *this;
  393. }
  394. inline BigInt& suba(const BigInt& o){
  395. assert((*this) >= (o));
  396. bool carry = 0;
  397. auto it1 = rbegin();
  398. auto it2 = o.rbegin();
  399. while(it1 != rend() && it2 != o.rend()){
  400. carry = __builtin_usubll_overflow(*it1 - carry, *it2, (unsigned long long*)(&(*it1)));
  401. if(!carry)carry = ((*it1 - carry) == std::numeric_limits<uint64_t>::max());
  402. ++it1;++it2;
  403. }
  404. while(it1 != rend() && carry){
  405. carry = __builtin_usubll_overflow(*it1, carry, (unsigned long long*)(&(*it1)));
  406. ++it1;
  407. }
  408. return *this;
  409. }
  410. inline BigInt mult(const BigInt& o)const{
  411. BigInt result(size() + o.size() + 1,0);
  412. BigInt temp(size() + o.size() + 1,0);
  413. int p = 0;
  414. for(auto it1 = rbegin();it1 != rend();it1++){
  415. auto it = temp.rbegin();
  416. lui carry = 0;
  417. for(auto it2 = o.rbegin();it2 != o.rend();it2++){
  418. lui prod = ((lui)*it1) * (*it2);
  419. prod += carry;
  420. *(it++) = (uint64_t)prod;
  421. carry = (prod >> 64);
  422. }
  423. if(carry)(*it) += carry;
  424. temp.chunkshiftLeft(p++);
  425. result.adda(temp);
  426. temp.setZero();
  427. }
  428. result.trim();
  429. return result;
  430. }
  431. inline std::string rawString(){
  432. std::string s = "";
  433. bool flag;
  434. for(unsigned int i = 0;i < size();i++){
  435. if(data[i])flag = true;
  436. if(flag);
  437. s += std::to_string(data[i]);
  438. if(i < size() - 1)s += "|";
  439. }
  440. return s;
  441. }
  442. inline std::string bitString(){
  443. auto it = begin();
  444. std::string ret = "";
  445. std::cout << size() << "; " << std::flush;
  446. while(it != end()){
  447. std::bitset<64> bits(*it);
  448. for(unsigned int i = 0;i < 64;i++){
  449. ret += chars.at(((*it) & (1ULL << (64 - i))) != 0);
  450. }
  451. ++it;
  452. }
  453. return ret;
  454. }
  455. inline std::string toString(){
  456. std::deque<char> c_str;
  457. const uint64_t q = 1000000000000000000ULL;
  458. //c_str.reserve(size() * 19);
  459. BigInt diver = *this;
  460. while(!diver.isZero()){
  461. std::string frac = std::to_string(diver.mod(q));
  462. int a = 0;
  463. for(auto it = frac.rbegin();it != frac.rend();it++){
  464. c_str.push_front(*it);
  465. a++;
  466. }
  467. while(a < 18){
  468. c_str.push_front('0');
  469. a++;
  470. }
  471. diver.div(q);
  472. }
  473. auto it = c_str.begin();
  474. while(*(it) == '0')++it;
  475. return std::string(it, c_str.end());
  476. }
  477. inline std::string toString(unsigned int base){
  478. std::vector<char> c_str;
  479. c_str.reserve(size() * (unsigned int)(64.0 * std::log(2) / std::log((double)base)));
  480. BigInt diver = *this;
  481. while(!diver.isZero()){
  482. c_str.push_back(chars.at(diver.mod(base)));
  483. diver.div(base);
  484. }
  485. return std::string(c_str.rbegin(), c_str.rend());
  486. }
  487. };
  488. namespace std{
  489. template<>
  490. struct hash<BigInt>{
  491. size_t operator()(const BigInt& o){
  492. size_t ret = 0;
  493. std::for_each(o.begin(), o.end(), [&ret](const uint64_t& ui){ret ^= ui;});
  494. return ret;
  495. }
  496. };
  497. }
  498. #endif //BIGINT64_HPP