BigInt.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef BIG_INT_HPP
  2. #define BIG_INT_HPP
  3. #include "semi_bitset.hpp"
  4. #include <cstdlib>
  5. #include <string>
  6. #include <iostream>
  7. #include <cassert>
  8. template<size_t size = dynamic>
  9. class BigInt{
  10. private:
  11. int signum = 1;
  12. semi_bitset<size> bits;
  13. public:
  14. inline BigInt() : bits(1){}
  15. inline BigInt(size_t _size, bool dummy) : bits(_size){
  16. }
  17. inline BigInt(int o) : BigInt(2,false){
  18. if(o < 0){signum = -1;o *= -1;}
  19. bits.data[bits.length() - 1] = (unsigned int)o;
  20. }
  21. inline BigInt(unsigned int o) : BigInt(2,false){
  22. bits.data[bits.length() - 1] = o;
  23. }
  24. inline BigInt(long long o) : BigInt(3,false){
  25. if(o < 0){signum = -1;o = 0ULL - o;}
  26. bits.data[bits.length() - 1] = (o & lower_half);
  27. bits.data[bits.length() - 2] = (o >> 32);
  28. }
  29. inline BigInt(unsigned long long o) : BigInt(3,false){
  30. bits.data[bits.length() - 1] = (o & lower_half);
  31. bits.data[bits.length() - 2] = (o >> 32);
  32. }
  33. template<std::size_t osize>
  34. inline BigInt<size> add(const BigInt<osize>& o)const{
  35. static_assert(size == osize);
  36. BigInt<size> ret;
  37. uint64_t carry = 0;
  38. for(size_t i = size - 1;i >= 0;i--){
  39. uint64_t res = (*this)[i] + o[i] + carry;
  40. carry = (res >> 32);
  41. ret[i] = res & lower_half;
  42. if(i == 0)break;
  43. }
  44. return ret;
  45. }
  46. template<size_t osize>
  47. inline BigInt<size + osize> mult(const BigInt<osize>& o)const{
  48. BigInt<size + osize> ret;
  49. uint64_t mat[size][size] = {0};
  50. uint64_t carry = 0;
  51. for(size_t i = 0;i < size;i++){
  52. for(size_t ih = size - 1;ih != ~(0ULL);ih--){
  53. mat[size - i - 1][ih] = (*this)[i] * o[ih] + carry;
  54. carry = mat[size - i - 1][ih] >> 32;
  55. mat[size - i - 1][ih] &= lower_half;
  56. if(ih == 0)break;
  57. }
  58. }
  59. for(size_t i = 0;i < size;i++){
  60. for(size_t ih = 0;ih < size + osize;ih++){
  61. std::cout << mat[i][ih] << " ";
  62. }
  63. std::cout << std::endl;
  64. }
  65. carry = 0;
  66. for(std::int64_t i = size - 1;i > -((std::int64_t)size);i--){
  67. uint64_t accum = 0;
  68. for(std::int64_t ih = 0;ih < size;ih++){
  69. accum += ((i + ih) >= size || (i + ih) < 0) ? 0 : mat[ih][i + ih];
  70. }
  71. accum += carry;
  72. ret[i + osize] = accum & lower_half;
  73. //std::cout << accum << " ";
  74. carry = accum >> 32;
  75. if(i == 0)break;
  76. }
  77. return ret;
  78. }
  79. inline BigInt<size> div(unsigned int o)const{
  80. uint64_t carry = 0;
  81. BigInt<size> ret;
  82. for(size_t i = 0;i < size;i++){
  83. uint64_t res = ((*this)[i] + (carry << 32)) / o;
  84. carry = (*this)[i] % o;
  85. ret[i] = res;
  86. }
  87. return ret;
  88. }
  89. inline unsigned int mod(unsigned int o)const{
  90. uint64_t carry = 0;
  91. for(size_t i = 0;i < size;i++){
  92. carry = (this->operator[](i) + (carry << 32)) % o;
  93. }
  94. return (unsigned int)(carry & lower_half);
  95. }
  96. inline std::uint64_t& operator[](size_t i){
  97. return bits.data[i];
  98. }
  99. inline const std::uint64_t& operator[](size_t i)const{
  100. return bits.data[i];
  101. }
  102. inline bool isZero()const{
  103. for(size_t i = 0;i < size;i++){
  104. if(this->operator[](i))return false;
  105. }
  106. return true;
  107. }
  108. };
  109. template<size_t osize>
  110. inline std::ostream& operator<<(std::ostream& s, const BigInt<osize>& o){
  111. std::string a = "";
  112. BigInt<osize> dis = o;
  113. while(!dis.isZero()){
  114. a = std::to_string(dis.mod(10)) + a;
  115. dis = dis.div(10);
  116. }
  117. return s << a;
  118. }
  119. #endif