BigInt.hpp 2.9 KB

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