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

Input: n = 3
Output: 100

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

Input: n = 6
Output: 8000

Three even places and three odd ones: 5 × 5 × 5 × 4 × 4 × 4 = 8000.

Example 3

Input: n = 31
Output: 120008029

Constraints

  • 1 ≤ n ≤ 2³¹ - 1, so n fits in a signed 32-bit integer

More Recursion

All Recursion problems