题意:

给出n个数,删去其中一些使得总的gcd(最大公约数)最大

题目:

Mr. F has n positive integers, a1,a2,…,an.

He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.

But this problem is too simple for him, so he does not want to do it by himself. If you help him, he will give you some scores in reward.

Your task is to calculate the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.

Input

The first line contains an integer n (2≤n≤3⋅105) — the number of integers Mr. F has.

The second line contains n integers, a1,a2,…,an (1≤ai≤1.5⋅107).

Output

Print an integer — the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.

You should not remove all of the integers.

If there is no solution, print «-1» (without quotes).

Examples

Input

3
1 2 4

Output

1

Input

4
6 9 15 30

Output

2

Input

3
1 1 1

Output

-1

Note

In the first example, the greatest common divisor is 1 in the beginning. You can remove 1 so that the greatest common divisor is enlarged to 2. The answer is 1.

In the second example, the greatest common divisor is 3 in the beginning. You can remove 6 and 9 so that the greatest common divisor is enlarged to 15. There is no solution which removes only one integer. So the answer is 2.

In the third example, there is no solution to enlarge the greatest common divisor. So the answer is −1.

分析:

这道题的思路取自素数筛,甚至是欧拉筛,我们要删除一些数字,
使得gcd增长,那么,我们就得把除去公共gcd之后的数进行一次筛选,
从最小的开始,我们求出那些个有相同质因数的数的数量的最大值。
举个例子: 对于2 4 4 8 8 ; 他们的gcd=2,
除以2以后:1 2 2 4 4,我们从2开始遍历“1~N”,
发现有4个是2的倍数,所以我们删除的是N-4=1。
或许一个例子不够形象: 对于3 6 6 9 ; 它们的gcd=3,
除以3以后:1 2 2 3,我们从2开始,2个是2的倍数, 1个是3的倍数,
之后5、7、11…没有这样的数了,所以删除N-2==2即可。
思路就是这样的。
特殊的,对于除完之后只剩下“1,1,1,1,…”这样的数,我们直接printf("-1\n")。
注意:容易超时,得想办法

