When writing a program, it is often necessary to deal with some big data. We know, however, that the maximum number of integers that can be stored in C + + is \ (2^64-1\). When the big data to be processed exceeds this range, it is necessary to use the so-called high-precision algorithm, the principle is actually the simulation of the vertical calculation (special, Division requires trial quotient).
When actually writing, we will find that a one-time operation takes a lot of hours, so consider how to improve the efficiency, where the use of \ (bitset\) used in the pressure-level thought, that is, 4-bit or 8-bit storage, operation. The most common is to press 4 bits so that multiplication does not overflow. But in this case, a trial business is too slow to divide, so a two-point trial is needed.
#include <algorithm> #include <cstring> #include <cstdio>const int MAXL = 100;struct bign {static const int BASE = 10000; static const int WIDTH = 4; int Len, V[MAXL]; Bign () {len = 0; memset (V, 0, sizeof (v)); } bign operator = (unsigned long long n) {while (n > 0) {V[++len] = n% BASE; n/= BASE; } return *this; } bign operator = (char *s) {int tmp = 0, x = 1; for (int i = strlen (s)-1; I >= 0; i--) {tmp + = x * (S[i]-48); x *= 10; if (x = = BASE) {V[++len] = tmp; TMP = 0; x = 1; }} if (TMP) {v[++len] = tmp; } return *this; } inline void Read () {char s[maxl << 2]; scanf ("%s", s); *this = s; Return } inline void Write () {printf ("%d\n", V[len]); for (int i = len; I >= 1; i--) printf ("%04d", V[i]); Return }};
High accuracy algorithm--step, pressure position and its application