题面

原题链接(CF1627D)

You have an array \(a_1,a_2,…,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≠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) 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?

Input

The first line consists of a single integer \(n\) \((2≤n≤10^6)\).

The second line consists of \(n\) integers \(a_1,a_2,…,a_n (1≤a_i≤10^6)\). All ai are distinct.

Output

Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.

Examples

input

5
4 20 1 25 30

output

3

input

3
6 10 15

output

4

Note

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.

大意

给出一个整数的不可重集合,可以对其重复执行以下操作:对于集合内的任意两个数,计算它们的最大公约数(GCD),如果这个GCD不存在于原集合中,就把它加入这个集合。新加入的数也可以在之后的操作中参与计算。重复此操作,求最多能进行的操作次数。

题解

题面的数据达到了1e6,很显然不能枚举每一对数来检查。但是,集合内的元素大小也在1e6范围内,因此可以开一个大小1e6的数组记录这个数有没有出现过。同时GCD还有一个很有用的性质:两个数的GCD不大于两个数的最小值,也就是\(gcd(a_i,a_j) \leq \min (a_i,a_j)\)。所以,如果一个数能够被加入这个集合,它一定比原集合的最大元素还要小。所以我们考虑对答案进行枚举,从1到这个最大值的所有整数全部判断一遍。

怎么判断这个数能否被加入集合呢?首先,如果已经存在于集合中的数肯定不能被加入,因此可以跳过。然后,对于不在集合中的数\(i\),要想让它被加入集合中,必须存在两个数使其最大公约数为\(i\)。也就是说,集合中存在两个数\(a_p=pi,a_q=qi\),其中\(p,q\)互质。由此,我们可以考虑对每一个没有出现的数\(i\),检查所有的\(2i,3i,\dots ,ki \leq \max(a)\)是否出现在集合中。但是,判断互质是一件非常麻烦的事情。所以联想到GCD的另一个重要性质:三个数的GCD等于其中两个数的GCD和另外一个数的GCD,也就是说\(gcd(a_i,a_j,a_k)=gcd(gcd(a_i,a_j),a_k)\),这个性质对\(n\)个数也成立。假设对所有的\(ki\),有三个数\(k_1i,k_2i,k_3i\)出现在了集合中,其中\(k_n\)两两不互质,但是\(k_1,k_2,k_3\)互质,那么我们可以把\(k_4i=gcd(k_1i,k_2i)\)加入到集合中(如果它已经在集合中那么就不用理睬了),然后计算\(gcd(k_4i,k_3i)=i\),将其加入集合。对于\(n\)个元素也同理。据此,我们可以得出,对于任意一个未出现在集合内的数\(i\),它能加入集合的充要条件是集合内所有\(i\)的倍数的数的GCD等于\(i\)。

枚举每一个可能的\(i\),对每一个\(i\)作判断计算次数为\(\frac{n}{i}\),对此求和(调和级数)得到线性乘以对数的复杂度,求GCD的复杂度大约为对数级别,因此总体复杂度为\(O(n \log ^2 n)\)。

代码如下:

#include <bits/stdc++.h>
#define GRP \
int T; \
cin >> T; \
rep(C, 1, T)
#define FAST \
ios::sync_with_stdio(false); \
cin.tie(0);
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define elif else if
#define mem(arr, val) memset(arr, val, sizeof(arr))
typedef long long ll;
typedef unsigned long long ull; int n;
int a[1000010];
bool vis[1000010];
int maxx, cnt, num;
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
} int main()
{
FAST;
cin >> n;
mem(vis, 0);
cnt = 0;
cin >> a[1];
maxx = a[1];
vis[a[1]] = true;
rep(i, 2, n)
{
cin >> a[i];
maxx = max(maxx, a[i]);
vis[a[i]] = true;
}
rep(i, 1, maxx) //进行枚举
{
if (vis[i])
{
continue;
}
num = 0; //令一个数和0的GCD等于该数本身,可以简化代码
for (int j = i * 2; j <= maxx; j += i) //枚举所有的倍数
{
if (!vis[j])
{
continue;
}
num = gcd(num, j);
}
if (num == i)
{
cnt++;
}
}
cout << cnt << endl;
return 0;
}

