QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3523|回复: 3
打印 上一主题 下一主题

谁有超长整数运算的好算法?

[复制链接]
字体大小: 正常 放大
xuefu998        

1

主题

0

听众

49

积分

升级  46.32%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-2-1 16:07 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

谁有超长整数运算的好算法?

- j' S2 o( X" t; `/ ]% |6 r

现在CPU还是64位的,量长整数不过int64(2^64),超过这个数的整数怎么办,如1000!等,请各位有心人指点迷津。

[em08][em08][em08]
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
aftermath        

0

主题

0

听众

49

积分

升级  46.32%

该用户从未签到

新人进步奖

高精度,用一个线性表保存一个大整数,具体说,将大整数写成p进制数,线性表的每一项存p进制其中一位。

参考下面代码

#include <iostream> ; `7 d$ C, o2 ^. S( d" U#include <memory>$ D% n) i6 S+ c0 a3 \1 [) Y # include <string> z v M. ? x& a# @# @using namespace std;

typedef long long hugeint;

const int Base = 1000000000;8 P- [- z. f* q& m. F* Z+ w" N const int Capacity = 200;

struct xnum ' r# V6 e: U% b0 D! g( h{8 W& v7 |5 |" L+ G+ l$ [ int Len;9 Q, T% n/ o! x6 a% @: s6 _ int Data[Capacity];. I/ X; P& d# c; f. } xnum() : Len(0) {} * F1 Y0 N6 K; C0 G. l xnum(const xnum& V) : Len(V.Len) { memcpy(Data, V.Data, Len * sizeof *Data); } " Q: j* `, n) M/ r$ E& t! Z xnum(int V) : Len(0) { for (; V > 0; V /= Base) Data[Len++] = V % Base; }) |# u0 K- S4 _) b5 y7 f xnum& operator=(const xnum& V) { Len = V.Len; memcpy(Data, V.Data, Len * sizeof *Data); return *this; } " z- j* V( [- Q0 Y9 B int& operator[](int Index) { return Data[Index]; } ' q9 H0 J, ]' Z% S' C6 v int operator[](int Index) const { return Data[Index]; } M/ C! d6 x f7 L. ?3 z};

8 o8 I: N% W1 s/ n' |, s int compare(const xnum& A, const xnum& B) - m2 m! J) E) P/ m{; V% t5 T1 e1 w$ h# i int I;/ T1 \& k' }, ~" Y' R4 V if (A.Len != B.Len) return A.Len > B.Len ? 1 : -1; . ^, L$ L5 L7 `- t for (I = A.Len - 1; I >= 0 && A[I] == B[I]; I--);" u2 h8 }+ h9 ^' D- a if (I < 0) return 0; _+ \ y2 A( ~! \ return A[I] > B[I] ? 1 : -1;( u$ [1 G5 @, Q }

xnum operator+(const xnum& A, const xnum& B) 7 d6 h/ Y$ a) ]+ J{ / l$ @* u0 Y1 b1 |4 ?( x" w R xnum R; 6 I8 S# q! K: ]1 j' ]0 t int I;" F) i/ A0 t$ ~+ ~; d ?$ W int Carry = 0; ! L* r, N- K9 |, L for (I = 0; I < A.Len || I < B.Len || Carry > 0; I++)7 Q, b! \* ~0 \' m! l7 R$ f8 a+ N {2 e; \# {# T a: @ if (I < A.Len) Carry += A[I]; 1 F, W, Y7 o8 d, U, r0 d if (I < B.Len) Carry += B[I];$ }3 o4 Q+ d+ k$ a4 v2 y R[I] = Carry % Base;2 @7 o& y" q0 ? q, e2 X7 F Carry /= Base; 5 O. p, r3 l# [. k' T }6 C/ M, K+ N5 J( e i4 T2 }4 ?6 p* Q( T R.Len = I;, C+ u2 H* L/ N/ i0 r return R;! H" b+ q8 ?8 f% k B' ]6 k }

xnum operator-(const xnum& A, const xnum& B)% T) B! M- N/ b {" ~' ~) _, `) R( U8 W: V xnum R;) n6 {. P9 R5 P; o& V int Carry = 0;( K) k# I0 W' A8 \# X/ d1 v# W5 Z R.Len = A.Len; - r2 `3 u# P" u7 _& \" B int I;& g; o- }) K9 i: ?1 @* G for (I = 0; I < R.Len; I++) - y$ W/ y! u w+ i {% {$ T% }2 L, y8 \! L4 j3 S! \3 s R[I] = A[I] - Carry; 0 @# I9 p3 V: c if (I < B.Len) R[I] -= B[I];" {: I' G& u( Z4 u. s if (R[I] < 0) Carry = 1, R[I] += Base;" H: x8 _& o$ W6 O' `% N3 s else Carry = 0;: @, V& |; S, r0 w2 y } ' H$ X' M, p6 D5 O: G while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;1 q2 N+ }9 o U return R;, e( ]* T1 l+ i7 y6 j }

xnum operator*(const xnum& A, const int B) 9 m, N6 Z: G% c{0 X! J+ o, z3 R6 p& t: n7 @- s int I;( g; I6 |6 s- P6 E7 b' H$ @ if (B == 0) return 0; 8 Y1 h# G+ O$ P5 k xnum R; 4 B/ T: V6 M! v hugeint Carry = 0;/ p; L9 ~( i; x8 z- i# O for (I = 0; I < A.Len || Carry > 0; I++)/ j6 |& Z' }* [- T( n) ? { + N1 B; W# U* L, K' r8 w J if (I < A.Len) Carry += hugeint(A[I]) * B; 9 f1 _' p5 C# @8 {) l9 ?, j6 L y R[I] = Carry % Base; 4 n0 {5 S! [( v0 k: S Carry /= Base; ' u2 s0 H$ Y3 }) y S' t } 9 o8 m0 p$ X ]( K R.Len = I;/ O. h1 D8 }$ [( o5 W( x! C return R;5 |/ `$ P- j) p, A+ u9 A& l }

