File size: 9,557 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10007
  Name: Count the Trees
  Problem: https://onlinejudge.org/external/100/10007.pdf
  Language: C++

  Author: Arash Shakery
  Email: [email protected]
*/

#include <bits/stdc++.h>
using namespace std;

class BigInt {
    typedef int CellType;
    const static int CellSize = sizeof(CellType);
    const static int MAX_C = 10000;
    const static int MAX_D = 4;
    CellType *cells;
    int capacity, size, sign;

    int sgn(int x) {
        return x<0 ? -1 : x>0 ? 1 : 0;
    }

    int countDigits(int x) {
        if (!x) return 0;
        if (x < 0) x = -x;
        return floor(log10(x)) + 1;
    }

    void init() {
        sign = size = 0;
        cells = new CellType[capacity = 4];
        memset(cells, 0, 4*CellSize);
    }

    int setSize(int sz, bool cp = true) {
        if (sz > capacity) {
            CellType* ncells = new CellType[capacity = sz + 16];
            if (cp && size)
                memcpy(ncells, cells, size*CellSize);

            if (cells) delete [] cells;
            cells = ncells;
        }
        if (!cp)
             memset(cells, 0, capacity*CellSize);
        else memset(cells+sz, 0, (capacity-sz)*CellSize);
        return size = sz;
    }

    BigInt& autoSize() {
        while (size>0 && !cells[size-1])
            size--;
        if (!size) sign=0;
        return *this;
    }

    // ---------------------------------------------------------------------

    public:
    BigInt() { init(); }
    BigInt(int x) {
        init();
        setTo(x);
    }

    BigInt(const char s[]) {
        while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++;
        sign = 1;
        if (s[0] == '+') s++;
        if (s[0] == '-') { sign=-1; s++; }
        while (s[0]=='0') s++;
        if (!s[0]) { init(); return; }

        int len = strlen(s);
        size = (len+3)/MAX_D;
        cells = new CellType[capacity = size+16];
        for (int i=len-1; i>=0; i-=MAX_D) {
            int rc = 0;
            for (int k=i-MAX_D+1; k<=i; k++) {
                if (k<0 || s[k]=='-' || s[k]=='+') continue;
                rc = rc*10 + s[k] - '0';
            }
            cells[(len-i-1) / MAX_D] = rc;
        }
        for (int i=size; i<capacity; i++) cells[i] = 0;
        autoSize();
    }

    BigInt(const BigInt& o): cells(0), capacity(0) {
        sign = o.sign;
        setSize(o.size, 0);
        for (int i=0; i<size; i++)
            cells[i] = o.cells[i];
    }

    ~BigInt() {
        if (capacity >= 0)
            delete [] cells;
    }

    int getSize() { return size; }
    int getCell(int i) { return cells[i]; }
    bool isZero() { return !sign; }
    BigInt& setOne() { sign=size=cells[0]=1; return *this; }
    BigInt& setTo(int x) {
        sign = sgn(x); x*=sign;
        for (size=0; x; size++) {
            cells[size] = x % MAX_C;
            x /= MAX_C;
        }
        return *this;
    }
    // ---------------------------------------------------------------------
    private:
    bool numLess(const BigInt &o) const {
        if (size != o.size)
            return size < o.size;
        for (int i=size-1; i>=0; i--)
            if (cells[i] != o.cells[i])
                return cells[i] < o.cells[i];
        return false;
    }
    bool numEq(const BigInt &o) const {
        if (size!=o.size)
            return false;
        for (int i=size-1; i>=0; i--)
            if (cells[i] != o.cells[i])
                return false;
        return true;
    }

