Catlan big number template, catlan large number Template

Source: Internet
Author: User

Catlan big number template, catlan large number Template
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <queue>
# Include <stdlib. h>
# Include <string. h>
# Include <iomanip>
# Define N 500010
# Define INF 10000000
# Define LL long
# Define eps 10E-9
# Define mem (a) memset (a, 0, sizeof ())
# Define w (a) while ()
# Define s (a) scanf ("% d", &)
# Define ss (a, B) scanf ("% d", & a, & B)
# Define sss (a, B, c) scanf ("% lld", & a, & B, & c)
# Define maxn9999
# Define MAXSIZE 10
# Define DLEN 4
Using namespace std;
Class BigNum
{
Private:
Int a [500]; // number of digits that can be controlled for a large number
Int len; // the length of a large number.
Public:
BigNum () {len = 1; memset (a, 0, sizeof (a);} // Constructor
BigNum (const int); // converts an int type variable to a large number.
BigNum (const char *); // converts a variable of the string type to a large number.
BigNum (const BigNum &); // copy the constructor
BigNum & operator = (const BigNum &); // overload the value assignment operator to assign values between large numbers.


Friend istream & operator> (istream &, BigNum &); // overload the input operator
Friend ostream & operator <(ostream &, BigNum &); // reload the output operator


BigNum operator + (const BigNum &) const; // overload addition operator, the addition operation between two large numbers
BigNum operator-(const BigNum &) const; // overload subtraction operator, subtraction between two large numbers
BigNum operator * (const BigNum &) const; // overload multiplication operator, multiplication between two large numbers
BigNum operator/(const int &) const; // overload division operator. A large number is used to divide an integer.


BigNum operator ^ (const int &) const; // nth power operation of large numbers
Int operator % (const int &) const; // a large number of Modulo operations on an int Type Variable
Bool operator> (const BigNum & T) const; // compare the size of a large number with that of another one
Bool operator> (const int & t) const; // compare the size of a large number and an int Type Variable


Void print (); // output large number
};
BigNum: BigNum (const int B) // convert an int type variable to a large number
{
Int c, d = B;
Len = 0;
Memset (a, 0, sizeof ());
While (d> MAXN)
{
C = d-(d/(MAXN + 1) * (MAXN + 1 );
D = d/(MAXN + 1 );
A [len ++] = c;
}
A [len ++] = d;
}
BigNum: BigNum (const char * s) // converts a variable of the string type to a large number.
{
Int t, k, index, l, I;
Memset (a, 0, sizeof ());
L = strlen (s );
Len = l/DLEN;
If (l % DLEN)
Len ++;
Index = 0;
For (I = L-1; I> = 0; I-= DLEN)
{
T = 0;
K = I-DLEN + 1;
If (k <0)
K = 0;
For (int j = k; j <= I; j ++)
T = t * 10 + s [j]-'0 ';
A [index ++] = t;
}
}
BigNum: BigNum (const BigNum & T): len (T. len) // copy the constructor
{
Int I;
Memset (a, 0, sizeof ());
For (I = 0; I <len; I ++)
A [I] = T. a [I];
}
BigNum & BigNum: operator = (const BigNum & n) // reload the value assignment operator and assign values between large numbers.
{
Int I;
Len = n. len;
Memset (a, 0, sizeof ());
For (I = 0; I <len; I ++)
A [I] = n. a [I];
Return * this;
}
Istream & operator> (istream & in, BigNum & B) // reload the input operator
{
Char ch [MAXSIZE * 4];
Int I =-1;
In> ch;
Int l = strlen (ch );
Int count = 0, sum = 0;
For (I = L-1; I> = 0 ;)
{
Sum = 0;
Int t = 1;
For (int j = 0; j <4 & I> = 0; j ++, I --, t * = 10)
{
Sum + = (ch [I]-'0') * t;
}
B. a [count] = sum;
Count ++;
}
B. len = count ++;
Return in;


}
Ostream & operator <(ostream & out, BigNum & B) // reload the output operator
{
Int I;
Cout <B. a [B. len-1];
For (I = B. len-2; I> = 0; I --)
{
Cout. width (DLEN );
Cout. fill ('0 ');
Cout <B. a [I];
}
Return out;
}


BigNum: operator + (const BigNum & T) const // sum operation between two large numbers
{
BigNum t (* this );
Int I, big; // number of digits
Big = T. len> len? T. len: len;
For (I = 0; I <big; I ++)
{
T. a [I] + = T. a [I];
If (t. a [I]> MAXN)
{
T. a [I + 1] ++;
T. a [I]-= MAXN + 1;
}
}
If (t. a [big]! = 0)
T. len = big + 1;
Else
T. len = big;
Return t;
}
BigNum: operator-(const BigNum & T) const // subtraction between two large numbers
{
Int I, j, big;
Bool flag;
BigNum t1, t2;
If (* this> T)
{
T1 = * this;
T2 = T;
Flag = 0;
}
Else
{
T1 = T;
T2 = * this;
Flag = 1;
}
Big = t1.len;
For (I = 0; I <big; I ++)
{
If (t1.a [I] <t2.a [I])
{
J = I + 1;
While (t1.a [j] = 0)
J ++;
T1.a [j --] --;
While (j> I)
T1.a [j --] + = MAXN;
T1.a [I] + = MAXN + 1-t2.a [I];
}
Else
T1.a [I]-= t2.a [I];
}
T1.len = big;
While (t1.a [len-1] = 0 & t1.len> 1)
{
T1.len --;
Big --;
}
If (flag)
T1.a [big-1] = 0-t1.a [big-1];
Return t1;
}


BigNum: operator * (const BigNum & T) const // multiplication between two large numbers
{
BigNum ret;
Int I, j, up;
Int temp, temp1;
For (I = 0; I <len; I ++)
{
Up = 0;
For (j = 0; j <T. len; j ++)
{
Temp = a [I] * T. a [j] + ret. a [I + j] + up;
If (temp> MAXN)
{
Temp1 = temp-temp/(MAXN + 1) * (MAXN + 1 );
Up = temp/(MAXN + 1 );
Ret. a [I + j] = temp1;
}
Else
{
Up = 0;
Ret. a [I + j] = temp;
}
}
If (up! = 0)
Ret. a [I + j] = up;
}
Ret. len = I + j;
While (ret. a [ret. len-1] = 0 & ret. len> 1)
Ret. len --;
Return ret;
}
BigNum: operator/(const int & B) const // a big number is used to divide an integer.
{
BigNum ret;
Int I, down = 0;
For (I = len-1; I> = 0; I --)
{
Ret. a [I] = (a [I] + down * (MAXN + 1)/B;
Down = a [I] + down * (MAXN + 1)-ret. a [I] * B;
}
Ret. len = len;
While (ret. a [ret. len-1] = 0 & ret. len> 1)
Ret. len --;
Return ret;
}
Int BigNum: operator % (const int & B) const // a large number of Modulo operations on an int Type Variable
{
Int I, d = 0;
For (I = len-1; I> = 0; I --)
{
D = (d * (MAXN + 1) % B + a [I]) % B;
}
Return d;
}
BigNum: operator ^ (const int & n) const // Npower operation for large numbers
{
BigNum t, ret (1 );
Int I;
If (n <0)
Exit (-1 );
If (n = 0)
Return 1;
If (n = 1)
Return * this;
Int m = n;
While (m> 1)
{
T = * this;
For (I = 1; I <1 <= m; I <= 1)
{
T = t * t;
}
M-= I;
Ret = ret * t;
If (m = 1)
Ret = ret * (* this );
}
Return ret;
}
Bool BigNum: operator> (const BigNum & T) const // compare the size of a large number with that of another one
{
Int ln;
If (len> T. len)
Return true;
Else if (len = T. len)
{
Ln = len-1;
While (a [ln] = T. a [ln] & ln> = 0)
Ln --;
If (ln> = 0 & a [ln]> T. a [ln])
Return true;
Else
Return false;
}
Else
Return false;
}
Bool BigNum: operator> (const int & t) const // compare the size of a large number and an int Type Variable
{
BigNum B (t );
Return * this> B;
}


Void BigNum: print () // output large number
{
Int I;
Cout <a [len-1];
For (I = len-2; I> = 0; I --)
{
Cout. width (DLEN );
Cout. fill ('0 ');
Cout <a [I];
}
Cout <endl;
}
Int main ()
{
Int I, n;
BigNum x [101]; // an array of objects defining large numbers
X [0] = 1;
For (I = 1; I <101; I ++)
X [I] = x [I-1] * (4 * I-2)/(I + 1 );
While (scanf ("% d", & n) = 1 & n! =-1)
{
X [n]. print ();
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.