-
Notifications
You must be signed in to change notification settings - Fork 0
/
small_factorials.cpp
106 lines (90 loc) · 1.65 KB
/
small_factorials.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
#include<bits/stdc++.h>
using namespace std;
void arrayfy(int number, vector<int> &result_array)
{
int i,j,k;
k=number;
while(k)
{
result_array.push_back(k%10);
k=k/10;
}
}
void multiply_arrays(vector<int> &temp_ans, vector<int> next_num)
{
vector<int> result;
int i,j,k;
int ta_size=temp_ans.size();
vector<vector<int> > temp_row_bunch;
for(i=0;i<next_num.size();i++)
{
vector<int> temp_row;
if(i==1)
temp_row.push_back(0);
if(i==2)
{
temp_row.push_back(0);
temp_row.push_back(0);
}
int carry=0;
for(j=0;j<ta_size;j++)
{
temp_row.push_back((next_num[i]*temp_ans[j]+carry)%10);
carry=(next_num[i]*temp_ans[j]+carry)/10;
}
while(carry>0)
{
temp_row.push_back(carry%10);
carry=carry/10;
}
temp_row_bunch.push_back(temp_row);
temp_row.clear();
}
int max_len=0;
for(i=0;i<next_num.size();i++)
{
if(temp_row_bunch[i].size()>max_len)
max_len=temp_row_bunch[i].size();
}
int carry_sum=0;
for(j=0;j<max_len;j++)
{
int temp_sum=0;
for(i=0;i<next_num.size();i++)
{
if(temp_row_bunch[i].size()>j)
temp_sum+=temp_row_bunch[i][j];
}
result.push_back((temp_sum+carry_sum)%10);
carry_sum=(temp_sum+carry_sum)/10;
}
while(carry_sum>0)
{
result.push_back(carry_sum%10);
carry_sum=carry_sum/10;
}
temp_ans=result;
}
int main()
{
int t,i,j,k,num;
cin>>t;
while(t--)
{
vector<int> next_num;
cin>>num;
vector<int> temp_ans;
temp_ans.push_back(1);
for(i=2;i<=num;i++)
{
arrayfy(i,next_num);
multiply_arrays(temp_ans,next_num);
next_num.clear();
}
int res_len=temp_ans.size();
for(i=res_len-1;i>=0;i--)
cout<<temp_ans[i];
cout<<endl;
}
return 0;
}