BigInt.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #define iszero(t) (t.len==1&&t.s[0]==0)
  2. #define setlen(l,t) t.len=l;while(t.len>1&&t.s[t.len-1]==0) t.len--
  3. const int maxlen=100;
  4. struct bigint {
  5. int len, s[maxlen];
  6. bigint() { *this = 0; }
  7. bigint(int a) { *this = a; }
  8. bigint(const char *a) { *this = a; }
  9. bigint operator=(int);
  10. bigint operator=(const char *);
  11. bigint operator=(const bigint &); //Optional
  12. friend ostream &operator<<(ostream &, const bigint &);
  13. bigint operator+(const bigint &);
  14. bigint operator-(const bigint &);
  15. bigint operator*(const bigint &);
  16. bigint operator/(const bigint &); //Require - cmp
  17. bigint operator%(const bigint &); //Require - cmp
  18. static int cmp(const bigint &, const bigint &);
  19. static bigint sqrt(const bigint &); //Require - * cmp
  20. };
  21. bigint bigint::operator=(int a) {
  22. len = 0;
  23. do {
  24. s[len++] = a % 10;
  25. a /= 10;
  26. } while (a > 0);
  27. return *this;
  28. }
  29. bigint bigint::operator=(const char *a) {
  30. len = strlen(a);
  31. for (int i = 0; i < len; i++) s[i] = a[len - i - 1] - '0';
  32. return *this;
  33. }
  34. bigint bigint::operator=(const bigint &a) {
  35. len = a.len;
  36. memcpy(s, a.s, sizeof(*s) * len);
  37. return *this;
  38. }
  39. ostream& operator<<(ostream &os,const bigint &a) {
  40. for (int i = a.len - 1; i >= 0; i--) os << a.s[i];
  41. return os;
  42. }
  43. bigint bigint::operator+(const bigint &a) {
  44. bigint b;
  45. b.s[b.len = max(len, a.len)] = 0;
  46. for (int i = 0; i < b.len; i++) b.s[i] = (i < len ? s[i] : 0) + (i < a.len ? a.s[i] : 0);
  47. for (int i = 0; i < b.len; i++)
  48. if (b.s[i] >= 10) {
  49. b.s[i] -= 10;
  50. b.s[i + 1]++;
  51. }
  52. if (b.s[b.len]) b.len++;
  53. return b;
  54. }
  55. //Make sure *this>=a
  56. bigint bigint::operator-(const bigint &a) {
  57. bigint b;
  58. for (int i = 0; i < len; i++) b.s[i] = s[i] - (i < a.len ? a.s[i] : 0);
  59. for (int i = 0; i < len; i++)
  60. if (b.s[i] < 0) {
  61. b.s[i] += 10;
  62. b.s[i + 1]--;
  63. }
  64. setlen(len, b);
  65. return b;
  66. }
  67. bigint bigint::operator*(const bigint &a) {
  68. bigint b;
  69. memset(b.s, 0, sizeof(*s) * (len + a.len + 1));
  70. for (int i = 0; i < len; i++)
  71. for (int j = 0; j < a.len; j++) b.s[i + j] += s[i] * a.s[j];
  72. for (int i = 0; i < len + a.len; i++) {
  73. b.s[i + 1] += b.s[i] / 10;
  74. b.s[i] %= 10;
  75. }
  76. setlen(len + a.len + 1, b);
  77. return b;
  78. }
  79. bigint bigint::operator/(const bigint &a) {
  80. bigint b, c;
  81. for (int i = len - 1; i >= 0; i--) {
  82. if (!iszero(b)) {
  83. for (int j = b.len; j > 0; j--) b.s[j] = b.s[j - 1];
  84. b.len++;
  85. }
  86. b.s[0] = s[i];
  87. c.s[i] = 0;
  88. while (cmp(b, a) >= 0) {
  89. b = b - a;
  90. c.s[i]++;
  91. }
  92. }
  93. setlen(len, c);
  94. return c;
  95. }
  96. bigint bigint::operator%(const bigint &a) {
  97. bigint b;
  98. for (int i = len - 1; i >= 0; i--) {
  99. if (!iszero(b)) {
  100. for (int j = b.len; j > 0; j--) b.s[j] = b.s[j - 1];
  101. b.len++;
  102. }
  103. b.s[0] = s[i];
  104. while (cmp(b, a) >= 0) b = b - a;
  105. }
  106. return b;
  107. }
  108. int bigint::cmp(const bigint &a,const bigint &b) {
  109. if (a.len < b.len) return -1;
  110. else if (a.len > b.len) return 1;
  111. for (int i = a.len - 1; i >= 0; i--)
  112. if (a.s[i] != b.s[i]) return a.s[i] - b.s[i];
  113. return 0;
  114. }
  115. bigint bigint::sqrt(const bigint &a) {
  116. int n = (a.len - 1) / 2, p;
  117. bigint b, d;
  118. b.len = n + 1;
  119. for (int i = n; i >= 0; i--) {
  120. if (!iszero(d)) {
  121. for (int j = d.len + 1; j > 1; j--) d.s[j] = d.s[j - 2];
  122. d.s[0] = a.s[i * 2];
  123. d.s[1] = a.s[i * 2 + 1];
  124. d.len += 2;
  125. }
  126. else d = a.s[i * 2] + (i * 2 + 1 < a.len ? a.s[i * 2 + 1] * 10 : 0);
  127. bigint c;
  128. c.s[1] = 0;
  129. for (int j = 1; j <= n - i; j++) {
  130. c.s[j] += b.s[i + j] << 1;
  131. if (c.s[j] >= 10) {
  132. c.s[j + 1] = 1;
  133. c.s[j] -= 10;
  134. } else c.s[j + 1] = 0;
  135. }
  136. c.len = n - i + 1 + c.s[n - i + 1];
  137. for (p = 1; ; p++) {
  138. c.s[0] = p;
  139. if (cmp(d, c * p) < 0) break;
  140. }
  141. b.s[i] = c.s[0] = p - 1;
  142. d = d - c * (p - 1);
  143. }
  144. return b;
  145. }