High-Precision templates and c ++ high-precision templates
# Include <cstring> # include <cstdio> # include <cstdlib> # define MAX 200 using namespace std; typedef struct {int len; int s [MAX + 1];} hp; void input (hp * a, int x) // read the number {int I; a-> len = 0; while (x> 0) {a-> s [1 + a-> len ++] = x % 10; x/= 10;} for (I = a-> len + 1; I <= MAX; I ++) a-> s [I] = 0;} void input1 (hp * a, char * str) // read the string {int I, len; a-> len = 0; if (str = NULL) return; len = strlen (str); while (len> 0) {a-> s [1 + a-> len ++] = * (str + len-1)-'0'; len --;} for (I = a-> len + 1; I <= MAX; I ++) a-> s [I] = 0;} void print (hp * y) // print the number {int I; for (I = y-> len; I> = 1; I --) printf ("% d ", y-> s [I]); printf ("\ n");} void add (hp * a, hp * B, hp * c) // high-precision addition c = a + B {int I, len; for (I = 1; I <= MAX; I ++) c-> s [I] = 0; if (a-> len> B-> len) len = a-> len; else len = B-> len; for (I = 1; I <= len; I ++) {c-> s [I] + = a-> s [I] + B-> s [I]; if (c-> s [I] >=10) {c-> s [I]-= 10; c-> s [I + 1] ++ ;}} if (c-> s [len + 1]> 0) len ++; c-> len = len;} void subtract (hp * a, hp * B, hp * c) // high-precision subtraction c = a-B {int I, len; for (I = 1; I <= MAX; I ++) c-> s [I] = 0; if (a-> len> B-> len) len = a-> len; else len = B-> len; for (I = 1; I <= len; I ++) {c-> s [I] + = a-> s [I]-B-> s [I]; if (c-> s [I] <0) {c-> s [I] + = 10; c-> s [I + 1] --;} while (len> 1 & c-> s [len] = 0) len --; c-> len = len;} int compare (hp * a, hp * B) // comparison of precision {int len; if (a-> len> B-> len) len = a-> len; else len = B-> len; while (len> 0 & a-> s [len] = B-> s [len]) len --; if (len = 0) return 0; else return a-> s [len]-B-> s [len];} void multiply (hp * a, int B, hp * c) // high precision * single precision {int I, len; for (I = 1; I <= MAX; I ++) c-> s [I] = 0; len = a-> len; for (I = 1; I <= len; I ++) {c-> s [I] + = a-> s [I] * B; c-> s [I + 1] + = c-> s [I]/10; c-> s [I] % = 10;} len ++; while (c-> s [len]> = 10) {c-> s [len + 1] + = c-> s [len]/10; c-> s [len] % = 10; len ++;} while (len> 1 & c-> s [len] = 0) len --; c-> len = len;} void multiplyh (hp * a, hp * B, hp * c) // high precision * High Precision {int I, j, len; for (I = 1; I <= MAX; I ++) c-> s [I] = 0; for (I = 1; I <= a-> len; I ++) {for (j = 1; j <= B-> len; j ++) {c-> s [I + J-1] + = a-> s [I] * B-> s [j]; c-> s [I + j] + = c-> s [I + J-1]/10; c-> s [I + J-1] % = 10 ;}} len = a-> len + B-> len + 1; while (len> 1 & c-> s [len] = 0) len --; c-> len = len;} void power (hp * a, int B, hp * c) // high-precision multiplication Party c = a ^ B {hp e; if (B = 0) {c-> len = 1; c-> s [1] = 1;} else if (B = 1) {memcpy (c, a, sizeof (hp);} else {power (a, B/2, & e); multiplyh (& e, & e, c ); if (B % 2 = 1) {memcpy (& e, c, sizeof (hp); multiplyh (& e, a, c );}}} void divide (hp * a, int B, hp * c, int * d) // High Precision/single precision {d is the remainder} {int I, len; for (I = 1; I <= MAX; I ++) c-> s [I] = 0; len = a-> len; * d = 0; for (I = len; i> = 1; I --) {* d = * d * 10 + a-> s [I]; c-> s [I] = * d/B; * d % = B;} while (len> 1 & c-> s [len] = 0) len --; c-> len = len ;} void multiply10 (hp * a) // high precision * 10 {int I; for (I = a-> len; I> = 1; I --) a-> s [I + 1] = a-> s [I]; a-> s [1] = 0; a-> len ++; while (a-> len> 1 & a-> s [a-> len] = 0) a-> len --;} void divideh (hp *, hp * B, hp * c, hp * d) // High Precision/High Precision {d is the remainder} {hp e; int I, len; for (I = 1; I <= MAX; I ++) {c-> s [I] = 0; d-> s [I] = 0;} len = a-> len; d-> len = 1; for (I = len; I> = 1; I --) {multiply10 (d ); d-> s [1] = a-> s [I]; while (compare (d, B)> = 0) {subtract (d, B, & e ); * d = e; c-> s [I] ++ ;}} while (len> 1 & c-> s [len] = 0) len --; c-> len = len;} int main () {return 0 ;}