Preimage Size of Factorial Zeroes Function - Problem
Preimage Size of Factorial Zeroes Function

Imagine you're working as a mathematician analyzing factorial patterns. Your task is to understand the relationship between factorials and their trailing zeroes.

Given a function f(x) that counts the number of trailing zeroes in x! (x factorial), you need to find how many non-negative integers x produce exactly k trailing zeroes.

Key Insights:
f(3) = 0 because 3! = 6 (no trailing zeroes)
f(11) = 2 because 11! = 39916800 (two trailing zeroes)
• Trailing zeroes come from factors of 10, which means pairs of 2 and 5
• Since there are always more factors of 2 than 5, we only need to count factors of 5

The Challenge: For a given k, determine how many values of x satisfy f(x) = k. This is asking for the preimage size of the function f.

Input & Output

example_1.py — Basic case
$ Input: k = 3
Output: 5
💡 Note: f(x) = 3 for x = 15, 16, 17, 18, 19. These are the only values where x! has exactly 3 trailing zeroes, so the answer is 5.
example_2.py — Edge case
$ Input: k = 0
Output: 5
💡 Note: f(x) = 0 for x = 0, 1, 2, 3, 4. For x ≥ 5, x! will have at least one factor of 5, creating at least one trailing zero.
example_3.py — Impossible case
$ Input: k = 1000000000
Output: 5
💡 Note: Even for very large k, if it's achievable by some factorial, there will be exactly 5 consecutive values of x that produce k trailing zeroes.

Constraints

  • 0 ≤ k ≤ 109
  • The answer will always be either 0 or 5
  • x must be a non-negative integer

Visualization

Tap to expand
Why Preimage Size is Always 0 or 5f(x) = trailing zeros in x!x:0-45-910-1415-1920-2401234f=0f=1f=2f=3f=4← Each level spans exactly 5 x valuesKey Mathematical Insights:• f(x) = ⌊x/5⌋ + ⌊x/25⌋ + ⌊x/125⌋ + ...• Function increases in "steps" of size 5• Some values of k are impossible (create gaps)• When k is possible, exactly 5 consecutive x values achieve itExample: No x satisfies f(x) = 5, but f(x) = 6 has 5 solutionsBinary Search Algorithm1. Find first x where f(x) ≥ k2. Check if f(first_x) = k3. If yes, find last x where f(x) ≤ k4. Return last - first + 1 (= 5)Time Complexity: O(log²k) vs O(k) for brute forceSpace Complexity: O(1) - only using a few variablesThe mathematical insight that answer ∈ {0,5} makes this problem elegant!
Understanding the Visualization
1
Trailing zeros come from factors of 10
Each trailing zero needs one factor of 2 and one factor of 5
2
Factors of 2 are abundant
In any factorial, there are always more factors of 2 than 5
3
Count factors of 5
f(x) = floor(x/5) + floor(x/25) + floor(x/125) + ...
4
Function is non-decreasing
As x increases, f(x) never decreases
5
Gaps create impossible values
Some k values are impossible (like k=5), others have exactly 5 solutions
Key Takeaway
🎯 Key Insight: The preimage size is always 0 or 5 because f(x) increases in predictable steps, and binary search can efficiently find the boundaries of these steps.
Asked in
Google 15 Meta 8 Amazon 6 Microsoft 4
22.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen