|
…
|
||
|---|---|---|
| .. | ||
| README.md | ||
| README_EN.md | ||
| Solution.cpp | ||
| Solution.go | ||
| Solution.java | ||
| Solution.py | ||
| Solution.ts | ||
README_EN.md
| comments | difficulty | edit_url | rating | source | tags | ||
|---|---|---|---|---|---|---|---|
| true | Medium | https://github.com/doocs/leetcode/edit/main/solution/1900-1999/1922.Count%20Good%20Numbers/README_EN.md | 1674 | Weekly Contest 248 Q3 |
|
1922. Count Good Numbers
Description
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
- For example,
"2582"is good because the digits (2and8) at even positions are even and the digits (5and2) at odd positions are prime. However,"3245"is not good because3is at an even index but is not even.
Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
Example 1:
Input: n = 1 Output: 5 Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
Example 2:
Input: n = 4 Output: 400
Example 3:
Input: n = 50 Output: 564908303
Constraints:
1 <= n <= 1015
Solutions
Solution 1: Fast Exponentiation
For a "good number" of length n, the even-indexed positions have \lceil \frac{n}{2} \rceil = \lfloor \frac{n + 1}{2} \rfloor digits, and these positions can be filled with 5 different digits (0, 2, 4, 6, 8). The odd-indexed positions have \lfloor \frac{n}{2} \rfloor digits, and these positions can be filled with 4 different digits (2, 3, 5, 7). Therefore, the total number of "good numbers" of length n is:
ans = 5^{\lceil \frac{n}{2} \rceil} \times 4^{\lfloor \frac{n}{2} \rfloor}
We can use fast exponentiation to compute 5^{\lceil \frac{n}{2} \rceil} and 4^{\lfloor \frac{n}{2} \rfloor}. The time complexity is O(\log n), and the space complexity is O(1).
Python3
class Solution:
def countGoodNumbers(self, n: int) -> int:
mod = 10**9 + 7
return pow(5, (n + 1) >> 1, mod) * pow(4, n >> 1, mod) % mod
Java
class Solution {
private final int mod = (int) 1e9 + 7;
public int countGoodNumbers(long n) {
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
}
private long qpow(long x, long n) {
long res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
}
C++
class Solution {
public:
int countGoodNumbers(long long n) {
const int mod = 1e9 + 7;
auto qpow = [](long long x, long long n) -> long long {
long long res = 1;
while (n) {
if ((n & 1) == 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
};
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
}
};
Go
const mod int64 = 1e9 + 7
func countGoodNumbers(n int64) int {
return int(myPow(5, (n+1)>>1) * myPow(4, n>>1) % mod)
}
func myPow(x, n int64) int64 {
var res int64 = 1
for n != 0 {
if (n & 1) == 1 {
res = res * x % mod
}
x = x * x % mod
n >>= 1
}
return res
}
TypeScript
function countGoodNumbers(n: number): number {
const mod = 1000000007n;
const qpow = (x: bigint, n: bigint): bigint => {
let res = 1n;
while (n > 0n) {
if (n & 1n) {
res = (res * x) % mod;
}
x = (x * x) % mod;
n >>= 1n;
}
return res;
};
const a = qpow(5n, BigInt(n + 1) / 2n);
const b = qpow(4n, BigInt(n) / 2n);
return Number((a * b) % mod);
}