返回介绍

solution / 2400-2499 / 2438.Range Product Queries of Powers / README_EN

发布于 2024-06-17 01:03:06 字数 5064 浏览 0 评论 0 收藏 0

2438. Range Product Queries of Powers

中文文档

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 _answers_, 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 109 + 7.

 

Example 1:

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:

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.

 

Constraints:

  • 1 <= n <= 109
  • 1 <= queries.length <= 105
  • 0 <= starti <= endi < powers.length

Solutions

Solution 1: Bit Manipulation + Simulation

First, we use bit manipulation (lowbit) to get the powers array, and then simulate to get the answer for each query.

The time complexity is $O(n \times \log n)$, ignoring the space consumption of the answer, the space complexity is $O(\log n)$. Here, $n$ is the length of $queries$.

class Solution:
  def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
    powers = []
    while n:
      x = n & -n
      powers.append(x)
      n -= x
    mod = 10**9 + 7
    ans = []
    for l, r in queries:
      x = 1
      for y in powers[l : r + 1]:
        x = (x * y) % mod
      ans.append(x)
    return ans
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int[] productQueries(int n, int[][] queries) {
    int[] powers = new int[Integer.bitCount(n)];
    for (int i = 0; n > 0; ++i) {
      int x = n & -n;
      powers[i] = x;
      n -= x;
    }
    int[] ans = new int[queries.length];
    for (int i = 0; i < ans.length; ++i) {
      long x = 1;
      int l = queries[i][0], r = queries[i][1];
      for (int j = l; j <= r; ++j) {
        x = (x * powers[j]) % MOD;
      }
      ans[i] = (int) x;
    }
    return ans;
  }
}
class Solution {
public:
  const int mod = 1e9 + 7;

  vector<int> productQueries(int n, vector<vector<int>>& queries) {
    vector<int> powers;
    while (n) {
      int x = n & -n;
      powers.emplace_back(x);
      n -= x;
    }
    vector<int> ans;
    for (auto& q : queries) {
      int l = q[0], r = q[1];
      long long x = 1l;
      for (int j = l; j <= r; ++j) {
        x = (x * powers[j]) % mod;
      }
      ans.emplace_back(x);
    }
    return ans;
  }
};
func productQueries(n int, queries [][]int) []int {
  var mod int = 1e9 + 7
  powers := []int{}
  for n > 0 {
    x := n & -n
    powers = append(powers, x)
    n -= x
  }
  ans := make([]int, len(queries))
  for i, q := range queries {
    l, r := q[0], q[1]
    x := 1
    for _, y := range powers[l : r+1] {
      x = (x * y) % mod
    }
    ans[i] = x
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文