Factorial Frequencies
Difficulty: Hard

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just 1x2x3x...xn. As she expects to use the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.

Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!

**Sample Input:**

	3   
	8   
	100  
	0  

**Expected Output:**  
(Hint: Make sure you match all the spaces exactly!)


	3! --

	   (0)    0    (1)    0    (2)    0    (3)    0    (4)    0

	   (5)    0    (6)    1    (7)    0    (8)    0    (9)    0

	8! --

	   (0)    2    (1)    0    (2)    1    (3)    1    (4)    1

	   (5)    0    (6)    0    (7)    0    (8)    0    (9)    0

	100! --

	   (0)   30    (1)   15    (2)   19    (3)   10    (4)   10

	   (5)   14    (6)   19    (7)    7    (8)   14    (9)   20

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	#include <stdio.h>

	int main() {
	   int digits[1000];
	   int digit_counts[10] = {0};
	   int n_digits, digit_no;
	   int i, j;
	   int n, overflow, prod;

   
	   scanf("%d", &n);

	   while (n) {
      digits[0] = 1;
      n_digits = 1;
      
      for (i=2; i<=n; i++) {
	 overflow = 0;
	 for (j=0; j<n_digits || overflow; j++) {
	    if (j<n_digits) {
	       digits[j] = digits[j] * i + overflow;
	    } else {
	       digits[j] = overflow;
	    }
	    overflow = digits[j] / 10;
	    digits[j] = digits[j] % 10;
	 }
	 n_digits = j;
	 
      }


      printf("%d! --\n", n);
      for (i=0; i<n_digits; i++) {
	 digit_counts[digits[i]]++;
      }
      for (i=0; i<10; i++) {
	 printf("   (%d)  %3d ", i, digit_counts[i]);
	 digit_counts[i] = 0;
	 if (i==4 || i==9) putchar('\n');
      }
      scanf("%d", &n);

	   }

	   return 0;
	}