原题链接:http://codeforces.com/contest/1047/problem/C

Enlarge GCD

Mr. F has nnn positive integers, a1,a2,⋯ ,ana_1,a_2,\cdots,a_na1​,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)n (2≤n≤3⋅10^5)n(2≤n≤3⋅105) — the number of integers Mr. F has.

The second line contains nnn integers, a1,a2,⋯ ,an(1≤ai≤1.5⋅107)a_1,a_2,\cdots,a_n (1≤a_i≤1.5⋅10^7)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 111 in the beginning. You can remove 111 so that the greatest common divisor is enlarged to 222. The answer is 111.

In the second example, the greatest common divisor is 333 in the beginning. You can remove 666 and 999 so that the greatest common divisor is enlarged to 151515. There is no solution which removes only one integer. So the answer is 222.

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

题解

先把所有数除个gcdgcdgcd,再开个桶记录除gcdgcdgcd后的数,枚举一下所有质数,统计含有该质数的数的个数取maxmaxmax,最后的答案就是n−maxn-maxn−max。

因为质数的个数大约为nlog⁡n\frac{n}{\log n}lognn​个,枚举倍数的复杂度大约也是log⁡n\log nlogn的,所以总复杂度O(n)O(n)O(n)。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e7+5,N=3e5+5;
int cot[M],val[N],n,g,mx,ans;
bool vis[M];
void in(){scanf("%d",&n);}
void ac()
{for(int i=1;i<=n;++i)scanf("%d",&val[i]),g=__gcd(g,val[i]);for(int i=1;i<=n;++i)++cot[mx=max(val[i]/g,mx),val[i]/g];for(int i=2,j,s;i<=mx;++i,ans=max(ans,s))if(!vis[i])for(j=i,s=0;j<=mx;j+=i)s+=cot[j],vis[j]=1;printf("%d",ans?n-ans:-1);
}
int main(){in();ac();}

CF1047C Enlarge GCD相关推荐

  1. Enlarge GCD CodeForces - 1034A(欧拉筛+最大公约数)

    题意: 给出n个数,删去其中一些使得总的gcd(最大公约数)最大 题目: Mr. F has n positive integers, a1,a2,-,an. He thinks the greate ...

  2. 【CodeForces - 1047C】Enlarge GCD(数学,枚举,预处理打表,思维)

    题干: F先生有n个正整数,a1,a2,...,an 他认为这些整数的最大公约数太小了,所以他想通过删除一些整数来扩大它 您的任务是计算需要删除的最小整数数,以便剩余整数的最大公约数大于所有整数的公约 ...

  3. CF1034A Enlarge GCD

    emmm真是简短的题意,真是妙题一个 假设原数组的gcdgcdgcd为ggg 考虑唯一分解的形式: gcd=∏pimin(c1,c2,c3....ci)gcd=\prod p_i^{min(c1,c2 ...

  4. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  5. Codeforces Round #511 (Div. 1) 题解

    在学校熬夜打$cf$找死啊. A - Enlarge GCD 先求个$gcd$,然后对于每个$a[i]$都除以$gcd$. 之后我们只需要统计每个质数整除的个数即可. 因为带上所有数的$gcd$一定是 ...

  6. LSNU寒假集训 题解

    目录 LSNU寒假第一场(基础算法+几何) A - Fifa and Fafa B - Anton and Lines C - Minimum path D - Thematic Contests E ...

  7. OI每周刷题记录——lrllrl

    看这标题就知道我是模仿的hzwer大佬,远程%%% 大佬的OI经历让蒟蒻我深受感触,为了晚一些AFO本蒟蒻也得加油了 从高二上期第一周开始计数,每个星期天更一次,一直更到我AFO 如果这是我此生最后一 ...

  8. CF803C Maximal GCD

    洛谷 CF 分析 考虑从 \(k\) 个数的 \(gcd\) 入手. 设他们的 \(gcd\) 为 \(d\) .则有 \(d|n\) ,那么这 \(k\) 个数都除以 \(d\) 剩下的和即为 \( ...

  9. 数论(一)——素数,GCD,LCM

    这是一个数论系列:) 一.素数 ×费马小定理 Theorem: 设 p 是一个素数,a 是一个整数且不是 p 的倍数,那么 很遗憾,费马小定理的逆定理是不成立的.对 a = 2,满足的非素数 n 是存 ...

  10. [CQOI2014]数三角形 组合数 + 容斥 + gcd

    推导过程 : 组合数+容斥原理+gcd 正确做法是暴力的一种优化,ans=所有情况 - 平行坐标轴的三点共线 - 斜线三点共线 如果快速求斜线三点共线: 首先要知道一个结论,对于点(a,b) (x,y ...

最新文章

  1. 《数据结构与算法 C语言版》—— 3.8习题
  2. 《巨富们给年轻人的45个忠告》读后感
  3. 精通python网络爬虫-精通Python网络爬虫:核心技术、框架与项目实战 PDF
  4. Contest Leaderboard(分组、聚合、连接)
  5. Qt编译过程中出现的问题
  6. oracle executesqlcommand,Oracle Execute Command Sql Script in Win
  7. 英文简历的几种常见形式
  8. (*长期更新)软考网络工程师学习笔记——Linux操作系统中的vi/vim 编辑器详解
  9. 阿里再添一员虎将!刚刚入职的80后硅谷科学家贾扬清到底有多牛?
  10. ruby中的特殊字符
  11. 全国计算机一级考试网络知识,全国计算机等级考试一级网络基础知识复习题及答案.doc...
  12. ISA Server 2006防火墙安装与管理指南(含企业版NLB与CARP的配置)
  13. Java中String、StringBuffer 、StringBuilder
  14. 网络营销数据解读(九)——客户族群细分(Segmentation)2-2
  15. 最新一键修改手机MAC地址和路由器wifi物理地址
  16. python平方和psum_python求和函数sum()详解
  17. 有管网气体消防系统小知识来啦
  18. 冯言冯语说DSP(二)序列的z变换
  19. 360度全景视频拍摄小技巧,这些方法你要知道
  20. 微信公众平台js算法逆向

热门文章

  1. 基础集合论 第一章 9 幂集
  2. 向量函数具有固定模的充要条件
  3. android 根据名字获取资源,Android利用资源名称获取其ID(一)—getIdentifier()
  4. MySQL的LIKE模糊查询优化
  5. 万能的搜索--之DFS(二)
  6. Linux下安装、配置、启动Apache
  7. java控制台输入输出
  8. 浏览器Firefox新标签页默认打开地址设置
  9. Linux环境下安装MySQL 5.6.45
  10. Oracle查询优化改写技巧与案例总结一