Skip to content

3312. Sorted GCD Pair Queries πŸ‘ΒΆ

  • Time: O(nβ‹…max⁑(nums))O(n \cdot \sqrt{\max(\texttt{nums})})
  • Space: O(max⁑(nums))O(\max(\texttt{nums}))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
 public:
  vector<int> gcdValues(vector<int>& nums, vector<long long>& queries) {
    const int maxNum = ranges::max(nums);
    vector<int> ans;
    // countDivisor[d] := the number of `nums` having `num % d == 0`
    vector<int> countDivisor(maxNum + 1);
    // countGcdPair[g] := the number of pairs having gcd == g
    vector<long> countGcdPair(maxNum + 1);
    // prefixCountGcdPair[g] := the number of pairs having gcd <= g
    vector<long> prefixCountGcdPair{0};

    for (const int num : nums)
      for (int i = 1; i * i <= num; ++i)
        if (num % i == 0) {
          ++countDivisor[i];
          if (i != num / i)
            ++countDivisor[num / i];
        }

    for (int gcd = maxNum; gcd >= 1; --gcd) {
      // There are C(countDivisor[gcd], 2) pairs that have a common divisor
      // that's a multiple of `gcd` (including the one that equals to `gcd`).
      // So, substract the multiples of `gcd` to have the number of pairs with a
      // gcd that's exactly `gcd`.
      countGcdPair[gcd] =
          countDivisor[gcd] * static_cast<long>(countDivisor[gcd] - 1) / 2;
      for (int largerGcd = 2 * gcd; largerGcd <= maxNum; largerGcd += gcd)
        countGcdPair[gcd] -= countGcdPair[largerGcd];
    }

    for (int gcd = 1; gcd <= maxNum; ++gcd)
      prefixCountGcdPair.push_back(prefixCountGcdPair.back() +
                                   countGcdPair[gcd]);

    for (const long query : queries)
      ans.push_back(getNthGcdPair(query, prefixCountGcdPair));

    return ans;
  }

 private:
  // Returns the `query`-th gcd pair.
  int getNthGcdPair(long query, const vector<long>& prefixCountGcdPair) {
    int l = 1;
    int r = prefixCountGcdPair.size() - 1;
    while (l < r) {
      const int m = (l + r) / 2;
      if (prefixCountGcdPair[m] < query + 1)
        l = m + 1;
      else
        r = m;
    }
    return l;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
  public int[] gcdValues(int[] nums, long[] queries) {
    int maxNum = Arrays.stream(nums).max().getAsInt();
    int[] ans = new int[queries.length];
    // countDivisor[d] := the number of `nums` having `num % d == 0`
    int[] countDivisor = new int[maxNum + 1];
    // countGcdPair[g] := the number of pairs having gcd == g
    long[] countGcdPair = new long[maxNum + 1];
    // prefixCountGcdPair[g] := the number of pairs having gcd <= g
    long[] prefixCountGcdPair = new long[maxNum + 1];

    for (final int num : nums)
      for (int i = 1; i * i <= num; ++i)
        if (num % i == 0) {
          ++countDivisor[i];
          if (i != num / i)
            ++countDivisor[num / i];
        }

    for (int gcd = maxNum; gcd >= 1; --gcd) {
      // There are C(countDivisor[gcd], 2) pairs that have a common divisor
      // that's a multiple of `gcd` (including the one that equals to `gcd`).
      // So, substract the multiples of `gcd` to have the number of pairs with a
      // gcd that's exactly `gcd`.
      countGcdPair[gcd] = (long) countDivisor[gcd] * (countDivisor[gcd] - 1) / 2;
      for (int largerGcd = 2 * gcd; largerGcd <= maxNum; largerGcd += gcd)
        countGcdPair[gcd] -= countGcdPair[largerGcd];
    }

    for (int gcd = 1; gcd <= maxNum; ++gcd)
      prefixCountGcdPair[gcd] = prefixCountGcdPair[gcd - 1] + countGcdPair[gcd];

    for (int i = 0; i < queries.length; ++i)
      ans[i] = getNthGcdPair(queries[i], prefixCountGcdPair);

    return ans;
  }

  // Returns the `query`-th gcd pair.
  private int getNthGcdPair(long query, long[] prefixCountGcdPair) {
    int l = 1;
    int r = prefixCountGcdPair.length - 1;
    while (l < r) {
      int m = (l + r) / 2;
      if (prefixCountGcdPair[m] < query + 1)
        l = m + 1;
      else
        r = m;
    }
    return l;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
  def gcdValues(self, nums: list[int], queries: list[int]) -> list[int]:
    maxNum = max(nums)
    # countDivisor[d] := the number of `nums` having `num % d == 0`
    countDivisor = [0] * (maxNum + 1)
    # countGcdPair[g] := the number of pairs having gcd == g
    countGcdPair = [0] * (maxNum + 1)

    for num in nums:
      for i in range(1, math.isqrt(num) + 1):
        if num % i == 0:
          countDivisor[i] += 1
          if i != num // i:
            countDivisor[num // i] += 1

    for gcd in range(maxNum, 0, -1):
      # There are C(countDivisor[gcd], 2) pairs that have a common divisor
      # that's a multiple of `gcd` (including the one that equals to `gcd`).
      # So, substract the multiples of `gcd` to have the number of pairs with a
      # gcd that's exactly `gcd`.
      countGcdPair[gcd] = countDivisor[gcd] * (countDivisor[gcd] - 1) // 2
      for largerGcd in range(2 * gcd, maxNum + 1, gcd):
        countGcdPair[gcd] -= countGcdPair[largerGcd]

    # prefixCountGcdPair[g] := the number of pairs having gcd <= g
    prefixCountGcdPair = list(itertools.accumulate(countGcdPair))
    return [bisect.bisect_left(prefixCountGcdPair, query + 1)
            for query in queries]
Was this page helpful?