码迷,mamicode.com
首页 > 其他好文 > 详细

大数模板(加减乘除幂次开方)

时间:2018-09-16 16:11:04      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:模板   size   char   void   大数模板   art   sizeof   ==   i++   

很好用的模板,但当时做题的时候从哪里找的不知道了,原作看到知会我一声我补上hhh

  1 struct BigInteger
  2 {
  3     int len;
  4     int arg[500];
  5     BigInteger(int x = 0) {
  6         IntToBigInteger(x);
  7     }
  8     
  9     BigInteger(char *str) {
 10         CharToBigInteger(str);
 11     }
 12     void print()
 13     {
 14         for (int i = len - 1; i >= 0; i--) {
 15             cout << arg[i];
 16         }
 17         cout << endl;
 18     }
 19     void CharToBigInteger(char *str)
 20     {
 21         len = strlen(str);
 22         memset(arg, 0, sizeof(arg));
 23         for (int i = 0; i < len; i++) {
 24             arg[i] = str[len - i - 1] - 48;
 25         }
 26         while (len > 0 && arg[len - 1] == 0) {
 27             len--;
 28         }
 29     }
 30     void IntToBigInteger(int x)
 31     {
 32         len = 0;
 33         memset(arg, 0, sizeof(arg));
 34         do {
 35             arg[len++] = x % 10;
 36             x = x / 10;
 37         } while (x);
 38     }
 39     friend istream &operator>>(istream &in, BigInteger &w)
 40     {
 41         char str[1005];
 42         in >> str;
 43         w.CharToBigInteger(str);
 44         return in;
 45     }
 46     friend ostream& operator<<(ostream &out, BigInteger w)
 47     {
 48         for (int i = w.len - 1; i >= 0; i--) {
 49             out << w.arg[i];
 50         }
 51         return out;
 52     }
 53     friend bool operator==(BigInteger r, BigInteger w)
 54     {
 55         if (r.len != w.len) return false;
 56         int i = r.len - 1;
 57         while (i >= 0) {
 58             if (r.arg[i] != w.arg[i])
 59                 return false;
 60             i--;
 61         }
 62         return true;
 63     }
 64     friend bool operator<=(BigInteger r, BigInteger w)
 65     {
 66         if (r.len < w.len) return true;
 67         else if (w.len < r.len) return false;
 68         int i = r.len - 1;
 69         while (i >= 0) {
 70             if (r.arg[i] < w.arg[i])
 71                 return true;
 72             else if (r.arg[i] > w.arg[i])
 73                 return false;
 74             i--;
 75         }
 76         return true;
 77     }
 78     friend bool operator<=(int x, BigInteger w)
 79     {
 80         BigInteger r;
 81         r.IntToBigInteger(x);
 82         if (r.len < w.len) return true;
 83         else if (w.len < r.len) return false;
 84         int i = r.len - 1;
 85         while (i >= 0) {
 86             if (r.arg[i] < w.arg[i])
 87                 return true;
 88             else if (r.arg[i] > w.arg[i])
 89                 return false;
 90             i--;
 91         }
 92         return true;
 93     }
 94     friend bool operator<(BigInteger r, BigInteger w)
 95     {
 96         if (r.len < w.len) return true;
 97         else if (w.len < r.len) return false;
 98         int i = r.len - 1;
 99         while (i >= 0) {
100             if (r.arg[i] < w.arg[i])
101                 return true;
102             else if (r.arg[i] > w.arg[i])
103                 return false;
104             i--;
105         }
106         return false;
107     }
108     friend bool operator<(int x, BigInteger w)
109     {
110         BigInteger r;
111         r.IntToBigInteger(x);
112         if (r.len < w.len) return true;
113         else if (w.len < r.len)    return false;
114         int i = r.len - 1;
115         while (i >= 0) {
116             if (r.arg[i] < w.arg[i])
117                 return true;
118             else if (r.arg[i] > w.arg[i])
119                 return false;
120             i--;
121         }
122         return false;
123     }
124     friend BigInteger operator+(int r, BigInteger w) {
125         w.arg[0] += r;
126         int i = 0;
127         while (w.arg[i] >= 10) {
128             w.arg[i + 1] += w.arg[i] / 10;
129             w.arg[i] = w.arg[i] % 10;
130             i++;
131         }
132         if (w.arg[i]) i++;
133         w.len = i > w.len ? i : w.len;
134         return w;
135     }
136     friend BigInteger operator+(BigInteger w, int r)
137     {
138         w.arg[0] += r;
139         int i = 0;
140         while (w.arg[i] >= 10) {
141             w.arg[i + 1] += w.arg[i] / 10;
142             w.arg[i] = w.arg[i] % 10;
143             i++;
144         }
145         if (w.arg[i]) i++;
146         w.len = i > w.len ? i : w.len;
147         return w;
148     }
149     friend BigInteger operator+(BigInteger r, BigInteger w)
150     {
151         int len = r.len > w.len ? r.len : w.len;
152         for (int i = 0; i < len; i++) {
153             if (i < w.len)
154                 r.arg[i] = r.arg[i] + w.arg[i];
155             r.arg[i + 1] += r.arg[i] / 10;
156             r.arg[i] = r.arg[i] % 10;
157         }
158         while (r.arg[len] >= 10) {
159             r.arg[len + 1] += r.arg[len] / 10;
160             r.arg[len] = r.arg[len] % 10;
161             len++;
162         }
163         if (r.arg[len]) len++;
164         r.len = len > r.len ? len : r.len;
165         return r;
166     }
167     friend BigInteger operator-(BigInteger r, BigInteger w)
168     {
169         for (int i = 0; i < r.len; i++) {
170             if (r.arg[i] >= w.arg[i])
171                 r.arg[i] = r.arg[i] - w.arg[i];
172             else {
173                 r.arg[i] = r.arg[i] + 10;
174                 r.arg[i + 1] = r.arg[i + 1] - 1;
175                 r.arg[i] = r.arg[i] - w.arg[i];
176             }
177         }
178         while (r.arg[r.len - 1] == 0 && r.len > 1)
179             r.len--;
180         return r;
181     }
182     friend BigInteger operator-(BigInteger r, int w)
183     {
184         for (int i = 0; i < r.len; i++, w = w / 10) {
185             if (r.arg[i] >= w % 10)
186                 r.arg[i] = r.arg[i] - w % 10;
187             else {
188                 r.arg[i] = r.arg[i] + 10;
189                 r.arg[i + 1] = r.arg[i + 1] - 1;
190                 r.arg[i] = r.arg[i] - w % 10;
191             }
192         }
193         while (r.arg[r.len - 1] == 0 && r.len > 1)
194             r.len--;
195         return r;
196     }
197     friend BigInteger operator*(int x, BigInteger w)
198     {
199         BigInteger r;
200         if (x == 0 || (w.len == 1 && w.arg[0] == 0)) {
201             return r;
202         }
203         for (int i = 0; i < w.len; i++) {
204             r.arg[i] += w.arg[i] * x;
205             r.arg[i + 1] += r.arg[i] / 10;
206             r.arg[i] = r.arg[i] % 10;
207         }
208         int i = r.arg[w.len] == 0 ? w.len - 1 : w.len;
209         while (r.arg[i] >= 10) {
210             r.arg[i + 1] = r.arg[i] / 10;
211             r.arg[i] = r.arg[i] % 10;
212             i++;
213         }
214         r.len = (i >= 0) ? i + 1 : 1;
215         return r;
216     }
217     friend BigInteger operator*(BigInteger w, int x)
218     {
219         BigInteger r;
220         if (x == 0 || (w.len == 1 && w.arg[0] == 0)) {
221             return r;
222         }
223         for (int i = 0; i < w.len; i++) {
224             r.arg[i] += w.arg[i] * x;
225             r.arg[i + 1] += r.arg[i] / 10;
226             r.arg[i] = r.arg[i] % 10;
227         }
228         int i = r.arg[w.len] == 0 ? w.len - 1 : w.len;
229         while (r.arg[i] >= 10) {
230             r.arg[i + 1] = r.arg[i] / 10;
231             r.arg[i] = r.arg[i] % 10;
232             i++;
233         }
234         r.len = (i >= 0) ? i + 1 : 1;
235         return r;
236     }
237     friend BigInteger operator*(BigInteger r, BigInteger w)
238     {
239         BigInteger v;
240         if ((r.len == 1 && r.arg[0] == 0) || (w.len == 1 && w.arg[0] == 0)) {
241             return v;
242         }
243         for (int i = 0; i < r.len; i++) {
244             for (int k = 0; k < w.len; k++) {
245                 v.arg[i + k] += w.arg[k] * r.arg[i];
246                 v.arg[i + k + 1] += v.arg[i + k] / 10;
247                 v.arg[i + k] = v.arg[i + k] % 10;
248             }
249         }
250         int i = w.len + r.len - 1;
251         i = v.arg[i] == 0 ? i - 1 : i;
252         while (v.arg[i] >= 10) {
253             v.arg[i + 1] = v.arg[i] / 10;
254             v.arg[i] = v.arg[i] % 10;
255             i++;
256         }
257         v.len = (i >= 0) ? i + 1 : 1;
258         return v;
259     }
260 
261     friend BigInteger operator/(BigInteger r, int w)
262     {
263         BigInteger  h, resl;
264         if (w == 0) return h;
265         for (int i = r.len - 1; i >= 0; i--) {
266             resl = 10 * resl;
267             h = r.arg[i] + (10 * h);
268             while (w <= h) {
269                 resl = 1 + resl;
270                 h = h - w;
271             }
272         }
273         return resl;
274     }
275     friend BigInteger operator/(BigInteger r, BigInteger w)
276     {
277         BigInteger h, resl;
278         if (w.len == 1 && w.arg[0] == 0) return h;
279         for (int i = r.len - 1; i >= 0; i--) {
280             resl = 10 * resl;
281             h = r.arg[i] + (10 * h);
282             while (w <= h) {
283                 resl = 1 + resl;
284                 h = h - w;
285             }
286         }
287         return resl;
288     }
289 
290     friend BigInteger operator%(BigInteger r, BigInteger w)
291     {
292         BigInteger h;
293         if (w.len == 1 && w.arg[0] == 0) return h;
294         for (int i = r.len - 1; i >= 0; i--) {
295             h = r.arg[i] + (10 * h);
296             while (w <= h) {
297                 h = h - w;
298             }
299         }
300         return h;
301     }
302     void sqrt()
303     {
304         BigInteger w, r;
305         w.len = r.len = 0;
306         int lens = len - 1;
307         if (len == 1 && arg[0] == 1)
308             return;
309         r.arg[r.len++] = arg[lens--];
310         if (len % 2 == 0)
311             r = arg[lens--] + 10 * r;
312         while (lens >= -1) {
313             int i = 0;
314             while ((i*(i + 20 * w)) <= r) {
315                 i++;
316             }
317             i--;
318             if (i == -1 || (r.len == 1 && r.arg[0] == 1))
319                 i = 0;
320             r = r - (i*(i + 20 * w));
321             w = i + 10 * w;
322             if (lens >= 0) {
323                 r = arg[lens--] + 10 * r;
324                 r = arg[lens--] + 10 * r;
325             }
326             else
327                 lens -= 2;
328         }
329         *this = w;
330     }
331 };

 

大数模板(加减乘除幂次开方)

标签:模板   size   char   void   大数模板   art   sizeof   ==   i++   

原文地址:https://www.cnblogs.com/romaLzhih/p/9655751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!