BigInt

 

 

 

const int N = 101;
#include <string>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
const int NMAXBITS = 128;
const int NBASE  = 100000000;
const int NSLEN = 8;
typedef int bn_t[NMAXBITS];

class BigInt
{
public:
 BigInt();
 BigInt(int n);
 BigInt(const char *);
    BigInt(const string &s);
// BigInt(BigInt &bt);
public:
 BigInt &operator = (const int &n);
 BigInt &operator = (const BigInt &bt);
 BigInt &operator = (const char *ps);
 BigInt &operator = (const string &s);
    bool operator == (const int &n) const;   
 bool operator == (const BigInt &bt) const;
    bool operator != (const int &n) const;
    bool operator != (const BigInt &bt) const;
    bool operator > (const int &n) const;
    bool operator > (const BigInt &bt) const;
    bool operator >= (const int &n) const;
    bool operator >= (const BigInt &bt) const;
    bool operator < (const int &n) const;
    bool operator < (const BigInt &bt) const;
    bool operator <= (const int &n) const;
    bool operator <= (const BigInt &bt) const;

 BigInt operator -();
 BigInt operator +(const int &n);
 BigInt operator +(const BigInt &bt);
 BigInt& operator +=(const int &n);
 BigInt& operator +=(const BigInt &bt);
    BigInt operator -(const int &n);
    BigInt operator -(const BigInt &bt);
 BigInt& operator -=(const int &n);
 BigInt& operator -=(const BigInt &bt);

 BigInt operator *(const int &n);
 BigInt operator *(const BigInt &bt);
 BigInt& operator *=(const int &n);
 BigInt& operator *=(const BigInt &bt);

 BigInt operator /(const int &n);
 BigInt operator /(const BigInt &bt);

    /*
 BigInt &operator /(BigInt &bt);
 BigInt &operator %(BigInt &bt);
 BigInt &operator **(BigInt &bt);
 */
 void Display();
// BigInt &operator ++();
// BigInt &operator --();
// BigInt operator ++(int);
// BigInt operator --(int);
protected:
 int& operator [] (const int index) const;
private:
 BigInt(int nn[], int n1, int n2);
 BigInt SubInt(int n1, int n2);
 void Init();
 bn_t  bn;
};

void BigInt::Init()
{
 for(int i = 0; i < NMAXBITS; i++) bn[i] = 0;
}

BigInt::BigInt()
{
 Init();
}

BigInt::BigInt(int n)
{
 *this = n;
}

BigInt::BigInt(int nn[], int n1, int n2)
{
 int i = 0,j = 0;
 for(i = n1; i <= n2; i++) {
        bn[j++] = nn[i];
 }

 if(j) bn[NMAXBITS - 1] = j - 1;
}

BigInt::BigInt(const char *s)
{
 int *pt = (int *)bn;
 int nlen = 0;
 int sign = 0, index = -1;
 Init();
 if(!s || !(nlen = (int)strlen(s))) {
      pt[0] = 0;  pt[NMAXBITS - 1] = 0;
 }
 else {
  char *pch = (char *)s, *pch1 = 0;
  char sn[NSLEN + 1];
  if(s[0] == '-') {sign = 0x80000000; nlen--;}
  if(s[0] == '-' || s[0] == '+') pch++;
  while((*pch) == '0') {pch++; nlen--;}
  
  if(nlen <= 0) {sign = 0; index = 0;}
  pch += nlen;
  while(nlen > 0) {
   int nlen1 = nlen - NSLEN;

   if(nlen1 > 0) pch1 = pch - NSLEN;
   else          pch1 = pch - nlen;
   nlen1 = (int) (pch - pch1);
            memcpy_s(sn, NSLEN + 1, pch1, nlen1);
            nlen -= NSLEN;
   sn[nlen1] = 0;
   pt[++index] = atoi(sn);
   pch = pch1;
  }
  pt[NMAXBITS - 1] = sign | index;
 }
}

BigInt::BigInt(const string &s)
{
    int nlen = (int)s.length();
 char *pch = new char[nlen + 1];
 memcpy_s(pch, nlen, s.c_str(), nlen);
 pch[nlen] = 0x00;
 Init();
    *this = (const char *)pch;
 delete [] pch;
}

int& BigInt::operator [] (int index) const
{
 return *(int *)&bn[index];
}

