-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path324-Factorial Frequencies.cpp
44 lines (40 loc) · 1.01 KB
/
324-Factorial Frequencies.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
#include <bits/stdc++.h>
using namespace std;
int vals[367][800];
int digitCount[367][10];
void precompute(){
memset(vals, 0, sizeof vals);
memset(digitCount, 0, sizeof digitCount);
vals[0][0] = 1, digitCount[0][1] = 1;
for(int i=1;i<=366;i++) {
for(int j=0;j<=783;j++) {
vals[i][j] += vals[i-1][j]*i;
vals[i][j+1] += vals[i][j]/10;
vals[i][j] %= 10;
}
bool started = false;
for(int j=783;j>=0;j--) {
if(vals[i][j]) started = true;
if(started)
digitCount[i][vals[i][j]]++;
}
}
}
int main()
{
precompute();
int n;
while(scanf("%d",&n), n){
printf("%d! --\n ",n);
for(int i=0;i<5;i++) {
if(i) printf(" ");
printf("(%d)%5d", i, digitCount[n][i]);
}
printf("\n ");
for(int i=5;i<10;i++) {
if(i>5) printf(" ");
printf("(%d)%5d", i, digitCount[n][i]);
}
printf("\n");
}
}