Count Good Numbers
Medium Recursion AI round
A string of digits is good when every digit at an even place is even (0, 2, 4, 6 or 8) and every digit at an odd place is a prime (2, 3, 5 or 7). Places count from 0, so the first digit is at an even place, and it may be 0.
Given n, count the good strings of exactly n digits.
That count grows very fast, so return it modulo 10⁹ + 7: the remainder after dividing it by 1,000,000,007.
Example 1
Places 0 and 2 take an even digit, 5 choices each, and place 1 a prime, 4 choices: 5 × 4 × 5 = 100 good strings, such as 020, 476 and 838.
Example 2
Three even places and three odd ones: 5 × 5 × 5 × 4 × 4 × 4 = 8000.
Example 3
Constraints
1 ≤ n ≤ 2³¹ - 1, sonfits in a signed 32-bit integer
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
More Recursion
- Permutation SequenceAI round · Hard
- Find Kth Bit in Nth Binary StringAI round · Medium
- Pow(x, n)Medium
- Power of ThreeEasy
- Elimination GameMedium