Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const int K = 10000;//Each bit in the array represents 1W
- const int M = 300;//10 digits in total
- const char show [] = "% 04lld";
- struct Bignum
- {
- LL a [M * 2];//array of large numbers
- int len;//length
- bool negative;//positive negative
- Bignum ()
- {
- clear ();
- }
- void clear ()
- {
- len = 0;
- negative = false;
- memset (a, 0, sizeof (a));
- }
- Bignum (LL num)
- {
- * this = num;
- }
- Bignum operator = (LL num)
- {
- clear ();
- if (num <0) negative = true, num = -num;
- while (num)
- a [len ++] = num% K, num/= K;
- return * this;
- }
- Bignum (const Bignum & cmp)
- {
- memcpy (this, & cmp, sizeof (Bignum));
- }
- Bignum operator = (const Bignum & cmp)
- {
- memcpy (this, & cmp, sizeof (Bignum));
- return * this;
- }
- int absCmp (const Bignum & cmp)
- {
- if (len! = cmp.len)
- return len> cmp.len? 1: -1;
- for (int i = len-1; i> = 0; i--)
- if (a [i]! = cmp.a [i])
- return a [i]> cmp.a [i]? 1: -1;
- return 0;
- }
- int absCmp (LL num)
- {
- Bignum cmp (num);
- return absCmp (cmp);
- }
- bool operator <(const Bignum & cmp)
- {
- if (negative ^ cmp.negative)
- return negative? true: false;
- if (negative)
- return absCmp (cmp)> 0;
- else
- return absCmp (cmp) <0;
- }
- bool operator <(LL num)
- {
- Bignum cmp (num);
- return * this <cmp;
- }
- bool operator == (const Bignum & cmp)
- {
- if (negative ^ cmp.negative)
- return false;
- return absCmp (cmp) == 0;
- }
- bool operator == (LL num)
- {
- Bignum cmp (num);
- return * this == cmp;
- }
- void absAdd (const Bignum & one, const Bignum & two)
- {
- len = max (one.len, two.len);
- for (int i = 0; i <len; i ++)
- {
- a [i] + = one.a [i] + two.a [i];
- if (a [i]> = K) a [i]-= K, a [i + 1] ++;
- }
- if (a [len]) len ++;
- }
- void absSub (const Bignum & one, const Bignum & two)
- {
- len = one.len;
- for (int i = 0; i <len; i ++)
- {
- a [i] + = one.a [i] -two.a [i];
- if (a [i] <0) a [i + 1]-, a [i] + = K;
- }
- while (len> 0 && a [len-1] == 0) len--;
- }
- void absMul (const Bignum & one, const Bignum & two)
- {
- len = one.len + two.len;
- for (int i = 0; i <one.len; i ++) for (int j = 0; j <two.len; j ++)
- a [i + j] + = one.a [i] * two.a [j];
- for (int i = 0; i <len; i ++) if (a [i]> = K)
- a [i + 1] + = a [i]/K, a [i]% = K;
- while (len> 0 && a [len-1] == 0) len--;
- }
- Bignum operator + (const Bignum & cmp)
- {
- Bignum c;
- if (negative ^ cmp.negative)
- {
- bool res = absCmp (cmp)> 0;
- c.negative =! (negative ^ res);
- if (res)
- c.absSub (* this, cmp);
- else
- c.absSub (cmp, * this);
- }
- else if (negative)
- {
- c.negative = true;
- c.absAdd (* this, cmp);
- }
- else
- {
- c.absAdd (* this, cmp);
- }
- return c;
- }
- Bignum operator- (const Bignum & cmp)
- {
- Bignum cpy;
- if (cpy == cmp)
- return * this;
- else
- cpy = cmp, cpy.negative ^ = true;
- return * this + cpy;
- }
- Bignum operator * (const Bignum & cmp)
- {
- Bignum c;
- if (c == cmp || c == * this)
- return c;
- c.negative = negative ^ cmp.negative;
- c.absMul (* this, cmp);
- return c;
- }
- void output ()
- {
- if (len == 0)
- {
- puts ("0");
- return;
- }
- if (negative)
- printf ("-");
- printf ("% lld", a [len-1]);
- for (int i = len-2; i> = 0; i--)
- printf (show, a [i]);
- puts ("");
- }
- };
- Bignum f[801];
- void fib()
- {
- f[1] = 0;
- f [2] = 1;
- for (int i = 3; i <= 800; i ++)
- f [i] = (f [i-1] + f [i-2]) * (i-1);
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- fib();
- int b;
- while(cin>>b && b!=-1)
- {
- cout<<f[b]<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement