题干:
Let a a a, b b b, and c c c be integers. We define function f ( a , b , c ) f(a, b, c) f(a,b,c) as follows:
Order the numbers a a a, b b b, c c c in such a way that a ≤ b ≤ c a \le b \le c a≤b≤c. Then return gcd ( a , b ) \gcd(a, b) gcd(a,b), where gcd ( a , b ) \gcd(a, b) gcd(a,b) denotes the greatest common divisor (GCD) of integers a a a and b b b.
So basically, we take the gcd \gcd gcd of the 2 2 2 smaller values and ignore the biggest one.
You are given an array a a a of n n n elements. Compute the sum of f ( a i , a j , a k ) f(a_i, a_j, a_k) f(ai,aj,ak) for each i i i, j j j, k k k, such that 1 ≤ i < j < k ≤ n 1 \le i < j < k \le n 1≤i<j<k≤n.
More formally, compute
∑ i = 1 n ∑ j = i + 1 n ∑ k = j + 1 n f ( a i , a j , a k ) . \sum_{i = 1}^n \sum_{j = i+1}^n \sum_{k =j +1}^n f(a_i, a_j, a_k). i=1∑nj=i+1∑nk=j+1∑nf(ai,aj,ak).
输入要求:
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 10 1 \le t \le 10 1≤t≤10). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 8 ⋅ 1 0 4 3 \le n \le 8 \cdot 10^4 3≤n≤8⋅104) — length of the array a a a.
The second line of each test case contains n n n integers, a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…

博客围绕数组中所有三元组较小两个数的gcd求和问题展开。先分析暴力解法复杂度高会超时,接着讲述错误思路及修正尝试。还介绍了从排行榜看到的代码思路,以及欧拉反演和容斥两种补充思路,最后指出两种补充思路本质相同。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



