高精模板

我肝了好多天啊!!!!

终于把高精的模板搞出来了!!!

这个高精是直接用结构体封装的,然后可以很方便的直接声明高精变量,然后就可以像普通的高精一样使用了

现有如下三个版本

Code1:

不支持负数

  1 struct HP {
  2     int p[25555], len;//array max are 25555
  3     bool flag;
  4     HP () {
  5         memset(p, 0, sizeof p);
  6         len = 1, flag = false;
  7     }
  8     void in() {
  9         len = 0;
 10         char c = getchar();
 11         while (c < '0' || c > '9'){if (c == '-') flag ^= 1; c = getchar();}
 12         while ('0' <= c && c <= '9') {p[++len] = c - '0'; c = getchar();}
 13         for (int l = 1, r = len; l < r;) {
 14             swap(p[l++], p[r--]);
 15         }
 16     }
 17     void make(int x) {
 18         memset(p, 0, sizeof p);
 19         len = 0;
 20         while (x) {
 21             p[++len] = x;
 22             x /= 10;
 23         }
 24     }
 25     void print() {
 26         if (flag == true) printf("-");
 27         for (int i = len; i >= 1; i--) {
 28             printf("%d", p[i]);
 29         }
 30     }
 31     bool operator < (const HP &b) const {
 32         if(len != b.len) return len < b.len;
 33         for (int i = len; i >= 1; i--) {
 34             if(p[i] != b.p[i]) return p[i] < b.p[i];
 35         }
 36         return false;
 37     }
 38     bool operator > (const HP &b) const {
 39         if(len != b.len) return len > b.len;
 40         for (int i = len; i >= 1; i--) {
 41             if(p[i] != b.p[i]) return p[i] > b.p[i];
 42         }
 43         return false;
 44     }
 45     bool operator <= (const HP &b) const {
 46         if (len != b.len) return len < b.len;
 47         for (int i = len; i >= 1; i--) {
 48             if (p[i] != b.p[i]) return p[i] < b.p[i];
 49         }
 50         return true;
 51     }
 52     bool operator >= (const HP &b) const {
 53         if (len != b.len) return len > b.len;
 54         for (int i = len; i >= 1; i--) {
 55             if (p[i] != b.p[i]) return p[i] > b.p[i];
 56         }
 57         return true;
 58     }
 59     bool operator == (const HP &b) const {
 60         if (len != b.len) return false;
 61         for (int i = len; i >= 1; i--) {
 62             if (p[i] != b.p[i]) return false;
 63         }
 64         return true;
 65     }
 66     bool operator != (const HP &b) const {
 67         if (len != b.len) return true;
 68         for (int i = len; i >= 1; i--) {
 69             if (p[i] != b.p[i]) return true;
 70         }
 71         return false;
 72     }
 73     HP operator + (const HP &b) const {//high + high
 74         HP c;
 75         c.len = max(len, b.len);
 76         int re = 0;
 77         for (int i = 1; i <= c.len; i++) {
 78             re += p[i] + b.p[i];
 79             c.p[i] = re % 10;
 80             re /= 10;
 81         }
 82         while (re) {
 83             c.p[++c.len] = re % 10;
 84             re /= 10;
 85         }
 86         return c;
 87     }
 88     HP operator - (const HP &B) const {//high - high
 89         HP a = (*this), b = B, c;
 90         if (a < b) {c.flag = true; swap(a, b);}
 91         c.len = a.len;
 92         int re = 0;
 93         for (int i = 1; i <= c.len; i++) {
 94             re += a.p[i] - b.p[i];
 95             if (re < 0) {
 96                 c.p[i] = re + 10;
 97                 re = -1;
 98             } else {
 99                 c.p[i] = re;
100                 re = 0;
101             }
102         }
103         while (c.len > 1 && c.p[c.len] == 0) c.len--;
104         return c;
105     }
106     HP operator * (const int &B) const {//high * low
107         if (B == 1) return (*this);
108         int b = B;
109         HP c;
110         c.len = len;
111         int re = 0;
112         for (int i = 1; i <= len; i++) {
113             re += p[i] * b;
114             c.p[i] = re % 10;
115             re /= 10;
116         }
117         while (re) {
118             c.p[++c.len] = re % 10;
119             re /= 10;
120         }
121         while (c.len > 1 && c.p[c.len] == 0) c.len--;
122         return c;
123     }
124     HP operator * (const HP &b) const {//high * high
125         if (b.len == 1 && b.p[1] == 1) return (*this);
126         if (len == 1 && p[1] == 1) return b;
127         HP c;
128         c.len = len + b.len;
129         for (int i = 1; i <= len; i++) {
130             int re = 0;
131             for (int j = 1; j <= b.len; j++) {
132                 c.p[i + j - 1] += p[i] * b.p[j] + re;
133                 re = c.p[i + j - 1] / 10;
134                 c.p[i + j - 1] %= 10;
135             }
136             c.p[i + b.len] = re;
137         }
138         while (c.p[c.len] > 0) c.len++;
139         while (c.len > 1 && c.p[c.len] == 0) c.len--;
140         return c;
141     }
142     HP operator / (const int &b) const {//high / low
143         if (b == 1) return (*this);
144         HP c;
145         c.len = len;
146         int re = 0;
147         for (int i = len; i >= 1; i--) {
148             re = re * 10 + p[i];
149             c.p[i] = re / b;
150             re %= b;
151         }
152         while (c.len > 1 && c.p[c.len] == 0) c.len--;
153         return c;
154     }
155     bool div_cmp(const HP& b) {//a compoent of high / high
156         if (len < b.len) return true;
157         for (int i = 0; i < b.len; i++) {
158             if (p[len - i] != b.p[b.len - i]) {
159                 return p[len - i] < b.p[b.len - i];
160             }
161         }
162         return false;
163     }
164     HP div_mul(int t) {//a compoent of high / high
165         if (!t) return (*this);
166         HP c;
167         c.len = len + t;
168         for (int i = 1; i <= len; i++) {
169             c.p[i + t] = p[i];
170         }
171         return c;
172     }
173     HP operator / (HP b) const {//high / high
174         if (b.len == 1 && b.p[1] == 1) return (*this);
175         if ((*this) < b) {
176             HP c;
177             c.len = 1, c.p[1] = 0;
178             return c;
179         }
180         HP a = (*this), c, tmp;
181         c.len = -1;
182         int l, t;
183         while (true) {
184             l = a.len - b.len - a.div_cmp(b);
185             if (l < 0) break;
186             tmp = b.div_mul(l);
187             t = 0;
188             while (tmp <= a) {
189                 a = a - tmp;
190                 t++;
191             }
192             if (c.len == -1 && t) {
193                 c.len = l + 1;
194             }
195             c.p[l + 1] = t;
196         }
197         return c;
198     }
199     HP operator % (const int b) const {//high % low
200         HP d = (*this), c = (*this) / b;
201         c = c * b;
202         HP e = d - c;
203         return e;
204     }
205     HP operator % (const HP &b) const {//high % high
206         HP d = (*this), c = (*this) / b;
207         c = c * b;
208         HP e = d - c;
209         return e;
210     }
211 };
212 HP pw(HP x, HP y) {
213     HP re = e1;//need to declare a variable as 1
214     for (; y > e0; y = y / 2) {
215         if (y % 2 == e1) {
216             re = re * x;
217         }
218         x = x * x;
219     }
220     return re;
221 }
222 HP Max(const HP &a, const HP &b) {
223     if (a.len > b.len) {
224         return a;
225     } else if (a.len < b.len) {
226         return b;
227     }
228     for (int i = a.len; i >= 1; i--) {
229         if (a.p[i] > b.p[i]) {
230             return a;
231         } else if (a.p[i] < b.p[i]) {
232             return b;
233         }
234     }
235     return a;
236 }
237 HP Min(const HP &a, const HP &b) {
238     if (a.len > b.len) {
239         return b;
240     } else if (a.len < b.len) {
241         return a;
242     }
243     for (int i = a.len; i >= 1; i--) {
244         if (a.p[i] < b.p[i]) {
245             return a;
246         } else if (a.p[i] > b.p[i]) {
247             return b;
248         }
249     }
250     return a;
251 }
View Code

