Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Hard

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

 

Example 1:

Input: nums = [1,2,3]

Output: 10

Explanation:

All strictly increasing subsequences and their GCDs are:

SubsequenceGCD
[1]1
[2]2
[3]3
[1,2]1
[1,3]1
[2,3]1
[1,2,3]1

Calculating beauty for each GCD:

GCDCount of subsequencesBeauty (GCD × Count)
151 × 5 = 5
212 × 1 = 2
313 × 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:

SubsequenceGCD
[4]4
[6]6
[4,6]2

Calculating beauty for each GCD:

GCDCount of subsequencesBeauty (GCD × Count)
212 × 1 = 2
414 × 1 = 4
616 × 1 = 6

Total beauty is 2 + 4 + 6 = 12.

 

Constraints:

  • 1 <= n == nums.length <= 104
  • 1 <= nums[i] <= 7 * 104
 
Code
Code
Testcase
Testcase
Test Result
Test Result