|
高精度,用一个线性表保存一个大整数,具体说,将大整数写成p进制数,线性表的每一项存p进制其中一位。 参考下面代码 #include <iostream>; v1 g5 e( H2 ^3 y: P, s9 S3 ^
#include <memory>
( X4 Q c+ g, E) A' L# include <string>
4 D/ R$ n8 d2 s8 Cusing namespace std; typedef long long hugeint; const int Base = 1000000000;) `% P4 K3 _0 r) s; M
const int Capacity = 200; struct xnum; S- D5 G/ A" @
{
. s, r6 J' p2 O3 G) n int Len;
$ _4 m( F1 l0 E" ?# \, Z2 X5 h int Data[Capacity];! E1 Q7 ]) D( ^
xnum() : Len(0) {}
- q" k! v. J% [9 I. R xnum(const xnum& V) : Len(V.Len) { memcpy(Data, V.Data, Len * sizeof *Data); }
, Q0 }) C6 D" S( I0 q xnum(int V) : Len(0) { for (; V > 0; V /= Base) Data[Len++] = V % Base; }
C' g L5 ?& e" Y4 P% u6 C. W xnum& operator=(const xnum& V) { Len = V.Len; memcpy(Data, V.Data, Len * sizeof *Data); return *this; }8 a+ J* k: g0 p6 G( G6 v! P% ^
int& operator[](int Index) { return Data[Index]; }
) _3 j/ N2 [* h t# m5 h8 F0 Y ?# v int operator[](int Index) const { return Data[Index]; }
2 D) E( w' ~ M};
% {0 } {" E1 F0 m6 mint compare(const xnum& A, const xnum& B)
' [ |5 g* m; n( n; }2 L5 K{
6 q: n! K/ }& J7 m' q# h# E int I;+ g% w, J, V# K$ B* k: L, G
if (A.Len != B.Len) return A.Len > B.Len ? 1 : -1;
- s9 O* Y8 s7 N+ b) ^) Z9 O Q for (I = A.Len - 1; I >= 0 && A[I] == B[I]; I--);* E: \! T3 _! r, o: ^+ W
if (I < 0) return 0;
' n/ r& ]/ p/ D% F; o7 [ return A[I] > B[I] ? 1 : -1;* B* g- |: v, h/ d+ C6 B
} xnum operator+(const xnum& A, const xnum& B)
2 D* f7 d* N9 W{
j; i( f0 P$ F xnum R;
( {- {5 J8 L' o+ M- V int I;
" t" U N! b; S g2 K4 K4 c* d int Carry = 0;
. {% S7 q/ Z. z2 K+ a' V for (I = 0; I < A.Len || I < B.Len || Carry > 0; I++)7 W' @( y: R6 ^" c6 f: D0 T
{
8 f. q" K ~/ M! u8 o) r# o6 ]7 I% G if (I < A.Len) Carry += A[I];
9 T! a" s7 [8 J$ ?; m6 Z if (I < B.Len) Carry += B[I];" X2 E# W- o V
R[I] = Carry % Base;
5 B: v! y0 M. B: @: H- Y Carry /= Base;2 T& d% ?$ t6 W5 ^
}
G" h( v' O# g# f! n R.Len = I;
4 r& j1 L7 w0 j: p return R;
# Z6 F' N+ k- g6 m, V7 a( a- D4 L8 {} xnum operator-(const xnum& A, const xnum& B)
/ I/ I* L9 p2 h2 X: b/ k; n{% o" O/ U$ m N# E2 b, _! A: y8 T# P1 R
xnum R;5 c" Q2 ] l2 n
int Carry = 0;
* ^$ K) x* S% t; `2 h R.Len = A.Len;
. h8 t- I9 D- U4 f/ z int I;0 Q5 S* A# ]0 ]; t" O: u. A
for (I = 0; I < R.Len; I++)& X: ~9 t3 s+ n2 w' ?
{- ]- M" v6 R# P2 E" k5 M
R[I] = A[I] - Carry;) g9 G; n* s- m+ e3 k# [& W0 @: J
if (I < B.Len) R[I] -= B[I];- W& l e+ u* B% M) o$ `* G
if (R[I] < 0) Carry = 1, R[I] += Base;
3 U, [# m2 D; J else Carry = 0;* O" t% X- f# I, S- S
}
- R) A, t/ T. O! [8 v1 H6 Y4 O while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;
. |; C+ }6 V- m return R;# i/ f# t; k8 {8 ]) |% U
} xnum operator*(const xnum& A, const int B)
' e% C% K' R/ g6 g{6 x. h- z- v( d8 y$ B
int I;6 H% |9 j/ C. E5 |
if (B == 0) return 0;
6 b6 w2 I; p, I6 u2 r* L8 B xnum R;
, _3 D# r' J1 B% `4 H9 @( y0 W9 P1 D3 H7 a$ P hugeint Carry = 0;
; S$ H' @: l5 O5 Z3 n1 D for (I = 0; I < A.Len || Carry > 0; I++)& ?# b0 R; A- s, i; N1 b
{
* P& z8 _/ h( G* t% ?. n" }' j6 ]8 M if (I < A.Len) Carry += hugeint(A[I]) * B;1 I4 \$ v. M6 ~/ c- f# ~; _5 c
R[I] = Carry % Base;9 G1 y1 Y/ d: V1 j: c! T, ^& c
Carry /= Base;
& m* k x1 f/ u/ I- U+ R4 \- X1 a# L }
$ x$ {* k% A. v6 Q7 o0 t R.Len = I;
' O3 y4 p q/ P. d, f return R;
" [' {* {- T& M- Y# c: B+ B} xnum operator*(const xnum& A, const xnum& B)6 k0 ?9 s6 o3 ~
{% u) ]* D- t$ l2 A$ U
int I;8 s) r! Q4 R- _% \
if (B.Len == 0) return 0;
" x9 M8 w: @4 C7 ^: r6 K1 j xnum R;0 ?9 i" l+ V9 a5 ?4 l- }5 @
for (I = 0; I < A.Len; I++)* P: g* g# R6 k
{: N2 J4 H6 j- @2 Z0 l( R
hugeint Carry = 0;
: W! r1 Q* X- h4 M% r: |$ l2 q7 Y for (int J = 0; J < B.Len || Carry > 0; J++)8 m7 S& g- q* Q* A) p+ i ]
{9 y; a3 }* W3 B/ Y/ r; a
if (J < B.Len) Carry += hugeint(A[I]) * B[J];2 V7 L+ W1 i$ _
if (I + J < R.Len) Carry += R[I + J];
# @6 Q. I4 m3 `. V5 F; W- q1 n+ V& { if (I + J >= R.Len) R[R.Len++] = Carry % Base;5 s; {0 z. E) J
else R[I + J] = Carry % Base;
: f0 G" h$ O3 T) E( Z+ ]& B% B7 g Carry /= Base;
- I4 w+ g& ?! e% U* Q" h: e9 x3 u1 {* J }
6 B6 [3 t$ p# d/ N1 k4 p' O% } }
5 x9 U8 J) J$ w. X/ L/ @* ^+ N; Z return R;2 _& z& B! s4 u+ M
} xnum operator/(const xnum& A, const int B)
% v6 n! ~4 ?3 c6 Z3 f{$ `; S5 n& b, N% P( ~/ ^
xnum R;& T; f' s" B: f* P) p# i! Y
int I;
5 \. @* u8 @3 Y% J4 z8 i5 l% g hugeint C = 0;
. t, s* ^ P6 f( G& b! x2 V for (I = A.Len - 1; I >= 0; I--)! W: Y1 \6 o: |: B) c- W
{7 I" Z' y$ u9 ?
C = C * Base + A[I];
3 M1 u, x& Y3 A0 Z R[I] = C / B;% O) ?6 a% r; |2 y1 Q1 \+ b
C %= B;
5 |9 \5 R( G9 g3 B }9 o) A9 T, D6 S
R.Len = A.Len;
' m7 S w6 X h* j6 l, ` while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;
: M6 }$ G8 p4 s6 Q7 W- ~! \ return R;
7 m+ u+ J- ~3 e} xnum operator/(const xnum& A, const xnum& B)
) F' t: w+ Y6 s& R; U& |5 n{2 e7 o. Q0 X% b( n) j( ^
int I;
$ u6 a1 b! h3 d! v xnum R, Carry = 0;
' i! w* q f) S6 a9 S4 w int Left, Right, Mid;9 `4 q7 h0 D& I* l
for (I = A.Len - 1; I >= 0; I--)
$ n; z- o) m. h+ w/ u {
: H5 ^% n, \2 S3 _4 l7 u Carry = Carry * Base + A[I];$ u* y. e, v7 n8 R. H
Left = 0;% ?. ^4 D# p {& U7 M t
Right = Base - 1; |% t+ H- B- O) ~2 N) q
while (Left < Right), I L6 x* H% U; r, q: p K
{
) K, X3 Z4 z2 H4 ^- I0 Z0 Z) q; l Mid = (Left + Right + 1) / 2;+ C5 f. @" w0 M6 C$ K2 z0 Y+ R
if (compare(B * Mid, Carry) <= 0) Left = Mid;4 v/ D3 t6 D+ L0 B' r" _8 w0 ^
else Right = Mid - 1;' X$ a d f J1 e7 R
}* u* Q w/ ^# Y" P x8 _
R[I] = Left;
. W2 [- z2 N7 o |2 E Carry = Carry - B * Left;
$ S: ?% y) U" g, N( Y. h }/ r7 F+ A: V: G4 M
R.Len = A.Len;
2 ]# G- ^% r& |2 z# l( f9 t5 [* X, ? while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;
, A; s0 ~1 }6 l& K return R;
" k% I' O5 w- V: Q% {) A3 P}
) |, Q# w; U' {6 r; H7 R; Axnum operator%(const xnum& A, const xnum& B)! C# M" o8 Y7 e/ a7 m: z7 A
{
1 r7 H& c \3 K6 \' S% G int I;0 d9 Q; v8 X4 a6 X
xnum R, Carry = 0;: d0 _1 `$ M1 w1 _6 p
int Left, Right, Mid;
, R7 v( ]& G7 I7 q1 A for (I = A.Len - 1; I >= 0; I--)+ e+ {: q b B ]
{
$ g/ h y) w5 ] Carry = Carry * Base + A[I];
6 O' u. U0 r( d$ P! s3 A Left = 0;; Q- x% T6 F1 q, ^4 H- _0 |
Right = Base - 1; J9 h9 m2 }; B8 s
while (Left < Right)
1 S# `& C/ G- f* w( o {9 O* Q+ J3 [1 P7 d# `" C7 \
Mid = (Left + Right + 1) / 2;* \5 g \9 Q) E3 C c" S
if (compare(B * Mid, Carry) <= 0) Left = Mid;: ?2 p; d8 ~5 B& V* N4 \
else Right = Mid - 1;' Y$ _. c' ~0 j" u, j' Y: k# x& q
}& |7 T1 ^1 F/ |% Y! k! R. k+ a. e
R[I] = Left;
( \+ o3 F) N" r; a% {" H% E7 d Carry = Carry - B * Left;
5 [* N0 p+ \8 j; O/ @( n4 ? }
: c* K5 b+ C8 ^ Z& h6 }$ w9 q R.Len = A.Len;
4 {7 n* e" c+ [' K( g+ _, H while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;
% [' J' N6 J v; ~9 m, E$ P return Carry;) r% O6 F& e) U2 M" @
} istream& operator>>(istream& In, xnum& V)
0 x/ u) f# e, l{
# K6 z# \9 S: A& b1 V char Ch;; q5 o' Q; O M3 W3 O
for (V = 0; In >> Ch;)2 x3 @7 `1 D2 m8 ?, s2 u
{
1 g2 `4 p' \& x+ A; Q V = V * 10 + (Ch - '0');) N! k; Y5 A6 J* {3 |
if (cin.peek() <= ' ') break;
$ H: K! e. [( }+ A; L }
$ _" i. ]& [) Q/ k( I return In;
6 U+ V8 d4 |) D} ostream& operator<<(ostream& Out, const xnum& V)
) I; v1 L3 m" Y: o, f8 Y{
- n$ m2 T+ D1 W2 x int I;4 [7 w# W( K( l2 ?
Out << (V.Len == 0 ? 0 : V[V.Len - 1]);
; T0 N9 V1 { K. i for (I = V.Len - 2; I >= 0; I--) for (int J = Base / 10; J > 0; J /= 10) Out << V[I] / J % 10;% t0 J2 A1 c! X( r$ H; j
return Out;5 Q7 k7 Z4 x- L9 F$ v
}+ Z/ S. N9 ?7 P/ e! S. M
|