BigInt.hpp 2.7 KB

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