-
Notifications
You must be signed in to change notification settings - Fork 0
/
FACTORIAL.CPP
176 lines (128 loc) · 3.02 KB
/
FACTORIAL.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
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
#include<bits/stdc++.h>
#define fr(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define fr1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define frr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define frab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))
#define vll vector<ll>
#define vvll vector< vll >
#define pll pair<ll ,ll >
#define vpll vector< pll >
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define fst first
#define scd second
#define MOD 1000000007
#define ll long long
#define mod 1000000007
#define MAX 100000
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ld long double
using namespace std;
void read( ll &a)
{
scanf("%lld",&a);
}
void read( ll &a, ll &b)
{
scanf("%lld %lld",&a,&b);
}
void read( ll &a, ll &b,ll &c)
{
scanf("%lld %lld %lld",&a,&b,&c);
}
void read(ll &a,ll &b,ll &c,ll &d)
{
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
}
void write(vll &oneD)
{
for(ll i=0;i<oneD.size();i++)
{
printf("%lld ",oneD[i]);
}
printf("\n");
}
void write(vvll &twoD)
{
for(ll i=0;i<twoD.size();i++)
{
write(twoD[i]);
}
}
void write(vpll &oneDP){
fr(i,oneDP.size())
{
printf("%lld %lld\n" , oneDP[i].fst , oneDP[i].scd);
}
}
void write(map< ll , ll > &mpp){
for(map<ll , ll >::iterator it = mpp.begin() ; it != mpp.end() ; it++)
{
cout<<it->fst<<" : "<<it->scd<<endl;
}
cout<<endl;
}
bool sortbysecdesc ( const pll &a , const pll &b)
{
return (a.scd > b.scd );
}
bool sortbysecasc( const pll &a, const pll &b )
{
return (a.scd < b.scd);
}
ll multiply(ll x, ll res[], ll res_size);
void factorial(ll n)
{
ll res[MAX];
// Initialize result
res[0] = 1;
ll res_size = 1;
// Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
for (ll x=2; x<=n; x++)
res_size = multiply(x, res, res_size);
//return res;
cout << "Factorial of given number is \n";
for (ll i=res_size-1; i>=0; i--)
cout << res[i]%mod;
}
// This function multiplies x with the number
// represented by res[].
// res_size is size of res[] or number of digits in the
// number represented by res[]. This function uses simple
// school mathematics for multiplication.
// This function may value of res_size and returns the
// new value of res_size
long long multiply(ll x, ll res[],ll res_size)
{
ll carry = 0; // Initialize carry
// One by one multiply n with individual digits of res[]
for (ll i=0; i<res_size; i++)
{
ll prod = res[i] * x + carry;
// Store last digit of 'prod' in res[]
res[i] = prod % 10;
// Put rest in carry
carry = prod/10;
}
// Put carry in res and increase result size
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
int main()
{
ll i,j,m,n,t,k,p,u,v;
read(t);
while(t--)
{
read(n);
factorial(n);
cout<<endl;
}
}