Editorial

GCD Subset Challenge

1. Problem Understanding

We are given an array of nn positive integers and asked to count the number of non-empty subsets such that the greatest common divisor (GCD) of all elements in the subset is exactly 11. In other words, we need to identify those subsets in which the numbers, when taken together, are "co-prime" in the sense that no divisor greater than 11 can evenly divide all of them. Since checking every subset by brute force is infeasible for nn as large as 10510^5, an efficient combinatorial and number-theoretic approach, often employing inclusion-exclusion or its related concept, Möbius inversion, must be used.

2. Solution Analysis

The idea is to leverage the properties of divisibility and the principle of inclusion-exclusion. Here is a step-by-step breakdown:

  • Step 1: Counting Subsets with Divisibility Constraints
    For every integer dd, consider counting the number of non-empty subsets where every number in the subset is divisible by dd. Let f(d)f(d) be the number of such subsets.
    If we denote by freq[d]\text{freq}[d] the number of elements in the array that are multiples of dd, then the number of possible non-empty subsets is:
    f(d)=2freq[d]1f(d) = 2^{\text{freq}[d]} - 1
    This formula holds because each element that is a multiple of dd can either be chosen or not, and we subtract 11 to exclude the empty subset.

  • Step 2: Using Möbius Inversion
    Notice that a subset with gcd=1\gcd = 1 will be counted in f(1)f(1) but might also be over-counted in f(d)f(d) for other divisors d>1d > 1.
    The Möbius function μ(d)\mu(d) helps correct for this over-counting. By Möbius inversion, the number of valid subsets (with gcd=1\gcd = 1) is given by:
    Answer=d=1maxμ(d)f(d)\text{Answer} = \sum_{d=1}^{\text{max}} \mu(d) \cdot f(d)
    where the summation is taken over all possible divisors dd up to some maximum value (which can be assumed as the maximum value in the input array), and μ(d)\mu(d) is defined as:

    • μ(d)=1\mu(d) = 1 if dd is a square-free positive integer with an even number of prime factors.
    • μ(d)=1\mu(d) = -1 if dd is a square-free positive integer with an odd number of prime factors.
    • μ(d)=0\mu(d) = 0 if dd has a squared prime factor.
  • Step 3: Preprocessing Step
    To efficiently compute f(d)f(d) for all relevant dd, count the numbers in the array which are divisible by dd. This can be done by:

    1. Computing the frequency of each element in the given array.
    2. Iterating over each divisor dd up to the maximum number and summing up frequencies for multiples of dd.
  • Step 4: Modular Arithmetic
    Since the answers can be very large, and we need to report the result modulo 109+710^9+7, all arithmetic operations (especially when computing powers of 22) must be performed modulo 109+710^9+7.

3. Time and Space Complexity Analysis

  • Time Complexity:

    • Counting frequencies requires O(n)O(n) time.
    • The divisor aggregation process iterates over all divisors for each number up to the maximum value (which is up to 10510^5) and hence takes approximately O(MlogM)O(M \log M) where MM is the maximum value among array elements.
    • Precomputing the Möbius function via a sieve-like method takes O(MloglogM)O(M \log \log M).
    • Calculating modular exponents for each divisor takes constant time on average (using fast exponentiation), leading to a total time close to O(M)O(M) operations.
      Overall, the approach works in O(MlogM)O(M \log M) time, which is acceptable for M=105M = 10^5.
  • Space Complexity:

    • The additional space used is for the frequency array and auxiliary arrays for the Möbius function and f(d)f(d), each of size O(M)O(M).
      Therefore, the space complexity is O(M)O(M).

Author's Solution

GNU C++23