forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfctrl2.cpp
78 lines (78 loc) · 1.14 KB
/
fctrl2.cpp
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
// 2008-01-29
#include <iostream>
#define MAX(x,y) ((x)>(y)?(x):(y))
using namespace std;
struct number
{
int digits[160];
int length;
number()
{
length=1;
memset(digits,0,sizeof(digits));
}
number(const number& x)
{
length=x.length;
memcpy(digits,x.digits,sizeof(digits));
}
void shift()
{
int carry=0;
int d;
for (d=0; d<=length; d++)
{
digits[d]=(digits[d]<<1)+carry;
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0;
}
if (digits[length]) length++;
}
void add(number x)
{
int l=MAX(length,x.length);
int d;
int carry=0;
for (d=0; d<=l; d++)
{
digits[d]=digits[d]+x.digits[d]+carry;
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0;
}
length=l;
if (digits[length]) length++;
}
void mult(int x)
{
number y;
number z(*this);
while (x>0)
{
if (x%2)
y.add(z);
z.shift();
x/=2;
}
*this=y;
}
void print()
{
int i;
for (i=length-1; i>=0; i--)
putchar(digits[i]+48);
}
};
int main()
{
int t,i,n,j;
cin >> t;
for (i=0; i<t; i++)
{
cin >> n;
number f;
f.digits[0]=1;
for (j=n; j>1; j--)
f.mult(j);
f.print();
cout << endl;
}
return 0;
}