bool BigInt::operator == (const int &n) const
{
 int sign1 = bn[NMAXBITS - 1] & 0x80000000 >> 31;
 int sign2 = (n >= 0 ? 0: 1);
 if(sign1 ^ sign2) return false;

 BigInt bt(n);
 return *this == bt;
}

bool BigInt::operator == (const BigInt &bt) const
{
 int sign1 = bn[NMAXBITS - 1] & 0x80000000 >> 31;
 int sign2 = bt[NMAXBITS - 1] & 0x80000000 >> 31;
 int index1 = bn[NMAXBITS - 1] & 0x7FFFFFFF;
 int index2 = bt[NMAXBITS - 1] & 0x7FFFFFFF;
 if((sign1 ^ sign2) || index1 != index2) return false;

 for(int i = 0; i < index1 + 1; i++) {
  if(bn[i] != bt[i]) return false;
 }

 return true;
}

bool BigInt::operator != (const int &n) const
{
 return !(*this == n);
}

bool BigInt::operator != (const BigInt &bt) const
{
    return !(*this == bt);
}


bool BigInt::operator > (const int &n) const
{
 int sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
 int sign2 = (n >= 0 ? 0 : 1);
 if(sign1 ^ sign2) {
     if(sign1) return false;
  else      return true;
 }
 else { 
  BigInt bt(n);
  return *this > bt;
 }
}

bool BigInt::operator > (const BigInt &bt) const
{
 bool bflag = false;
 int sign1, sign2, index1, index2, index;

 sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
 sign2 = (bt[NMAXBITS - 1] & 0x80000000) >> 31;
    index1 = bn[NMAXBITS - 1] &0x7FFFFFFF;
    index2 = bt[NMAXBITS - 1] &0x7FFFFFFF;
    index = max(index1, index2);
 if(sign1 ^ sign2) {
     if(sign1) return false;
  else      return true;
 }
 else {
  int i = 0;
  for(i = index; i >= 0; i--) {
   if(bn[i] != bt[i]) {
    if(bn[i] < bt[i]) {
     if(sign1) return true;
     else      return false;
    }
    else {
     if(sign1) return false;
     else      return true;
    }
   }
  }
  if(i < 0) return false;
 }

 return false;
}

bool BigInt::operator >= (const int &n) const
{
 int sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
 int sign2 = (n >= 0 ? 0 : 1);
 if(sign1 ^ sign2) {
     if(sign1) return false;
  else      return true;
 }
 else { 
  BigInt bt(n);
  return *this >= bt;
 }
}

bool BigInt::operator >= (const BigInt &bt) const
{
 return (*this == bt || *this > bt);
}

bool BigInt::operator < (const int &n) const
{
 int sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
 int sign2 = (n >= 0 ? 0 : 1);
 if(sign1 ^ sign2) {
     if(sign1) return true;
  else      return false;
 }
 else { 
  BigInt bt(n);
  return *this < bt;
 }
}

bool BigInt::operator < (const BigInt &bt) const
{
 return !(*this >= bt);
}

bool BigInt::operator <= (const int &n) const
{
 BigInt bt(n);
 return *this <= bt;
}

bool BigInt::operator <= (const BigInt &bt) const
{
 return !(*this > bt);
}

BigInt & BigInt::operator =(const int &n)
{
 int sign = 0;
 int q = 0, r = 0, n1 = n;

 Init();
 if(n < 0) {
  sign = 1;
     n1 = -n1;
 }
    q = n1 / NBASE; r = n1 % NBASE;
 if(q > 0) { bn[NMAXBITS - 1] = 1; bn[1] = q;}
 bn[0] = r;

 bn[NMAXBITS - 1] |= (sign << 31);

 return *this;
}

BigInt & BigInt::operator =(const BigInt &bt)
{
 int index = bt[NMAXBITS - 1] & 0x7FFFFFFF;
 Init();
 bn[NMAXBITS - 1] = bt[NMAXBITS - 1];
 for(int i = 0; i < index + 1; i++) {
         bn[i] = bt[i];
 }
 return *this;
}

BigInt& BigInt::operator =(const char *ps)
{
 BigInt bt(ps);
 *this = bt;

 return *this;
}

BigInt& BigInt::operator =(const string &s)
{
 BigInt bt(s);
 *this = bt;

 return *this;
}

