A16303 | Small GCD
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 \le b \le c$ . Then return $\gcd(a, b)$ , where $\gcd(a, b)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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(a_i, a_j, a_k)$ for each $i$ , $j$ , $k$ , such that $1 \le i < j < k \le 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).$
Order the numbers $a$ , $b$ , $c$ in such a way that $a \le b \le c$ . Then return $\gcd(a, b)$ , where $\gcd(a, b)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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(a_i, a_j, a_k)$ for each $i$ , $j$ , $k$ , such that $1 \le i < j < k \le 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).$
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10$ ). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $3 \le n \le 8 \cdot 10^4$ ) — length of the array $a$ .
The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^5$ ) — elements of the array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $8 \cdot 10^4$ .
The first line of each test case contains a single integer $n$ ( $3 \le n \le 8 \cdot 10^4$ ) — length of the array $a$ .
The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^5$ ) — elements of the array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $8 \cdot 10^4$ .
输出格式
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(a_i,a_j,a_k)=f(2,3,6)=\gcd(2,3)=1$ ;
- $i=1$ , $j=2$ , $k=4$ , $f(a_i,a_j,a_k)=f(2,3,12)=\gcd(2,3)=1$ ;
- $i=1$ , $j=2$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,3,17)=\gcd(2,3)=1$ ;
- $i=1$ , $j=3$ , $k=4$ , $f(a_i,a_j,a_k)=f(2,6,12)=\gcd(2,6)=2$ ;
- $i=1$ , $j=3$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,6,17)=\gcd(2,6)=2$ ;
- $i=1$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,12,17)=\gcd(2,12)=2$ ;
- $i=2$ , $j=3$ , $k=4$ , $f(a_i,a_j,a_k)=f(3,6,12)=\gcd(3,6)=3$ ;
- $i=2$ , $j=3$ , $k=5$ , $f(a_i,a_j,a_k)=f(3,6,17)=\gcd(3,6)=3$ ;
- $i=2$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=f(3,12,17)=\gcd(3,12)=3$ ;
- $i=3$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=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(a_i,a_j,a_k)$ is $203$ .
- $i=1$ , $j=2$ , $k=3$ , $f(a_i,a_j,a_k)=f(2,3,6)=\gcd(2,3)=1$ ;
- $i=1$ , $j=2$ , $k=4$ , $f(a_i,a_j,a_k)=f(2,3,12)=\gcd(2,3)=1$ ;
- $i=1$ , $j=2$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,3,17)=\gcd(2,3)=1$ ;
- $i=1$ , $j=3$ , $k=4$ , $f(a_i,a_j,a_k)=f(2,6,12)=\gcd(2,6)=2$ ;
- $i=1$ , $j=3$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,6,17)=\gcd(2,6)=2$ ;
- $i=1$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=f(2,12,17)=\gcd(2,12)=2$ ;
- $i=2$ , $j=3$ , $k=4$ , $f(a_i,a_j,a_k)=f(3,6,12)=\gcd(3,6)=3$ ;
- $i=2$ , $j=3$ , $k=5$ , $f(a_i,a_j,a_k)=f(3,6,17)=\gcd(3,6)=3$ ;
- $i=2$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=f(3,12,17)=\gcd(3,12)=3$ ;
- $i=3$ , $j=4$ , $k=5$ , $f(a_i,a_j,a_k)=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(a_i,a_j,a_k)$ is $203$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted