Count Good Numbers - Problem
Count Good Numbers is a fascinating combinatorics problem that challenges you to count valid digit strings with specific constraints.

A digit string is considered "good" if it follows these rules:
• Digits at even positions (0, 2, 4, ...) must be even (0, 2, 4, 6, 8)
• Digits at odd positions (1, 3, 5, ...) must be prime (2, 3, 5, 7)

For example:
"2582" is good: positions 0,2 have even digits (2,8), positions 1,3 have prime digits (5,2)
"3245" is not good: position 0 has odd digit (3)

Given an integer n, return the total count of good digit strings of length n. Since the answer can be very large, return it modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: n = 1
Output: 5
💡 Note: For length 1, only position 0 exists (even position). Valid digits are 0,2,4,6,8 - total of 5 good numbers.
example_2.py — Small Even Length
$ Input: n = 4
Output: 400
💡 Note: Positions 0,2 are even (5 choices each), positions 1,3 are odd (4 choices each). Total = 5² × 4² = 25 × 16 = 400.
example_3.py — Small Odd Length
$ Input: n = 50
Output: 564908303
💡 Note: 25 even positions and 25 odd positions. Result is (5²⁵ × 4²⁵) mod (10⁹ + 7) = 564908303.

Constraints

  • 1 ≤ n ≤ 1015
  • The answer must be returned modulo 109 + 7
  • Large input size requires efficient O(log n) solution

Visualization

Tap to expand
Good Numbers: Pattern RecognitionStep 1: Understanding Position RulesEven Positions (0, 2, 4, 6, ...): Must contain even digitsValid: 0, 2, 4, 6, 8 (5 choices)Odd Positions (1, 3, 5, 7, ...): Must contain prime digitsValid: 2, 3, 5, 7 (4 choices)Step 2: Example with n = 6Pos 0Even5 choicesPos 1Odd4 choicesPos 2Even5 choicesPos 3Odd4 choicesPos 4Even5 choicesPos 5Odd4 choicesEven positions: 3Odd positions: 3Step 3: Mathematical CalculationMultiplication Principle: Total = (Choices for even positions) × (Choices for odd positions)For n = 6: Total = 5³ × 4³ = 125 × 64 = 8000General formula: 5^⌈n/2⌉ × 4^⌊n/2⌋Time Complexity: O(log n) using fast exponentiationStep 4: Fast Exponentiation ExampleTo compute 5³ efficiently:3 in binary: 11 → result = 1, then result = result × 5 = 5, then result = result × 5² = 125This reduces O(n) multiplication to O(log n) operations
Understanding the Visualization
1
Analyze Position Rules
Even positions (0,2,4...) need even digits (0,2,4,6,8), odd positions (1,3,5...) need prime digits (2,3,5,7)
2
Count Position Types
For length n: even positions = ceil(n/2), odd positions = floor(n/2)
3
Apply Multiplication Principle
Total combinations = 5^(even_positions) × 4^(odd_positions)
4
Use Fast Exponentiation
Compute large powers efficiently using binary exponentiation in O(log n) time
Key Takeaway
🎯 Key Insight: Independence of positions allows us to use the multiplication principle. Fast exponentiation makes it efficient for very large n values up to 10^15.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 25
39.5K Views
Medium Frequency
~18 min Avg. Time
1.4K 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