Code2:

支持负数

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

Code2:

压4位高精,不带除法

  1 const int power = 4;
  2 const int Base = 1e4;
  3 struct HP {
  4     int p[10001], len;
  5     bool flag;
  6     HP () {
  7         len = 1, memset(p, 0, sizeof p);
  8     }
  9     void in() {
 10         int a[10000007], cnt = 0;
 11         char ch = getchar();
 12         while (ch < '0' || ch > '9') {ch = getchar();}
 13         while ('0' <= ch && ch <= '9') {a[cnt++] = ch - '0'; ch = getchar();}
 14         for (int l = 0, r = cnt - 1; l < r;) {
 15             swap(a[l++], a[r--]);
 16         }
 17         memset(p, 0, sizeof 0);
 18         len = (cnt + power - 1) / power;
 19         for (int i = 0, t = 0, w; i < cnt; i++, w *= 10) {
 20             if (i % power == 0) w = 1, t++;
 21             p[t] += w * a[i];
 22         }
 23     }
 24     void make(int x) {
 25         int a[10000007], cnt = 0;
 26         while (x) a[cnt++] = x % 10, x /= 10;
 27         memset(p, 0, sizeof 0);
 28         len = (cnt + power - 1) / power;
 29         for (int i = 0, t = 0, w; i < cnt; i++, w *= 10) {
 30             if (i % power == 0) w = 1, t++;
 31             p[t] += w * a[i];
 32         }
 33     }
 34     void print() {
 35         printf("%d", p[len]);
 36         for (int i = len - 1; i >= 1; i--) {
 37             printf("%.4d", p[i]);
 38         }
 39         puts("");
 40     }
 41     void add(int x) {
 42         if (x || len) p[++len] = x;
 43     }
 44     void rev() {
 45         reverse(p + 1, p + 1 + len);
 46     }
 47     bool operator >= (const HP &b) const {
 48         if (len != b.len) return len > b.len;
 49         for (int i = len; i >= 1; i--) {
 50             if (p[i] != b.p[i]) return p[i] > b.p[i];
 51         }
 52         return true;
 53     }
 54     HP operator + (const HP &b) {
 55         HP c;
 56         c.len = max(len, b.len);
 57         int re = 0;
 58         for (int i = 1; i <= c.len; i++) {
 59             c.p[i] = p[i] + b.p[i] + re;
 60             re = c.p[i] / Base;
 61             c.p[i] %= Base;
 62         }
 63         while (re) c.p[++c.len] = re, re /= Base;
 64         return c;
 65     }
 66     HP operator - (const HP &b) {
 67         HP c = *(this);
 68         for (int i = 1; i <= c.len; i++) {
 69             c.p[i] -= b.p[i];
 70             if (c.p[i] < 0) {
 71                 c.p[i] += Base;
 72                 c.p[i + 1]--;
 73             }
 74         }
 75         while (c.len && !c.p[c.len]) c.len--;
 76         return c;
 77     }
 78     HP operator * (const int &b) {
 79         HP c;
 80         c.len = len;
 81         int re = 0;
 82         for (int i = 1; i <= c.len; i++) {
 83             c.p[i] = p[i] * b + re;
 84             re = c.p[i] / Base;
 85             c.p[i] %= Base;
 86         }
 87         while (re) c.p[++c.len] = re % Base, re /= Base;
 88         return c;
 89     }
 90     HP operator * (const HP &b) {
 91         HP c;
 92         c.len = len + b.len - 1;
 93         for (int i = 1; i <= len; i++) {
 94             for (int j = 1; j <= b.len; j++) {
 95                 c.p[i + j - 1] += p[i] * b.p[j];
 96                 c.p[i + j] += c.p[i + j - 1] / Base;
 97                 c.p[i + j - 1] %= Base;
 98             }
 99         }
100         while (c.p[c.len + 1]) c.len++;
101         return c;
102     }
103 };
View Code

当然,我也不太敢保证完全无锅,但是我觉得差不多(逃

原文地址:https://www.cnblogs.com/Sundial/p/11830629.html