A14810 | Not Adding
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have an array $a_1, a_2, \dots, a_n$ consisting of $n$ distinct integers. You are allowed to perform the following operation on it:
- Choose two elements from the array $a_i$ and $a_j$ ( $i \ne j$ ) such that $\gcd(a_i, a_j)$ is not present in the array, and add $\gcd(a_i, a_j)$ to the end of the array. Here $\gcd(x, y)$ denotes [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$ .
Note that the array changes after each operation, and the subsequent operations are performed on the new array.
What is the maximum number of times you can perform the operation on the array?
- Choose two elements from the array $a_i$ and $a_j$ ( $i \ne j$ ) such that $\gcd(a_i, a_j)$ is not present in the array, and add $\gcd(a_i, a_j)$ to the end of the array. Here $\gcd(x, y)$ denotes [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$ .
Note that the array changes after each operation, and the subsequent operations are performed on the new array.
What is the maximum number of times you can perform the operation on the array?
输入格式
The first line consists of a single integer $n$ ( $2 \le n \le 10^6$ ).
The second line consists of $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \leq a_i \leq 10^6$ ). All $a_i$ are distinct.
The second line consists of $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \leq a_i \leq 10^6$ ). All $a_i$ are distinct.
输出格式
Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.
输入输出样例
输入 #1
5 4 20 1 25 30
输出 #1
3
输入 #2
3 6 10 15
输出 #2
4
In the first example, one of the ways to perform maximum number of operations on the array is:
- Pick $i = 1, j= 5$ and add $\gcd(a_1, a_5) = \gcd(4, 30) = 2$ to the array.
- Pick $i = 2, j= 4$ and add $\gcd(a_2, a_4) = \gcd(20, 25) = 5$ to the array.
- Pick $i = 2, j= 5$ and add $\gcd(a_2, a_5) = \gcd(20, 30) = 10$ to the array.
It can be proved that there is no way to perform more than $3$ operations on the original array.
In the second example one can add $3$ , then $1$ , then $5$ , and $2$ .
- Pick $i = 1, j= 5$ and add $\gcd(a_1, a_5) = \gcd(4, 30) = 2$ to the array.
- Pick $i = 2, j= 4$ and add $\gcd(a_2, a_4) = \gcd(20, 25) = 5$ to the array.
- Pick $i = 2, j= 5$ and add $\gcd(a_2, a_5) = \gcd(20, 30) = 10$ to the array.
It can be proved that there is no way to perform more than $3$ operations on the original array.
In the second example one can add $3$ , then $1$ , then $5$ , and $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted