BigInt64.hpp 13 KB

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