BigInt BigInt::operator -()
{
 BigInt rbt;
 rbt = *this;
 int sign = (rbt[NMAXBITS - 1] & 0x80000000) + 0x80000000;
 rbt[NMAXBITS - 1] = sign | (rbt[NMAXBITS - 1] & 0x7FFFFFFF);
   
 return rbt;
}

BigInt BigInt::operator +(const int &n)
{
 BigInt rbt, rbt1(n);
   
 rbt = *this + rbt1;

 return rbt;
}

BigInt BigInt::operator +(const BigInt &bt)
{
 BigInt rbt;
 int sign1 = (bn[NMAXBITS - 1] & 0x80000000);
 int sign2 = (bt[NMAXBITS - 1] & 0x80000000);
    int index1 = bn[NMAXBITS - 1] & 0x7FFFFFFF;
    int index2 = bt[NMAXBITS - 1] & 0x7FFFFFFF;
    int index = 0, sign = 0;
 index = max(index1, index2);
    sign = sign1;
 if(sign1 == sign2) {
  int sum = 0, q = 0,r = 0;
  for(int i = 0; i <= index; i++) {
   sum = bn[i] + bt[i] + q;
   q = sum / NBASE;
   r = sum % NBASE;
            rbt[i] = r;
  }
  if(q > 0) rbt[++index] = q;
        rbt[NMAXBITS - 1] = sign | index;
 }
 else {
  BigInt bt1 = *this, bt2 = bt;
  bt1[NMAXBITS - 1] = index1;
  bt2[NMAXBITS - 1] = index2;
  if(sign1) rbt = bt2 - bt1;
  else      rbt = bt1 - bt2;
 }

 return rbt;
}

BigInt& BigInt::operator +=(const int &n)
{
 *this = *this + BigInt(n);

 return *this;
}

BigInt& BigInt::operator +=(const BigInt &bt)
{
 *this = *this + bt;

 return *this;
}

BigInt BigInt::operator -(const int &n)
{
 BigInt rbt;
    rbt = *this;

 rbt = rbt - BigInt(n);

 return rbt;
}

BigInt BigInt::operator -(const BigInt &bt)
{
 BigInt rbt, bt1 = *this, bt2 = bt;
    int sign1 = (bt1[NMAXBITS - 1] & 0x80000000) >> 31;
    int sign2 = (bt2[NMAXBITS - 1] & 0x80000000) >> 31;
    int index1 = bt1[NMAXBITS - 1] & 0x7FFFFFFF;
    int index2 = bt2[NMAXBITS - 1] & 0x7FFFFFFF;
    bt1[NMAXBITS - 1] = index1;
 bt2[NMAXBITS - 1] = index2;
 int index = 0, sign = 0;
 index = max(index1, index2);
 if(sign1 ^ sign2) {
  rbt = bt1 + bt2;
  rbt[NMAXBITS - 1] |= (sign1 << 31);
 }
 else {
  if(sign1) {
   rbt = bt2 - bt1;
  }
  else  {
   int sub = 0, rt = 0, ind = 0;
   int *pbt1 = &bt1[0], *pbt2 = &bt2[0];
   if(bt1 >= bt2) sign = 0;
   else           {sign = 1; pbt1 = &bt2[0]; pbt2 = &bt1[0];}
   for(int i = 0; i <= index; i++) {
    sub = 0;
    if(pbt1[i] < pbt2[i]) sub = NBASE;
                sub = sub + pbt1[i] - pbt2[i] - rt;
    rbt[i] = sub;
    if(pbt1[i] < pbt2[i]) rt = 1;
    else                  rt = 0;
    if(rbt[i]) ind = i;
   }
   rbt[NMAXBITS - 1] = (sign << 31) | ind;
  }
 }

 return rbt;
}

BigInt& BigInt::operator -=(const int &n)
{
 *this = *this - BigInt(n);

 return *this;
}

BigInt& BigInt::operator -=(const BigInt &bt)
{
 *this = *this - bt;

 return *this;
}

BigInt BigInt::operator *(const int &n)
{
      BigInt bt(n);

   return *this * bt;
}

