Skip to content

Commit 8c6c6ef

Browse files
authored
feat: add solutions to lc problem: No.1922 (#4347)
No.1922.Count Good Numbers
1 parent 1371b77 commit 8c6c6ef

File tree

5 files changed

+124
-56
lines changed

5 files changed

+124
-56
lines changed

Diff for: solution/1900-1999/1922.Count Good Numbers/README.md

+46-19
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:快速幂
71+
72+
长度为 $n$ 的好数字,偶数下标一共有 $\lceil \frac{n}{2} \rceil = \lfloor \frac{n + 1}{2} \rfloor$ 位,偶数下标可以填入 $5$ 种数字($0, 2, 4, 6, 8$);奇数下标一共有 $\lfloor \frac{n}{2} \rfloor$ 位,奇数下标可以填入 $4$ 种数字($2, 3, 5, 7$)。因此长度为 $n$ 的好数字的个数为:
73+
74+
$$
75+
ans = 5^{\lceil \frac{n}{2} \rceil} \times 4^{\lfloor \frac{n}{2} \rfloor}
76+
$$
77+
78+
我们可以使用快速幂来计算 $5^{\lceil \frac{n}{2} \rceil}$ 和 $4^{\lfloor \frac{n}{2} \rfloor}$,时间复杂度为 $O(\log n)$,空间复杂度为 $O(1)$。
7179

7280
<!-- tabs:start -->
7381

@@ -84,13 +92,13 @@ class Solution:
8492

8593
```java
8694
class Solution {
87-
private int mod = 1000000007;
95+
private final int mod = (int) 1e9 + 7;
8896

8997
public int countGoodNumbers(long n) {
90-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
98+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
9199
}
92100

93-
private long myPow(long x, long n) {
101+
private long qpow(long x, long n) {
94102
long res = 1;
95103
while (n != 0) {
96104
if ((n & 1) == 1) {
@@ -107,25 +115,22 @@ class Solution {
107115
#### C++
108116

109117
```cpp
110-
int MOD = 1000000007;
111-
112118
class Solution {
113119
public:
114120
int countGoodNumbers(long long n) {
115-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
116-
}
117-
118-
private:
119-
long long myPow(long long x, long long n) {
120-
long long res = 1;
121-
while (n) {
122-
if ((n & 1) == 1) {
123-
res = res * x % MOD;
121+
const int mod = 1e9 + 7;
122+
auto qpow = [](long long x, long long n) -> long long {
123+
long long res = 1;
124+
while (n) {
125+
if ((n & 1) == 1) {
126+
res = res * x % mod;
127+
}
128+
x = x * x % mod;
129+
n >>= 1;
124130
}
125-
x = x * x % MOD;
126-
n >>= 1;
127-
}
128-
return res;
131+
return res;
132+
};
133+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
129134
}
130135
};
131136
```
@@ -152,6 +157,28 @@ func myPow(x, n int64) int64 {
152157
}
153158
```
154159

160+
#### TypeScript
161+
162+
```ts
163+
function countGoodNumbers(n: number): number {
164+
const mod = 1000000007n;
165+
const qpow = (x: bigint, n: bigint): bigint => {
166+
let res = 1n;
167+
while (n > 0n) {
168+
if (n & 1n) {
169+
res = (res * x) % mod;
170+
}
171+
x = (x * x) % mod;
172+
n >>= 1n;
173+
}
174+
return res;
175+
};
176+
const a = qpow(5n, BigInt(n + 1) / 2n);
177+
const b = qpow(4n, BigInt(n) / 2n);
178+
return Number((a * b) % mod);
179+
}
180+
```
181+
155182
<!-- tabs:end -->
156183

157184
<!-- solution:end -->

Diff for: solution/1900-1999/1922.Count Good Numbers/README_EN.md

+46-19
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Fast Exponentiation
69+
70+
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:
71+
72+
$$
73+
ans = 5^{\lceil \frac{n}{2} \rceil} \times 4^{\lfloor \frac{n}{2} \rfloor}
74+
$$
75+
76+
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)$.
6977

7078
<!-- tabs:start -->
7179

@@ -82,13 +90,13 @@ class Solution:
8290

8391
```java
8492
class Solution {
85-
private int mod = 1000000007;
93+
private final int mod = (int) 1e9 + 7;
8694

8795
public int countGoodNumbers(long n) {
88-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
96+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
8997
}
9098

91-
private long myPow(long x, long n) {
99+
private long qpow(long x, long n) {
92100
long res = 1;
93101
while (n != 0) {
94102
if ((n & 1) == 1) {
@@ -105,25 +113,22 @@ class Solution {
105113
#### C++
106114

107115
```cpp
108-
int MOD = 1000000007;
109-
110116
class Solution {
111117
public:
112118
int countGoodNumbers(long long n) {
113-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
114-
}
115-
116-
private:
117-
long long myPow(long long x, long long n) {
118-
long long res = 1;
119-
while (n) {
120-
if ((n & 1) == 1) {
121-
res = res * x % MOD;
119+
const int mod = 1e9 + 7;
120+
auto qpow = [](long long x, long long n) -> long long {
121+
long long res = 1;
122+
while (n) {
123+
if ((n & 1) == 1) {
124+
res = res * x % mod;
125+
}
126+
x = x * x % mod;
127+
n >>= 1;
122128
}
123-
x = x * x % MOD;
124-
n >>= 1;
125-
}
126-
return res;
129+
return res;
130+
};
131+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
127132
}
128133
};
129134
```
@@ -150,6 +155,28 @@ func myPow(x, n int64) int64 {
150155
}
151156
```
152157

158+
#### TypeScript
159+
160+
```ts
161+
function countGoodNumbers(n: number): number {
162+
const mod = 1000000007n;
163+
const qpow = (x: bigint, n: bigint): bigint => {
164+
let res = 1n;
165+
while (n > 0n) {
166+
if (n & 1n) {
167+
res = (res * x) % mod;
168+
}
169+
x = (x * x) % mod;
170+
n >>= 1n;
171+
}
172+
return res;
173+
};
174+
const a = qpow(5n, BigInt(n + 1) / 2n);
175+
const b = qpow(4n, BigInt(n) / 2n);
176+
return Number((a * b) % mod);
177+
}
178+
```
179+
153180
<!-- tabs:end -->
154181

155182
<!-- solution:end -->
+12-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
int MOD = 1000000007;
2-
31
class Solution {
42
public:
53
int countGoodNumbers(long long n) {
6-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
7-
}
8-
9-
private:
10-
long long myPow(long long x, long long n) {
11-
long long res = 1;
12-
while (n) {
13-
if ((n & 1) == 1) {
14-
res = res * x % MOD;
4+
const int mod = 1e9 + 7;
5+
auto qpow = [](long long x, long long n) -> long long {
6+
long long res = 1;
7+
while (n) {
8+
if ((n & 1) == 1) {
9+
res = res * x % mod;
10+
}
11+
x = x * x % mod;
12+
n >>= 1;
1513
}
16-
x = x * x % MOD;
17-
n >>= 1;
18-
}
19-
return res;
14+
return res;
15+
};
16+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
2017
}
2118
};

Diff for: solution/1900-1999/1922.Count Good Numbers/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
2-
private int mod = 1000000007;
2+
private final int mod = (int) 1e9 + 7;
33

44
public int countGoodNumbers(long n) {
5-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
5+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
66
}
77

8-
private long myPow(long x, long n) {
8+
private long qpow(long x, long n) {
99
long res = 1;
1010
while (n != 0) {
1111
if ((n & 1) == 1) {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function countGoodNumbers(n: number): number {
2+
const mod = 1000000007n;
3+
const qpow = (x: bigint, n: bigint): bigint => {
4+
let res = 1n;
5+
while (n > 0n) {
6+
if (n & 1n) {
7+
res = (res * x) % mod;
8+
}
9+
x = (x * x) % mod;
10+
n >>= 1n;
11+
}
12+
return res;
13+
};
14+
const a = qpow(5n, BigInt(n + 1) / 2n);
15+
const b = qpow(4n, BigInt(n) / 2n);
16+
return Number((a * b) % mod);
17+
}

0 commit comments

Comments
 (0)