大数模板

1.0:

View Code

1.1(加入了字符串和整数构造大数):

 1 struct BigNum{

 2         #define maxlen 10

 3         #define memc(a, b) memcpy(a, b, sizeof(b))

 4         #define mem0(a) memset(a, 0, sizeof(a))

 5         typedef __int64 Num[maxlen + 2];

 6         Num num;

 7         char s[maxlen + 2];

 8         BigNum operator+(BigNum num2) {

 9                 BigNum ans;

10                 mem0(ans.num);

11                 for(int i = 1; i <= maxlen; i++) {

12                         ans.num[i] += num[i] + num2.num[i];

13                         ans.num[i + 1] += ans.num[i] / (int)1e9;

14                         ans.num[i] %= (int)1e9;

15                 }

16                 return ans;

17         }

18         BigNum operator*(BigNum num2) {

19                 BigNum ans;

20                 mem0(ans.num);

21                 for(int i = 1; i <= maxlen; i++) {

22                         for(int j = 1; j <= maxlen; j++) {

23                                 if(i + j - 1 <= maxlen) {

24                                         ans.num[i + j - 1] += num[i] * num2.num[j];

25                                         ans.num[i + j] += ans.num[i + j - 1] / (int)1e9;

26                                         ans.num[i + j - 1] %= (int)1e9;

27                                 }

28                         }

29                 }

30                 return ans;

31         }

32         void convert() {

33                 int len = strlen(s), cnt = 0;

34                 for(int i = len - 1; i >= 0; i -= 9) {

35                         int p = 0, x = 0, t = 1;

36                         while(i - p >= 0 && p < 9) {

37                                 x += t * (s[i - p] - '0');

38                                 p++;

39                                 t *= 10;

40                         }

41                         num[++cnt] = x;

42                 }

43         }

44         void inp() {

45                 mem0(num);

46                 scanf("%s", s);

47                 convert();

48         }

49         void outp() {

50                 int p = 1;

51                 for(int i = maxlen; i >= 1; i--) {

52                         if(num[i]) {

53                                 p = i;

54                                 break;

55                         }

56                 }

57                 cout<< num[p];

58                 while(--p) {

59                         int a[9] = {0}, x = num[p];

60                         for(int i = 0; i < 9; i++) {

61                                 a[i] = x % 10;

62                                 x /= 10;

63                         }

64                         for(int i = 8; i >= 0; i--) {

65                                 printf("%d", a[i]);

66                         }

67                 }

68         }

69         void inttostr(int num) {

70                 int cnt = 0;

71                 while(num) {

72                         s[cnt++] = num % 10 + '0';

73                         num /= 10;

74                 }

75                 s[cnt] = 0;

76                 for(int i = 0, j = cnt - 1; i < j; i++, j--) {

77                         swap(s[i], s[j]);

78                 }

79         }

80         BigNum(char str[]) {

81                 strcpy(s, str);

82                 mem0(num);

83                 convert();

84         }

85         BigNum(int n) {

86                 inttostr(n);

87                 mem0(num);

88                 convert();

89         }

90         BigNum(){}

91 };
View Code

 1.2(加入了整除,取模,减法,现在可以求两个大数的gcd了)

  1 struct BigNum{

  2         #define maxlen 10

  3         #define memc(a, b) memcpy(a, b, sizeof(b))

  4         #define mem0(a) memset(a, 0, sizeof(a))

  5         typedef LL Num[maxlen + 2];

  6         Num num;

  7         char s[maxlen + 2];

  8         int numCmp(BigNum a, BigNum b) {

  9                 for(int i = maxlen; i; i--) {

 10                         if(a.num[i] > b.num[i]) return 1;

 11                         if(a.num[i] < b.num[i]) return -1;

 12                 }

 13                 return 0;

 14         }

 15         bool operator==(BigNum num2) {

 16                 BigNum tmp;

 17                 memc(tmp.num, num);

 18                 return numCmp(tmp, num2) == 0;

 19         }

 20         bool operator<(BigNum num2) {

 21                 BigNum tmp;

 22                 memc(tmp.num, num);

 23                 return numCmp(tmp, num2) < 0;

 24         }

 25         bool operator>(BigNum num2) {

 26                 BigNum tmp;

 27                 memc(tmp.num, num);

 28                 return numCmp(tmp, num2) > 0;

 29         }

 30         BigNum operator+(BigNum num2) {

 31                 BigNum ans;

 32                 mem0(ans.num);

 33                 for(int i = 1; i <= maxlen; i++) {

 34                         ans.num[i] += num[i] + num2.num[i];

 35                         ans.num[i + 1] += ans.num[i] / (int)1e9;

 36                         ans.num[i] %= (int)1e9;

 37                 }

 38                 return ans;

 39         }

 40         BigNum operator-(BigNum num2) {

 41                 BigNum ans;

 42                 mem0(ans.num);

 43                 for(int i = 1; i <= maxlen; i++) {

 44                         ans.num[i] += (int)1e9 + num[i] - num2.num[i];

 45                         ans.num[i + 1] += ans.num[i] / (int)1e9 - 1;

 46                         ans.num[i] %= (int)1e9;

 47                 }

 48                 return ans;

 49         }

 50         BigNum operator*(BigNum num2) {

 51                 BigNum ans;

 52                 mem0(ans.num);

 53                 for(int i = 1; i <= maxlen; i++) {

 54                         for(int j = 1; j <= maxlen; j++) {

 55                                 if(i + j - 1 <= maxlen) {

 56                                         ans.num[i + j - 1] += num[i] * num2.num[j];

 57                                         ans.num[i + j] += ans.num[i + j - 1] / (int)1e9;

 58                                         ans.num[i + j - 1] %= (int)1e9;

 59                                 }

 60                         }

 61                 }

 62                 return ans;

 63         }

 64         BigNum operator/(BigNum num2) {

 65                 BigNum ans;

 66                 mem0(ans.num);

 67                 BigNum thisnum;

 68                 memc(thisnum.num, num);

 69                 for(int i = maxlen / 2; i; i--) {

 70                         int l = 0, r = (int)1e9 - 1;

 71                         while(l < r) {

 72                                 int m = (l + r + 1) >> 1;

 73                                 ans.num[i] = m;

 74                                 BigNum tmp = ans * num2;

 75                                 if(numCmp(tmp, thisnum) <= 0) l = m;

 76                                 else r = m - 1;

 77                         }

 78                         ans.num[i] = l;

 79                 }

 80                 return ans;

 81         }

 82         BigNum operator%(BigNum num2) {

 83                 BigNum ans;

 84                 memc(ans.num, num);

 85                 return ans - ans / num2 * num2;

 86         }

 87         void convert() {

 88                 int len = strlen(s), cnt = 0;

 89                 for(int i = len - 1; i >= 0; i -= 9) {

 90                         int p = 0, x = 0, t = 1;

 91                         while(i - p >= 0 && p < 9) {

 92                                 x += t * (s[i - p] - '0');

 93                                 p++;

 94                                 t *= 10;

 95                         }

 96                         num[++cnt] = x;

 97                 }

 98         }

 99         void inp() {

100                 mem0(num);

101                 scanf("%s", s);

102                 convert();

103         }

104         void outp() {

105                 int p = 1;

106                 for(int i = maxlen; i >= 1; i--) {

107                         if(num[i]) {

108                                 p = i;

109                                 break;

110                         }

111                 }

112                 cout<< num[p];

113                 while(--p) {

114                         int a[9] = {0}, x = num[p];

115                         for(int i = 0; i < 9; i++) {

116                                 a[i] = x % 10;

117                                 x /= 10;

118                         }

119                         for(int i = 8; i >= 0; i--) {

120                                 printf("%d", a[i]);

121                         }

122                 }

123         }

124         void inttostr(int num) {

125                 int cnt = 0;

126                 while(num) {

127                         s[cnt++] = num % 10 + '0';

128                         num /= 10;

129                 }

130                 s[cnt] = 0;

131                 for(int i = 0, j = cnt - 1; i < j; i++, j--) {

132                         swap(s[i], s[j]);

133                 }

134         }

135         BigNum(char str[]) {

136                 strcpy(s, str);

137                 mem0(num);

138                 convert();

139         }

140         BigNum(int n) {

141                 inttostr(n);

142                 mem0(num);

143                 convert();

144         }

145         BigNum(){}

146 };
View Code

 1.3(重写,加减乘,整除,取模, 带符号,2014.12.30)

  1 struct BigInt {

  2         #define MAXN 100

  3         #define DIGIT 8

  4         #define D_VAL 100000000

  5         #define mem0(a) memset(a, 0, sizeof(a))

  6         #define LL long long

  7         typedef int NUM[MAXN + 2];

  8         NUM num;

  9         bool flag;

 10         int cmp(const int a[], const int b[]) const {

 11                 for(int i = MAXN; i; i--) {

 12                         if(a[i] != b[i]) return a[i] - b[i];

 13                 }

 14                 return 0;

 15         }

 16         bool operator < (const BigInt _A) const {

 17                 return cmp(num, _A.num) < 0;

 18         }

 19         bool operator <= (const BigInt _A) const {

 20                 return cmp(num, _A.num) <= 0;

 21         }

 22         bool operator == (const BigInt _A) const {

 23                 return cmp(num, _A.num) == 0;

 24         }

 25         int &operator [] (int x) {

 26                 return num[x];

 27         }

 28         BigInt() {}

 29         BigInt(int x) {

 30                 mem0(num);

 31                 int c = 1, p = 1, v = 1;

 32                 if(x < 0) {

 33                         flag = 1;

 34                         x = -x;

 35                 }

 36                 else flag = 0;

 37                 while(x) {

 38                         num[p] += x % 10 * v;

 39                         if(c < DIGIT) v *= 10;

 40                         else v = 1;

 41                         p += c / DIGIT;

 42                         c = c % DIGIT + 1;

 43                         x /= 10;

 44                 }

 45         }

 46         BigInt(char s[]) {

 47                 mem0(num);

 48                 int len = strlen(s), c = 1, p = 1, v = 1, low = 0;

 49                 if(s[low] == '-') {

 50                         low++;

 51                         flag = 1;

 52                 }

 53                 else flag = 0;

 54                 for(int i = len - 1; i >= low; i--) {

 55                         num[p] += (s[i] - '0') * v;

 56                         if(c < DIGIT) v *= 10;

 57                         else v = 1;

 58                         p += c / DIGIT;

 59                         c = c % DIGIT + 1;

 60                 }

 61         }

 62         BigInt(int a[], int F) {

 63                 memcpy(num, a, sizeof(num));

 64                 flag = F;

 65         }

 66 

 67         void add(int a[], const int b[], const int c[]) const {

 68                 a[1] = 0;

 69                 for(int i = 1; i <= MAXN; i++) {

 70                         a[i] += b[i] + c[i];

 71                         a[i + 1] = a[i] / D_VAL;

 72                         a[i] %= D_VAL;

 73                 }

 74         }

 75         void sub(int a[], const int b[], const int c[]) const {

 76                 a[1] = 0;

 77                 for(int i = 1; i <= MAXN; i++) {

 78                         a[i] += b[i] - c[i] + D_VAL;

 79                         a[i + 1] = a[i] / D_VAL - 1;

 80                         a[i] %= D_VAL;

 81                 }

 82         }

 83         void mul(int a[], const int b[], const int c[]) const {

 84                 LL aa[MAXN + 2];

 85                 mem0(aa);

 86                 int P = MAXN, Q = MAXN;

 87                 while(b[P] == 0 && P) P--;

 88                 while(c[Q] == 0 && Q) Q--;

 89                 for(int i = 1; i <= P; i++) {

 90                         for(int j = 1; j <= Q; j++) {

 91                                 int pos = i + j - 1;

 92                                 if(pos <= MAXN) {

 93                                         aa[pos] += (LL)b[i] * c[j];

 94                                         aa[pos + 1] += aa[pos] / D_VAL;

 95                                         aa[pos] %= D_VAL;

 96                                 }

 97                         }

 98                 }

 99                 for(int i = 1; i <= MAXN; i++) a[i] = aa[i];

100         }

101         int div(const BigInt &a, const BigInt &b) const {

102                 int l = 1, r = D_VAL - 1;

103                 while(l < r) {

104                         int m = (l + r + 1) >> 1;

105                         if(BigInt(m) * b <= a) l = m;

106                         else r = m - 1;

107                 }

108                 return l;

109         }

110         void div(BigInt &a, const BigInt &b, const BigInt &c) const {

111                 BigInt rest(0);

112                 for(int i = MAXN; i; i--) {

113                         rest = BigInt(D_VAL) * rest;

114                         rest[1] = b.num[i];

115                         if(rest < c) {

116                                 a[i] = 0;

117                                 continue;

118                         }

119                         a[i] = div(rest, c);

120                         rest = rest - BigInt(a[i]) * c;

121                 }

122         }

123         BigInt operator + (const BigInt &_A) const {

124                 BigInt ans;

125                 int tmp = flag << 1 | _A.flag;

126                 ans.flag = 0;

127                 if(tmp == 0) add(ans.num, num, _A.num);

128                 if(tmp == 1) {

129                         if(cmp(num, _A.num) >= 0) sub(ans.num, num, _A.num);

130                         else {

131                                 sub(ans.num, _A.num, num);

132                                 ans.flag = 1;

133                         }

134                 }

135                 if(tmp == 2) {

136                         if(cmp(num, _A.num) <= 0) sub(ans.num, _A.num, num);

137                         else {

138                                 sub(ans.num, num, _A.num);

139                                 ans.flag = 1;

140                         }

141                 }

142                 if(tmp == 3) {

143                         ans.flag = 1;

144                         add(ans.num, num, _A.num);

145                 }

146                 return ans;

147         }

148         BigInt operator - (const BigInt &_A) const {

149                 BigInt tmp = _A;

150                 tmp.flag ^= 1;

151                 return *this + tmp;

152         }

153         BigInt operator * (const BigInt &_A) const {

154                 BigInt ans;

155                 ans.flag = flag ^ _A.flag;

156                 mul(ans.num, num, _A.num);

157                 return ans;

158         }

159         BigInt operator / (const BigInt &_A) const {

160                 BigInt ans;

161                 int tmp = flag << 1 | _A.flag;

162                 ans.flag = 0;

163                 if(tmp == 0) div(ans, *this, _A);

164                 if(tmp == 1) {

165                         div(ans, *this, _A);

166                         ans.flag = 1;

167                 }

168                 if(tmp == 2) {

169                         BigInt T = *this, U = _A;

170                         T.flag = 0;

171                         U.flag = 1;

172                         T = T + _A - BigInt(1);

173                         return T / U;

174                 }

175                 if(tmp == 3) {

176                         BigInt T = *this, U = _A;

177                         T.flag = U.flag = 0;

178                         T = T + _A - BigInt(1);

179                         return T / U;

180                 }

181                 return ans;

182         }

183         BigInt operator % (const BigInt &_A) const {

184                 return *this - *this / _A * _A;

185         }

186         void OO(int x) const {

187                 int a[DIGIT] = {}, i = 0;

188                 while(x) {

189                         a[i++] = x % 10;

190                         x /= 10;

191                 }

192                 for(int i = DIGIT - 1; i >= 0; i--) {

193                         printf("%d", a[i]);

194                 }

195         }

196         void outp() const {

197                 if(flag) putchar('-');

198                 int pos = MAXN;

199                 while(num[pos] == 0 && pos > 1) pos--;

200                 cout << num[pos--];

201                 while(pos) OO(num[pos--]);

202         }

203         void printLine() const {

204                 outp();

205                 cout << endl;

206         }

207         void printDigitCount() {

208                 int p = MAXN, ans = 0;

209                 while(num[p] == 0 && p) p--;

210                 if(!p) {

211                         puts("1");

212                         return;

213                 }

214                 int tmp = num[p--];

215                 while(tmp) {

216                         ans++;

217                         tmp /= 10;

218                 }

219                 ans += DIGIT * p;

220                 printf("%d\n", ans);

221         }

222 };
View Code

 

你可能感兴趣的:(模板)