AC代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int M=1.5e7+5;
const int N=3e5+10;
int sum[M];
int a[N],book[M];
int p[M],tot;
void init()
{memset(book,0,sizeof(book));for(int i=2; i<M; i++){if(!book[i])p[tot++]=i;for(int j=0; j<tot&&i*p[j]<M; j++)///减少复杂度{book[i*p[j]]=1;if(i%p[j]==0)break;}}/**for(int i=2; i<sqrt(M); i++){if(!book[i])p[tot++]=i;for(int j=i*2; j<M; j+=i)book[j]=1;}*/
}
int main()
{int n,x;scanf("%d",&n);init();int d=0;for(int i=1; i<=n; i++)//找到所有数的公因数{scanf("%d",&a[i]);d=__gcd(a[i],d);}for(int i=1; i<=n; i++){x=a[i]/d;///把所有的数都除以它们的gcd,则问题变为:保留最多的数,使得它们的gcd不等于1。for(int j=0; p[j]*p[j]<=x&&j<tot; j++)///枚举gcd的时候可以只枚举质数。可以通过线性筛将所有的质数O(n)时间复杂度筛出来{//除去公因数数之后将所有的数分解if(x%p[j]==0)sum[p[j]]++;//对素因数计数while(x%p[j]==0)x/=p[j];}if(x>1)sum[x]++;}int ans=0;for(int i=0; i<tot; i++)ans=max(sum[p[i]],ans);//找到最大的素因数出现次数if(ans==0)///假设所有数都等于1,显然无解。printf("-1\n");elseprintf("%d\n",n-ans);
}

Enlarge GCD CodeForces - 1034A(欧拉筛+最大公约数)相关推荐

  1. 【BZOJ2818】Gcd,数论练习之欧拉筛

    传送门 写在前面:比较简单的数论题目了 思路:对i来说,所有与i互质的数和i都乘同一个质数p,那么得到的两个数的gcd一定是p,所以我们就可以利用这个来搞一搞了,对1-n的phi预处理出来(欧拉筛), ...

  2. 欧拉筛(bzoj 2818: Gcd)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 6707  Solved: 2952 [Submit][Status][Discu ...

  3. cf 1062d 思维 欧拉筛变形

    http://codeforces.com/contest/1062/problem/D 题意:给个n ,在n之内 所有(a,b) ,如果存在唯一的x 使a*|x| == b 或者 b* |x| == ...

  4. 欧拉筛 筛法求素数 及其例题 时间复杂度O(n)

    埃式筛法尽管不错,但是确实做了许多无用功,某个数可能会被重复的筛好几次,欧拉筛解决了这个方法,下面为代码: 注意理解if(i%prim[j]==0) break; 大佬讲的不错的博客,我就不做复读机了 ...

  5. 线性筛素数(欧拉筛)

    欧拉筛是O(n)复杂度的筛素数算法,1秒内埃筛能处理1e6的数据,而1e7的数据就必须用欧拉筛了. 埃筛的基本思想是:素数的倍数一定是合数. 欧拉筛基本思想是:任何数与素数的乘积一定是合数 算法概述: ...

  6. 【数的专题】——欧拉筛

    上题:https://www.luogu.org/problemnew/show/P3383 当你总是觉得筛质数太慢的时候,不妨来试一下欧拉筛: 基本原理: 设一个整数x,保证它只被它的最小质因子筛去 ...

  7. 素数筛选法(埃氏筛 欧拉筛)

    质数筛选法 文章目录 质数筛选法 前言 一.埃氏筛 O(nloglogn)O(nloglogn)O(nloglogn) 二.欧拉筛O(n)O(n)O(n) 总结 前言 当需要大范围内的素数时,例如1e ...

  8. 埃氏筛 线性筛(欧拉筛) 算法解析

    埃氏晒 埃拉托斯特尼筛法,简称埃氏晒,是一种用来求自然数n以内的全部素数. 他的基本原理是,如果我们要获得小于n的所有素数,那就把不大于根号n的所有素数的倍数剔除. 埃氏晒的原理很容易理解,一个合数, ...

  9. 素数的线性筛法java,埃氏筛 线性筛(欧拉筛) 算法解析

    埃氏晒 埃拉托斯特尼筛法,简称埃氏晒,是一种用来求自然数n以内的全部素数. 他的基本原理是,如果我们要获得小于n的所有素数,那就把不大于根号n的所有素数的倍数剔除. 埃氏晒的原理很容易理解,一个合数, ...

最新文章

  1. active mq topic消费后删除_Spring cloud stream 整合mq
  2. EasyUI--datebox设置默认时间
  3. 猪的诱惑(2005-12-25 15:45:05)(新浪)
  4. 在centos8 stream启用 Extra Packages
  5. 【随机】Kuroni and the Punishment(CF1305F)
  6. jconsole查看连接数_在JConsole和VisualVM中查看DiagnosticCommandMBean
  7. 关于MQTT、HTTP、WebService
  8. 分布式锁实现方式介绍和Zookeeper实现原理
  9. onenote复制出来是图片_OneNote入门篇
  10. BZOJ 1025: [SCOI2009]游戏
  11. 交通部 城轨交通运营管理规定_重庆启动节前轨道交通运营管理安全执法检查...
  12. NVIDIA Control Panel 闪退(英伟达控制面板闪退)
  13. 怎样做小游戏挖金子(VC,源码4)
  14. VMware虚拟机 网桥模式详细配置(及计算机网络基本知识 IP/子网掩码/网关/DNS)
  15. 超详细,从零开始搭建阿里云服务器(centos7)第一章 远程连接
  16. 通过新浪天气api查询天气
  17. VNC Connect远程工具使用-使用体验极高
  18. HTTP常用端口号与对应的服务说明
  19. 怎么做拼多多活动|成都百择
  20. Kubernetes资源调度之污点与Pod容忍度

热门文章

  1. linux之 !!命令
  2. Android之第一次不显示EditText光标
  3. java获取tomcat目录结构_tomcat目录结构简介_动力节点Java学院整理
  4. 豆瓣评分9.4!这一部纪录片,探秘中国的未至之境!
  5. 为什么说,每个人都应该多读些书?
  6. 高校教师抄袭豆瓣博主文章,学校证实:基本属实!记过并调离教学科研岗位...
  7. 梦真的是反的 | 今日最佳
  8. 双十一来了,揭秘菜鸟物流背后的那些算法黑科技
  9. php7 不是有效的32位,Win7系统安装软件提示“不是有效的win32应用程序”怎么办?...
  10. php 查询数据是否大于,怎么实现从数据查询数据的时候判断如果数据大于N条分次查询 递归吗?...