    public:
    bool operator <  (const BigInt &o) const {
        return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign;
    }
    bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); }
    bool operator != (const BigInt &o) const { return !(*this == o); }
    bool operator <= (const BigInt &o) const { return !(o  < *this); }
    bool operator >  (const BigInt &o) const { return  (o  < *this); }
    bool operator >= (const BigInt &o) const { return !(*this <  o); }
    // ---------------------------------------------------------------------
    BigInt& operator=(const BigInt& o) {
        if ((sign = o.sign)) {
            setSize(o.size, 0);
            memcpy(cells, o.cells, o.size*CellSize);
        }
        else size=0;
        return *this;
    }

    BigInt& operator+= (BigInt o) {
        if (!o.sign) return *this;
        if (!sign) sign = o.sign;
        if (sign != o.sign) {
            o.sign *= -1;
            operator-=(o);
            o.sign *= -1;
            return *this;
        }

        int osz = size;
        setSize(max(size, o.size)+1);
        for (int i=osz; i<o.size+1; i++)
            cells[i] = 0;

        for (int i=0; i<size; i++) {
            if (i < o.size)
                cells[i] += o.cells[i];
            if (cells[i] >= MAX_C) {
                cells[i] -= MAX_C;
                ++cells[i+1];
            }
        }
        return autoSize();
    }
    BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize
    BigInt operator+ (int x) const { return *new BigInt(*this) += x; }
    BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; }

    // ---------------------------------------------------------------------

    BigInt& operator-= (BigInt& o) {
        if (!o.sign) return *this;
        if (!sign) sign = -o.sign;
        if (sign != o.sign) {
            o.sign *= -1;
            operator+=(o);
            o.sign *= -1;
            return *this;
        }

        if (*this == o) {
            sign = size = 0;
            return *this;
        }
        if (!o.numLess(*this))
            return *this = -(o - *this);

        for (int i=0; i<size; i++) {
            if (i < o.size)
                cells[i] -= o.cells[i];
            if (cells[i] < 0) {
                cells[i] += MAX_C;
                --cells[i+1];
            }
        }
        return autoSize();
    }
    BigInt& operator-= (int x) { return *this -= *new BigInt(x); }
    BigInt operator- (int x) const { return *new BigInt(*this) -= x; }
    BigInt operator- (BigInt& o) const { return *new BigInt(*this) -= o; }
    BigInt operator-() { BigInt tmp(*this); tmp.sign *= -1; return tmp; }

    // ---------------------------------------------------------------------

    BigInt& operator*= (int x) {
        if (x > 10000) {
            return *this *= BigInt(x);
        }

        sign *= sgn(x);
        if (!sign) {
            size = 0;
            return *this;
        }
        if (x<0) x = -x;

        int cr = 0;
        for (int i=0; i < size; i++) {
            cells[i] = cells[i] * x + cr;
            cr = cells[i] / MAX_C;
            cells[i] %= MAX_C;
        }

        if (cr) {
            int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size;
            setSize(size + ex);
            size = sz;
            for (; cr; cr /= MAX_C)
                cells[size++] = cr % MAX_C;
        }

        return autoSize();
    }
    BigInt operator* (const BigInt &o) const {
        if (sign*o.sign == 0)
            return *new BigInt(0);
        if (o.size == 1)
            return *new BigInt(*this) *= o.cells[0];

        BigInt r(sign*o.sign);
        r.setSize(size + o.size + 1, 0);
        for (int i=0; i<size; i++)
            for (int j=0; j<o.size; j++) {
                r.cells[i+j] += cells[i]*o.cells[j];
                r.cells[i+j+1] += r.cells[i+j] / MAX_C;
                r.cells[i+j] %= MAX_C;
            }
        return r.autoSize();
    }
    BigInt& operator*= (const BigInt &o) {
        if (!(sign *= o.sign)) {
            size = 0;
            return *this;
        }
        if (o.size == 1)
            return *this *= o.cells[0];
        return *this = *this * o;
    }
    BigInt operator* (int x) const { return *new BigInt(*this) *= x; }

    // ---------------------------------------------------------------------

    BigInt& operator/= (long long den) {
        if (!den) den/=den;
        long long t = 0;
        for (int i=size-1; i>=0; --i) {
            t = t*MAX_C + cells[i];
            cells[i] = t / den;
            t -= (long long) cells[i] * den;
        }
        return autoSize();
    }
    BigInt operator/ (int den) const { return *new BigInt(*this) /= den; }

    int operator% (int den) const {
        int r = 0;
        for (int i = size-1; i>=0; --i)
            r = (r * MAX_C + cells[i]) % den;
        return r;
    }

    // ---------------------------------------------------------------------
    friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) {
        if (!bs.sign)
            stream << 0;
        else {
            if (bs.sign < 0) stream << '-';
            stream << bs.cells[bs.size-1];
            for (int i=bs.size-2; i>=0; i--)
                stream << setfill('0') << setw(MAX_D) << bs.cells[i];
        }
        return stream;
    }
};
BigInt operator*(int m, BigInt &n) { return n * m; }
BigInt operator+(int m, BigInt &n) { return n + m; }
BigInt operator-(int m, BigInt &n) { return -n + m; }

// ----------------------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);

    BigInt F[313];
    F[0].setOne();
    F[1].setOne();
    for (int i=2; i<301; ++i)
        F[i] = F[i-1] * i;

    BigInt C[313];
    C[0].setOne();
    for (int i=1; i<313; ++i) {
        C[i]  = C[i-1];
        C[i] *= 4*i-2;
        C[i] /= i+1;
    }

    int n;
    while (cin >> n && n)
        cout << C[n] * F[n] << endl;
}