BigInt BigInt::operator *(const BigInt &bt)
{
 BigInt rbt;
    int sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
    int sign2 = (bt[NMAXBITS - 1] & 0x80000000) >> 31;
    int index1 = bn[NMAXBITS - 1] & 0x7FFFFFFF;
    int index2 = bt[NMAXBITS - 1] & 0x7FFFFFFF;
    int sign = 0, index = index1 + index2, h = 0;
 sign = (sign1 + sign2) % 2;
 if(*this == 0 || bt == 0) return rbt;
 __int64 product = 0;
 __int64 q = 0, r = 0, qr = 0;
 for(int i = index2; i >= 0; i--) {
     for(int j = index1; j >= 0; j--) {
   q = bn[j]; r = bt[i];
   //product = (bn[j]) * (bt[i]);
   product = q * r;
   q = product / NBASE;
   r = product % NBASE;
   h = i + j - 1;
   qr = r;
   do {
    h++;
                product = rbt[h] + qr;
    qr = product / NBASE;
    r = product % NBASE;
       rbt[h] = r;
   } while(qr);
   if(h > index) index = h;
   h = i + j;
   if(q) {
    do {
        h++;
        product = rbt[h] + q;
           q = product / NBASE;
           r = product % NBASE;
     rbt[h] = r;
    } while(q);
   }
   if(h > index) index = h;
     }
 }
    rbt[NMAXBITS - 1] = (sign << 31) | index;

 return rbt;
}

BigInt& BigInt::operator *=(const int &n)
{
 *this = *this * BigInt(n);

 return *this;
}

BigInt& BigInt::operator *=(const BigInt &bt)
{
 *this = *this * bt;

 return *this;
}

BigInt BigInt::operator /(const int &n)
{
 return *this / BigInt(n);
}

///???????????///
BigInt BigInt::operator /(const BigInt &bt)
{
 BigInt qbt, rbt;
    int sign1 = (bn[NMAXBITS - 1] & 0x80000000) >> 31;
    int sign2 = (bt[NMAXBITS - 1] & 0x80000000) >> 31;
    int index1 = bn[NMAXBITS - 1] & 0x7FFFFFFF;
    int index2 = bt[NMAXBITS - 1] & 0x7FFFFFFF;
 int sign = 0, index = 0;

 if(bt == 0) {
        qbt[NMAXBITS - 1] = 0x80000000 | 0x7FFFFFFF;
 }
 else {
  qbt = *this;
  qbt[NMAXBITS - 1] = index1;
  rbt = bt;
  rbt[NMAXBITS - 1] = index2;
  if(qbt < rbt) {
   qbt = 0;
  }
  else {
   __int64 q = 0;
   BigInt qbn, tbt = qbt.SubInt(index1, index1 - index2); int j = 0;
   for(int i = index1 - index2; i >= 0; i--) {
    j = max(tbt[NMAXBITS - 1], index2);
    if(!tbt[j]) tbt = tbt * NBASE + (*this)[i];
    else {
     q = tbt[j] / rbt[index2];
     if(!q) {
      tbt = tbt * NBASE + (*this)[i];
      j++;
      q = tbt[j];
      q = q * NBASE;
      q += tbt[j - 1];
     }
     else q = tbt[j];
     q = q / rbt[index2];
     qbn = tbt;
     tbt = qbn - rbt * q;
     while(tbt < 0) {
      q--;
      tbt = qbn - rbt *q;
     }
     qbt[i] = q;
     if(index < i) index = i;
    }
   }
  }
  if(sign1 ^ sign2) sign = 1;
  qbt[NMAXBITS - 1] = (sign << 31) | index;
 }

 return qbt;
}

BigInt BigInt::SubInt(int n1, int n2)
{
 BigInt bt;

 int i = 0,j = 0;
 for(i = n2; i <= n1; i++) {
        bt[j++] = bn[i];
 }

 if(j) bt[NMAXBITS - 1] = j - 1;

 return bt;
}

void BigInt::Display()
{
 int sign = bn[NMAXBITS - 1] & 0x80000000;
 int index = bn[NMAXBITS - 1] & 0x7FFFFFFF;
 if(sign) printf("-");
 printf("%d", bn[index]);
 for(int i = index - 1; i >= 0; i--) {
         printf("%08d", bn[i]);
 }
 printf("\n");
 
}

BigInt bt[N];
int main()
{
    int n = 0;
 bt[0] = "1"; bt[1] = "1"; bt[2] = "2"; bt[3] = "5";
 for(int i = 4; i < N; i++) {
  for(int j = 0; j < i; j++) {
   bt[i] = bt[i] + bt[j] * bt[i - j - 1];
  }
 }

    while(EOF != scanf("%d", &n), n!= -1)
    {
  bt[n].Display();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值