xuefu998 发表于 2005-2-1 16:07

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

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

aftermath 发表于 2005-2-1 23:57

<P>高精度,用一个线性表保存一个大整数,具体说,将大整数写成p进制数,线性表的每一项存p进制其中一位。</P><P>参考下面代码</P><P>#include &lt;iostream&gt;
#include &lt;memory&gt;
# include &lt;string&gt;
using namespace std;</P><P>typedef long long hugeint;</P><P>const int Base = 1000000000;
const int Capacity = 200;</P><P>struct xnum
{
int Len;
int Data;
xnum() : Len(0) {}
xnum(const xnum&amp; V) : Len(V.Len) { memcpy(Data, V.Data, Len * sizeof *Data); }
xnum(int V) : Len(0) { for (; V &gt; 0; V /= Base) Data = V % Base; }
xnum&amp; operator=(const xnum&amp; V) { Len = V.Len; memcpy(Data, V.Data, Len * sizeof *Data); return *this; }
int&amp; operator[](int Index) { return Data; }
int operator[](int Index) const { return Data; }
};</P><P>
int compare(const xnum&amp; A, const xnum&amp; B)
{
    int I;
if (A.Len != B.Len) return A.Len &gt; B.Len ? 1 : -1;
for (I = A.Len - 1; I &gt;= 0 &amp;&amp; A == B; I--);
if (I &lt; 0) return 0;
return A &gt; B ? 1 : -1;
}</P><P>xnum operator+(const xnum&amp; A, const xnum&amp; B)
{
xnum R;
int I;
int Carry = 0;
for (I = 0; I &lt; A.Len || I &lt; B.Len || Carry &gt; 0; I++)
{
  if (I &lt; A.Len) Carry += A;
  if (I &lt; B.Len) Carry += B;
  R = Carry % Base;
  Carry /= Base;
}
R.Len = I;
return R;
}</P><P>xnum operator-(const xnum&amp; A, const xnum&amp; B)
{
xnum R;
int Carry = 0;
R.Len = A.Len;
int I;
for (I = 0; I &lt; R.Len; I++)
{
  R = A - Carry;
  if (I &lt; B.Len) R -= B;
  if (R &lt; 0) Carry = 1, R += Base;
  else Carry = 0;
}
while (R.Len &gt; 0 &amp;&amp; R == 0) R.Len--;
return R;
}</P><P>xnum operator*(const xnum&amp; A, const int B)
{
    int I;
if (B == 0) return 0;
xnum R;
hugeint Carry = 0;
for (I = 0; I &lt; A.Len || Carry &gt; 0; I++)
{
  if (I &lt; A.Len) Carry += hugeint(A) * B;
  R = Carry % Base;
  Carry /= Base;
}
R.Len = I;
return R;
}</P><P>xnum operator*(const xnum&amp; A, const xnum&amp; B)
{
    int I;
if (B.Len == 0) return 0;
xnum R;
for (I = 0; I &lt; A.Len; I++)
{
  hugeint Carry = 0;
  for (int J = 0; J &lt; B.Len || Carry &gt; 0; J++)
  {
   if (J &lt; B.Len) Carry += hugeint(A) * B;
   if (I + J &lt; R.Len) Carry += R;
   if (I + J &gt;= R.Len) R = Carry % Base;
   else R = Carry % Base;
   Carry /= Base;
  }
}
return R;
}</P><P>xnum operator/(const xnum&amp; A, const int B)
{
xnum R;
int I;
hugeint C = 0;
for (I = A.Len - 1; I &gt;= 0; I--)
{
  C = C * Base + A;
  R = C / B;
  C %= B;
}
R.Len = A.Len;
while (R.Len &gt; 0 &amp;&amp; R == 0) R.Len--;
return R;
}</P><P>xnum operator/(const xnum&amp; A, const xnum&amp; B)
{
    int I;
xnum R, Carry = 0;
int Left, Right, Mid;
for (I = A.Len - 1; I &gt;= 0; I--)
{
  Carry = Carry * Base + A;
  Left = 0;
  Right = Base - 1;
  while (Left &lt; Right)
  {
   Mid = (Left + Right + 1) / 2;
   if (compare(B * Mid, Carry) &lt;= 0) Left = Mid;
   else Right = Mid - 1;
  }
  R = Left;
  Carry = Carry - B * Left;
}
R.Len = A.Len;
while (R.Len &gt; 0 &amp;&amp; R == 0) R.Len--;
return R;
}
xnum operator%(const xnum&amp; A, const xnum&amp; B)
{
    int I;
xnum R, Carry = 0;
int Left, Right, Mid;
for (I = A.Len - 1; I &gt;= 0; I--)
{
  Carry = Carry * Base + A;
  Left = 0;
  Right = Base - 1;
  while (Left &lt; Right)
  {
   Mid = (Left + Right + 1) / 2;
   if (compare(B * Mid, Carry) &lt;= 0) Left = Mid;
   else Right = Mid - 1;
  }
  R = Left;
  Carry = Carry - B * Left;
}
R.Len = A.Len;
while (R.Len &gt; 0 &amp;&amp; R == 0) R.Len--;
return Carry;
}</P><P>istream&amp; operator&gt;&gt;(istream&amp; In, xnum&amp; V)
{
char Ch;
for (V = 0; In &gt;&gt; Ch;)
{
  V = V * 10 + (Ch - '0');
  if (cin.peek() &lt;= ' ') break;
}
return In;
}</P><P>ostream&amp; operator&lt;&lt;(ostream&amp; Out, const xnum&amp; V)
{
    int I;
Out &lt;&lt; (V.Len == 0 ? 0 : V);
for (I = V.Len - 2; I &gt;= 0; I--) for (int J = Base / 10; J &gt; 0; J /= 10) Out &lt;&lt; V / J % 10;
return Out;
}
</P>

wangjiaqi49 发表于 2005-8-1 11:16

神人也,外星人!!太牛了

cshdzxjtu 发表于 2005-8-12 21:46

强!
页: [1]
查看完整版本: 谁有超长整数运算的好算法?