#P1591. Counting Digit Occurrence in Factorial

    ID: 14877 Type: Default 1000ms 256MiB

Counting Digit Occurrence in Factorial

Counting Digit Occurrence in Factorial

Given two inputs, an integer n and a digit d, the task is to compute n! (n factorial) and count the number of times the digit d appears in the decimal representation of n!.

Recall that the factorial of a non-negative integer n is given by:

\( n! = \prod_{i=1}^{n} i \)

For the purpose of this problem, you can assume that n is small enough (e.g. \( n \le 20 \)) so that its factorial can be computed exactly using standard 64-bit or arbitrary precision arithmetic.

inputFormat

The input consists of a single line containing two values separated by space:

  • An integer n (\( 0 \le n \le 20 \));
  • A single digit d (0-9) whose occurrences in \( n! \) you need to count.

outputFormat

Output a single integer representing the number of times the digit d appears in the decimal representation of \( n! \).

sample

5 0
1