Not Adding - 题解【数学,枚举】的更多相关文章

  1. 51nod 1943 联通期望 题解【枚举】【二进制】【概率期望】【DP】

    集合统计类期望题目. 题目描述 在一片大海上有 \(n\) 个岛屿,规划建设 \(m\) 座桥,第i座桥的成本为 \(z_i\),但由于海怪的存在,第 \(i\) 座桥有 \(p_i\) 的概率不能建 ...

  2. [题解]数学期望_luogu_P1850_换教室

    数学期望dp,题面第一次见很吓人,然而从CCF语翻译成人话就简单多了, 开始一般会想到用 f [ i ] [ j ]表示前 i 个课程申请 j 次的期望,然而其实会发现转移的时候还和上一次的情况有关( ...

  3. bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1779  Solved: 823[Submit][Sta ...

  4. 2-08. 用扑克牌计算24点(25) (ZJU_PAT 数学 枚举)

    题目链接:http://pat.zju.edu.cn/contests/ds/2-08 一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌.即得到4个1~ ...

  5. The Golden Age CodeForces - 813B (数学+枚举)

    Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a  ...

  6. Codeforces 813B The Golden Age(数学+枚举)

    题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ≤ 10^18, 1 ≤ l ≤ r ≤ 10^18),求出l到 ...

  7. FZU 2125 简单的等式 【数学/枚举解方程式】

    现在有一个等式如下:x^2+s(x,m)x-n=0.其中s(x,m)表示把x写成m进制时,每个位数相加的和.现在,在给定n,m的情况下,求出满足等式的最小的正整数x.如果不存在,请输出-1. Inpu ...

  8. 【数学+枚举】OpenJ_POJ - C17J Pairs

    https://vjudge.net/contest/171652#problem/J [题意] 问有多少个正整数对(x,y),使得存在正整数p,q满足 1 <= T <= 15 1 &l ...

  9. ZROI17普及23-A.如烟题解--技巧枚举

    题目链接 因版权原因不予提供 分析 别看这是普及模拟赛,其实基本上是提高难度...像这题做NOIpT1的话也说的过去 有个很显然的暴力思路就是枚举c,a,b,时间复杂度\(O(N^3)\), 然后正解 ...

随机推荐

  1. 2022届字节跳动校园招聘&内推「【内推码】:4J8CA3W」

     字节跳动2022校园招聘全面启动!8000+Offer来袭,持续内推. 内推时间:2021年8月12日-10月31日 招聘对象:2021年9月-2022年8月期间毕业,且最高学历毕业后无全职工作经验 ...

  2. BUAA_DS_北航数据结构:输出全排列

    输入一个数 \(n\),输出 \(1\sim n\) 的所有全排列,每个排列占一行,每个字符保留 \(5\) 个场宽.勤奋的同学一定已经开始打表了是吧. 说是能做肯定不是骗大家,那怎么做呢~ 其实回溯 ...

  3. [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业

    [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj   bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...

  4. redis整理:常用命令,雪崩击穿穿透原因及方案,分布式锁实现思路,分布式锁redission(更新中)

    redis个人整理笔记 reids常见数据结构 基本类型 String: 普通key-value Hash: 类似hashMap List: 双向链表 Set: 不可重复 SortedSet: 不可重 ...

  5. SpringMVC源码解读 - RequestMapping注解实现解读

    SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系  https://www.cnblogs.com/leftthen/p/520840 ...

  6. 讲一讲 kafka 的 ack 的三种机制 ?

    request.required.acks 有三个值 0 1 -1(all) 0:生产者不会等待 broker 的 ack,这个延迟最低但是存储的保证最弱当 server 挂 掉的时候就会丢数据. 1 ...

  7. MySQL 中 InnoDB 支持的四种事务隔离级别名称,以及逐 级之间的区别?

    SQL 标准定义的四个隔离级别为: 1.read uncommited :读到未提交数据 2.read committed:脏读,不可重复读 3.repeatable read:可重读 4.seria ...

  8. 微信小程序答题,怎么设计页面渲染,答完一题,跳到下一题

    想要的效果 1.第一页只显示第一道题的内容,如图红框2.答题后,点击下一题,内容显示第二道题的内容 代码 answer.wxml <!--pages/answer/answer.wxml--&g ...

  9. 【Android开发】安卓炫酷效果集合

    1. android-ripple-background 能产生波浪效果的背景图片控件,可以自定义颜色,波浪扩展的速度,波浪的圈数. github地址 2. android-shapeLoadingV ...

  10. 一个chome的广告拦截小插件

    先附上下载地址:https://chromecj.com/productivity/2015-03/391.html 可以屏蔽绝大多数广告啊,浏览器用起来神清气爽! 下载完成后有一个名字为这个的文件, ...