Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add solution and test-cases for problem 2438 #1284

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
6boris merged 1 commit into 6boris:main from 0xff-dev:2438
Aug 22, 2025
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
34 changes: 20 additions & 14 deletions leetcode/2401-2500/2438.Range-Product-Queries-of-Powers/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# [2438.Range Product Queries of Powers][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
Given a positive integer `n`, there exists a **0-indexed** array called `powers`, composed of the **minimum** number of powers of `2` that sum to `n`. The array is sorted in **non-decreasing** order, and there is **only one** way to form the array.

You are also given a **0-indexed** 2D integer array `queries`, where `queries[i] = [lefti, righti]`. Each `queries[i]` represents a query where you have to find the product of all `powers[j]` with `lefti <= j <= righti`.

Return an array `answer`, equal in length to `queries`, where `answers[i]` is the answer to the `ith` query. Since the answer to the `ith` query may be too large, each `answers[i]` should be returned **modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 15, queries = [[0,1],[2,2],[0,3]]
Output: [2,4,64]
Explanation:
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
Answer to 2nd query: powers[2] = 4.
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
Each answer modulo 109 + 7 yields the same answer, so [2,4,64] is returned.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Range Product Queries of Powers
```go
```

Input: n = 2, queries = [[0,0]]
Output: [2]
Explanation:
For n = 2, powers = [2].
The answer to the only query is powers[0] = 2. The answer modulo 109 + 7 is the same, so [2] is returned.
```

## 结语

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func construct(n int, powers [33]int) []int {
if n&(n-1) == 0 {
return []int{n}
}
index := sort.Search(33, func(i int) bool {
return powers[i] > n
})
set := []int{powers[index-1]}
left := construct(n-powers[index-1], powers)
set = append(set, left...)
return set
}

func modPow(x int64, n int64, m int64) int64 {
result := int64(1)
x %= m
for n > 0 {
if n&1 == 1 {
result = (result * x) % m
}
x = (x * x) % m
n >>= 1
}
return result
}

func modInverse(x int64, m int64) int64 {
return modPow(x, m-2, m)
}

const mod = 1000000007

func Solution(n int, queries [][]int) []int {

powers := [33]int{}
powers[0] = 1
for i := 1; i < 33; i++ {
powers[i] = powers[i-1] * 2
}
arr := construct(n, powers)
for s, e := 0, len(arr)-1; s < e; s, e = s+1, e-1 {
arr[s], arr[e] = arr[e], arr[s]
}
ans := make([]int, len(queries))
prefix := make([]int64, len(arr)+1)
index := 0
prefix[index] = 1
for i := 0; i < len(arr); i++ {
prefix[i+1] = (prefix[index] * int64(arr[i])) % mod
index++
}
for i, q := range queries {
l, r := q[0], q[1]
res := (prefix[r+1] * modInverse(prefix[l], mod)) % mod
ans[i] = int(res)
}
return ans
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n int
queries [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 15, [][]int{{0, 1}, {2, 2}, {0, 3}}, []int{2, 4, 64}},
{"TestCase2", 2, [][]int{{0, 0}}, []int{2}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.queries)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.queries)
}
})
}
}

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

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

AltStyle によって変換されたページ (->オリジナル) /