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

很好用的模板,但当时做题的时候从哪里找的不知道了,原作看到知会我一声我补上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 };

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

时间: 2024-10-13 07:22:47

大数模板(加减乘除幂次开方)的相关文章

hdu 4927 Series 1 (大数模板加减乘除)

Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1067    Accepted Submission(s): 390 Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-order series of A i

ACM大数模板(支持正负整数)

之前就保留过简陋的几个用外部数组变量实现的简单大数模板,也没有怎么用过,今天就想着整合封装一下,封装成C++的类,以后需要调用的时候也方便得多. 实现了基本的加减乘除和取模运算的操作符重载,大数除以大数难度太大就没实现,另外还实现了比较运算符,方便实际使用贴近内置类型的体验. 话不多说,贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <ctype.h> 4 5 #define MAXBIT 1007

timus 1547. Password Search【题意思路+大数模板】

题目地址传送门:URAL 1547 这道题需要用到大数的很多模板,推荐大家去刷刷! 题目大意:Vova忘记了在Timus OJ上面的密码了,密码是由小写字母(a~z)组成的,他只知道密码长度不大于n位,现在他需要用m台数据处理器对密码进行检索,其中检索顺序需要满足字典序.比如他的密码长度不大于2,那就需要依次检索a,b,..........,y,z,aa,ab,..........,zy,zz.输出每台数据检索器的检索区间,使得总的检索效率可以达到最高. 已知密码的总可能数不少于数据处理器个数.

大数模板 poj3982

1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就好了,可是假设不能独立写出来的话,不相当于白搭吗.所以我学完c++后会手写出模板的!. 注意,这个大数模板仅仅适用于不太大的模拟,几万位,肯定会爆内存的,兴许会补上功能更强大的模板和JAVA大数模板. #include<iostream> #include<cstdio> #inclu

大数模板

1 #include<stdio.h> 2 #include<string.h> 3 #include<conio.h> 4 #include<stdlib.h> 5 #include<time.h> 6 #define N 10000 7 // 大数模板... 8 class BigNum 9 { 10 public: 11 int s[N]; //存放各位数字,s[0]为符号位,1代表正数,-1代表负数 12 //数组内高位存高位,123存在

简单大数模板(+ - )--待完善

水了ural的dp专题前三道1009,1012,1013,都是同一个问题,只是数据规模变大了. 题意大概是这样的:求一个k进制的n位数字,满足不含前导0和不含连续两个0的个数有多少个. dp[i][0]表示第i位为0有多少个满足条件,dp[i][1]表示i位不为0满足条件的个数,则结果就是dp[n][1]; 递推关系如下: dp[i][0]=dp[i-1][1]; dp[i][1]=(dp[i-1][0]+dp[i-1][1])*k; 顺便贴上简单的大数类 1 #include<iostream

Acdream 1210 Chinese Girls&#39; Amusement(大数模板运算 + 找规律)

传送门 Chinese Girls' Amusement Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You must have heard that the Chinese culture is quite different from that of Europe or Rus

vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)

P1447开关灯泡 Accepted 标签:CSC WorkGroup III[显示标签] 描写叙述 一个房间里有n盏灯泡.一開始都是熄着的,有1到n个时刻.每一个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的. 格式 输入格式 一个数n 输出格式 m,表示最后有m盏是亮着的 例子1 例子输入1[复制] 5 例子输出1[复制] 2 限制 1s 提示 范围:40%的数据保证,n<=maxlongint 100%的数据保证,n<=

bignum 大数模板

今天无意间看到一个很好的大数模板,能算加.减.乘.除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减.我借用了一下:(作过少许代码上的精简) 1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<iostream> 6 using