Skip to content

Add solution and test-cases for problem 1922 #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions leetcode/1901-2000/1922.Count-Good-Numbers/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [1922.Count Good Numbers][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## 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 (`2` and `8`) at even positions are even and the digits (`5` and `2`) at odd positions are prime. However, `"3245"` is **not** good because `3` is 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** `10^9 + 7`.

A **digit string** is a string consisting of digits `0` through `9` that may contain leading zeros.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 1
Output: 5
Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Count Good Numbers
```go
```
Input: n = 4
Output: 400
```

**Example 3:**

```
Input: n = 50
Output: 564908303
```

## 结语

Expand Down
27 changes: 25 additions & 2 deletions leetcode/1901-2000/1922.Count-Good-Numbers/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package Solution

func Solution(x bool) bool {
return x
const mod1922 = 1000000007

func quickPow(a int, n int64) int {
if n == 0 {
return 1
}
if n == 1 {
return a
}

ans := quickPow(a, n>>1)
ans = (ans * ans) % mod1922
if n&1 == 1 {
ans = (ans * a) % mod1922
}
return ans
}

func Solution(n int64) int {
add := int64(0)
if n&1 == 1 {
add = 1
}
mid := n >> 1
return (quickPow(5, mid+add) * quickPow(4, mid)) % mod1922
}
14 changes: 7 additions & 7 deletions leetcode/1901-2000/1922.Count-Good-Numbers/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int64
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 5},
{"TestCase2", 4, 400},
{"TestCase3", 50, 564908303},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading