D._Small_GCD题解(codeforces 911 div.2)

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

题干:

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 abc. 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 1i<j<kn.

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=1nj=i+1nk=j+1nf(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 1t10). 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 3n8104) — 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,

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值