xnum operator*(const xnum& A, const xnum& B) - B1 e8 O0 o) Y{ 0 w8 r- e! n6 ^% a+ M9 a9 `: u int I; " ~5 Z, |0 {8 V$ a/ S. A9 n! L8 E if (B.Len == 0) return 0;7 b4 ?1 `, ?. ?5 W* c xnum R;6 X- f1 h+ z0 W; w7 i* h* j for (I = 0; I < A.Len; I++) 9 k b$ S, C# S; M { " s0 k8 s/ y+ Q* ]" s7 p2 T# z hugeint Carry = 0; + _& g% k4 g* S1 d3 I for (int J = 0; J < B.Len || Carry > 0; J++) $ Z7 _9 Y1 @6 ]% V+ O, R5 t1 h {& x2 Y. i6 {- a% C# L4 t, u if (J < B.Len) Carry += hugeint(A[I]) * B[J]; N7 ?6 m$ [) } if (I + J < R.Len) Carry += R[I + J];5 V6 J8 t: X9 ~! s; y if (I + J >= R.Len) R[R.Len++] = Carry % Base;) E0 z2 h' m n) I' n5 E' }5 s8 {( l else R[I + J] = Carry % Base; 1 c7 F9 L, N0 I% {: X Carry /= Base;; F! d Q: V9 n } ) }' }, R# M' x* Z6 \6 j+ t/ m } ; ^- U( j7 j- q% R3 ] return R; ; i8 e/ h& s0 d& x# ?6 g}

xnum operator/(const xnum& A, const int B) - h% t4 j. m, V$ J7 A g{" {3 `6 C- p. ]! j r xnum R; % V: U# G! X: T W int I;4 k! q/ e. W8 u$ v hugeint C = 0; 3 P1 m6 T% N8 ]2 G# a- Y for (I = A.Len - 1; I >= 0; I--) $ I/ {& R% x$ e7 N5 b' q5 j, y { ' o& o# l a6 K. H C = C * Base + A[I];, |& E% L; p' L ~% _ R[I] = C / B; / I* b0 X7 L% o8 c& i% u) H0 n C %= B;, O) P3 g& e0 d: f; ? } ( ?2 E9 `9 B0 u- R+ d/ [ R.Len = A.Len;% l& Y) K$ i; Q6 O5 z& N while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--; / J$ ~( \% W. e0 l/ J return R; 1 y) @- _* ~7 L}

xnum operator/(const xnum& A, const xnum& B) ( h! A& r# o; n/ K{5 N4 ~# i5 w8 ]- H( N/ H int I; : I0 X w3 W' n1 u, | xnum R, Carry = 0; 4 ], e6 u6 K% E1 n int Left, Right, Mid; 0 R# ^1 v* q4 |6 F) ?4 i6 q2 k for (I = A.Len - 1; I >= 0; I--); O1 X1 @% @) e. S2 W {/ a/ P; Y3 c8 u( s7 R5 x1 B Carry = Carry * Base + A[I];" I* }/ \2 r" J* L Left = 0; " G) w% J" s4 @5 q0 u& ]' E3 I Right = Base - 1; b; L, Z. [7 g* _% M* y while (Left < Right)8 B# W0 n% Y1 b6 B& }3 ] {7 Q, f) k& a: U1 u1 z Mid = (Left + Right + 1) / 2; 9 n& f% C6 B: e6 F if (compare(B * Mid, Carry) <= 0) Left = Mid; & H0 l" m3 K: l; x0 U$ Q- N1 E else Right = Mid - 1;8 p) {' k6 N# w; d3 x, [ }7 F" f# u- h7 j; j/ K R[I] = Left; , `' p8 k4 ~0 X Carry = Carry - B * Left; + q1 ]3 `+ V5 U y }7 t. X: ?+ J1 E2 `% s8 z R.Len = A.Len;8 [3 y0 i4 Z- O( p- N) X3 y* T while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;/ J5 d0 A7 S! G return R;' p% Z9 N. k! L& _( N5 n. x8 o& q } 3 t& B5 c3 C" g V! c' O. Z* _# Sxnum operator%(const xnum& A, const xnum& B) 5 C% G" v* u. S; w; K{% f* h+ y) k$ E0 z9 w) A/ N" d int I; % J1 Y6 Q" H( L2 v# n6 ]+ D xnum R, Carry = 0;+ ^, B7 k# l* R* s( J" M int Left, Right, Mid; % y% y& c7 P) Q* {6 W for (I = A.Len - 1; I >= 0; I--)" n& M {) d, V' H' e { . y% z3 `8 ^% F! D- k: p0 U Carry = Carry * Base + A[I];) a+ |" ?, y; d5 b+ v) v! A Left = 0; + V( x6 y* w1 d% W- \% d Right = Base - 1; ! @9 K3 |3 C2 ]/ K0 A% b c while (Left < Right) - z5 }" T9 l0 t7 O+ \. D9 `2 u( j { $ p) Q3 J4 n* W Mid = (Left + Right + 1) / 2;! a3 F2 H: {0 j1 m if (compare(B * Mid, Carry) <= 0) Left = Mid;& Q- c$ ?) a+ `, F else Right = Mid - 1;- H1 g) j l0 ]! `% P }0 S$ i# B' E) \/ r" g: c1 P8 i, E* T7 n R[I] = Left; & v4 D9 ^9 ?- W- C* s; h Carry = Carry - B * Left; 4 R3 F7 m) ^0 F7 ` }% K# Y- H/ l; r' Q R.Len = A.Len;2 a- M1 j" {9 l7 i while (R.Len > 0 && R[R.Len - 1] == 0) R.Len--;8 j5 H6 Z% Y# h6 r( u4 U return Carry; % Q$ s# v3 i& P& v8 w}

istream& operator>>(istream& In, xnum& V)/ z. E w) R8 g! p& Y. u) W { , q+ F) B/ U6 k/ w5 y3 e) J char Ch;3 _7 c' \% V) K! H. ` for (V = 0; In >> Ch;); { H- M4 W" i7 s5 m( o7 T) K { , k; c: f% e: M ?- L' D4 c V = V * 10 + (Ch - '0'); # `9 @, t" v# z7 i$ c if (cin.peek() <= ' ') break; , m( o2 D( n4 r4 |5 n0 A } 4 f/ H; {! m6 B& T L9 D4 e return In;: X" L& _. Q5 N1 p! q }

ostream& operator<<(ostream& Out, const xnum& V) * H3 l/ E, \7 R& \. O0 {2 {{2 u8 a- S, a" V int I;. P+ [9 s/ P3 P; Y; E7 v" R' A" V3 F Out << (V.Len == 0 ? 0 : V[V.Len - 1]); " y- n" s4 {# r, I! ^ for (I = V.Len - 2; I >= 0; I--) for (int J = Base / 10; J > 0; J /= 10) Out << V[I] / J % 10; , c* f6 \9 z0 t# T return Out; 0 d3 m$ x7 I2 K; X8 F- S& ]} , j: @( F* F7 d

回复

使用道具 举报

0

主题

2

听众

32

积分

升级  28.42%

该用户从未签到

新人进步奖

回复

使用道具 举报

cshdzxjtu        

2

主题

2

听众

38

积分

升级  34.74%

该用户从未签到

新人进步奖

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2025-11-12 20:21 , Processed in 1.937235 second(s), 73 queries .

回顶部