-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.cpp
46 lines (37 loc) · 772 Bytes
/
20.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
//Project Euler Problem 20 - Factorial digit Sum
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MAX_SIZE 2000000
int multiply(int res[], int x, int res_size){
int carry = 0;
int i = 0;
for( ; i<=res_size-1; i++){
ll w = res[i] * x + carry;
res[i] = w % 10;
carry = w / 10;
}
while(carry){
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
void sumFact(int n){
int res[MAX_SIZE];
res[0] = 1;
int res_size = 1;
for(int i=2; i<=n; i++){
res_size = multiply(res, i, res_size);
}
ll sum = 0;
for(int i = res_size-1; i>=0; i--) sum += res[i];
cout<<sum<<endl;;
}
int main(){
int n;
cin>>n;
sumFact(n);
return 0;
}