Sum of Beautiful Subsequences Problem
Description
LeetCode Problem 3671.
You are given an integer array nums of length n.
For every positive integer g, we define the beauty of g as the product of g and the number of strictly increasing subsequences of nums whose greatest common divisor (GCD) is exactly g.
Return the sum of beauty values for all positive integers g.
Since the answer could be very large, return it modulo 10^9 + 7.
Example 1:
Input: nums = [1,2,3]
Output: 10
Explanation:
All strictly increasing subsequences and their GCDs are:
Subsequence GCD
[1] 1
[2] 2
[3] 3
[1,2] 1
[1,3] 1
[2,3] 1
[1,2,3] 1
Calculating beauty for each GCD:
GCD Count of subsequences Beauty (GCD × Count)
1 5 1 × 5 = 5
2 1 2 × 1 = 2
3 1 3 × 1 = 3
Total beauty is 5 + 2 + 3 = 10.
Example 2:
Input: nums = [4,6]
Output: 12
Explanation:
All strictly increasing subsequences and their GCDs are:
Subsequence GCD
[4] 4
[6] 6
[4,6] 2
Calculating beauty for each GCD:
GCD Count of subsequences Beauty (GCD × Count)
2 1 2 × 1 = 2
4 1 4 × 1 = 4
6 1 6 × 1 = 6
Total beauty is 2 + 4 + 6 = 12.
Constraints:
1 <= n == nums.length <= 10^41 <= nums[i] <= 7 * 10^4
Solution
Approach: Counting by Divisor and Inclusion-Exclusion
Let f(d) be the number of strictly increasing subsequences whose elements are all multiples of d; their gcd is a multiple of d. Let g(d) be the number whose gcd is exactly d. Then f(d) = g(d) + g(2d) + g(3d) + ..., so going from the largest d down, g(d) = f(d) - g(2d) - g(3d) - .... The answer is the sum of d * g(d).
To compute f(d), take the numbers divisible by d in their original order (collected once by listing the divisors of every element). Divide them by d and count increasing subsequences with a Fenwick tree indexed by value: the number of subsequences ending at v is 1 plus the sum over smaller values already seen. Add it to the tree at v and to the running total. Everything is taken modulo 10^9 + 7.
Complexity: time about O(S log M + M log M), where S is the total number of (element, divisor) pairs and M is the largest value; space O(M + S).
class Solution:
def totalBeauty(self, nums: List[int]) -> int:
mod = 10**9 + 7
mx = max(nums)
# lists[d]: the numbers divisible by d, in their original order
lists = [[] for _ in range(mx + 1)]
for x in nums:
i = 1
while i * i <= x:
if x % i == 0:
lists[i].append(x)
if i * i != x:
lists[x // i].append(x)
i += 1
# f[d]: number of strictly increasing subsequences whose elements are all multiples of d
f = [0] * (mx + 1)
for d in range(1, mx + 1):
if not lists[d]:
continue
size = mx // d + 1
tree = [0] * (size + 1)
total = 0
for x in lists[d]:
v = x // d
# ways ending at x: itself plus any smaller value before it
cnt = 1
j = v - 1
while j > 0:
cnt += tree[j]
j &= j - 1
cnt %= mod
total += cnt
j = v
while j <= size:
tree[j] = (tree[j] + cnt) % mod
j += j & -j
f[d] = total % mod
# g[d]: subsequences whose gcd is exactly d
ans = 0
for d in range(mx, 0, -1):
for k in range(2 * d, mx + 1, d):
f[d] -= f[k]
f[d] %= mod
ans = (ans + d * f[d]) % mod
return ans