CF1900D.Small GCD
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let a, b, and c be integers. We define function f(a,b,c) as follows:
Order the numbers a, b, c in such a way that a≤b≤c. Then return gcd(a,b), where gcd(a,b) denotes the greatest common divisor (GCD) of integers a and b.
So basically, we take the gcd of the 2 smaller values and ignore the biggest one.
You are given an array a of n elements. Compute the sum of f(ai,aj,ak) for each i, j, k, such that 1≤i<j<k≤n.
More formally, compute $$\sum_{i = 1}^n \sum_{j = i+1}^n \sum_{k =j +1}^n f(a_i, a_j, a_k).$$
设 a、b、c 为整数。我们定义函数 f(a,b,c) 如下:
将数字 a、b、c 按非降序排列,使得 a≤b≤c;然后返回 gcd(a,b),其中 gcd(a,b) 表示整数 a 和 b 的最大公约数(GCD)。
换言之,我们取三个数中较小的两个数的 gcd,而忽略最大的那个数。
给定一个包含 n 个元素的数组 a,请计算对所有满足 1≤i<j<k≤n 的三元组 (i,j,k) 对应的 f(ai,aj,ak) 的总和。
更形式化地,计算
i=1∑nj=i+1∑nk=j+1∑nf(ai,aj,ak).
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10). The description of the test cases follows.
The first line of each test case contains a single integer n (3≤n≤8⋅104) — length of the array a.
The second line of each test case contains n integers, a1,a2,…,an (1≤ai≤105) — elements of the array a.
It is guaranteed that the sum of n over all test cases does not exceed 8⋅104.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤10)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(3≤n≤8⋅104)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤105)—— 数组 a 的元素。
保证所有测试用例的 n 之和不超过 8⋅104。
输出格式
For each test case, output a single number — the sum from the problem statement.
对于每个测试用例,输出一个数字——即题目陈述中要求的和。
输入输出样例
输入#1
2 5 2 3 6 12 17 8 6 12 8 10 15 12 18 16
输出#1
24 203
说明/提示
In the first test case, the values of f are as follows:
- i=1, j=2, k=3, f(ai,aj,ak)=f(2,3,6)=gcd(2,3)=1;
- i=1, j=2, k=4, f(ai,aj,ak)=f(2,3,12)=gcd(2,3)=1;
- i=1, j=2, k=5, f(ai,aj,ak)=f(2,3,17)=gcd(2,3)=1;
- i=1, j=3, k=4, f(ai,aj,ak)=f(2,6,12)=gcd(2,6)=2;
- i=1, j=3, k=5, f(ai,aj,ak)=f(2,6,17)=gcd(2,6)=2;
- i=1, j=4, k=5, f(ai,aj,ak)=f(2,12,17)=gcd(2,12)=2;
- i=2, j=3, k=4, f(ai,aj,ak)=f(3,6,12)=gcd(3,6)=3;
- i=2, j=3, k=5, f(ai,aj,ak)=f(3,6,17)=gcd(3,6)=3;
- i=2, j=4, k=5, f(ai,aj,ak)=f(3,12,17)=gcd(3,12)=3;
- i=3, j=4, k=5, f(ai,aj,ak)=f(6,12,17)=gcd(6,12)=6.
The sum over all triples is 1+1+1+2+2+2+3+3+3+6=24.
In the second test case, there are 56 ways to choose values of i, j, k. The sum over all f(ai,aj,ak) is 203.
在第一个测试用例中,函数 f 的取值如下:
- i=1, j=2, k=3, f(ai,aj,ak)=f(2,3,6)=gcd(2,3)=1;
- i=1, j=2, k=4, f(ai,aj,ak)=f(2,3,12)=gcd(2,3)=1;
- i=1, j=2, k=5, f(ai,aj,ak)=f(2,3,17)=gcd(2,3)=1;
- i=1, j=3, k=4, f(ai,aj,ak)=f(2,6,12)=gcd(2,6)=2;
- i=1, j=3, k=5, f(ai,aj,ak)=f(2,6,17)=gcd(2,6)=2;
- i=1, j=4, k=5, f(ai,aj,ak)=f(2,12,17)=gcd(2,12)=2;
- i=2, j=3, k=4, f(ai,aj,ak)=f(3,6,12)=gcd(3,6)=3;
- i=2, j=3, k=5, f(ai,aj,ak)=f(3,6,17)=gcd(3,6)=3;
- i=2, j=4, k=5, f(ai,aj,ak)=f(3,12,17)=gcd(3,12)=3;
- i=3, j=4, k=5, f(ai,aj,ak)=f(6,12,17)=gcd(6,12)=6。
所有三元组对应的函数值之和为 1+1+1+2+2+2+3+3+3+6=24。
在第二个测试用例中,共有 56 种选择 i、j、k 的方式。所有 f(ai,aj,ak) 的总和为 203。
输入解题思路,AI测评打分。不知道怎么写?