题目:

Mr. F has nn 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 nn (2≤n≤3⋅10^5) — the number of integers Mr. F has.

The second line contains nn integers, a1,a2,…,an (1≤ai≤1.5⋅10^7).

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.

题意分析:

这题意思就是给你N个数,这N个数会有一个最大公约数G,那么需要去掉K个数,使余下的N-K个数的最大公约数变大。求最小K。

我们从gcd的原理分析,这N个数都除以gcd后,余下的数的最大公约数无法变大时因为不存在公因子,所以我们需要对这N个数进行分类,分类的标准就是是否还有共同的公因子,然后找出包含的数目最多的类别,假设这个类别有M个数。

那么N-M就是我们最终的结果。

这里需要注意的技巧是,在进行类别划分的时候,我们用的素数打表的原理。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 3e5+5;
const int MAX = 1.5e7+5;
int A[MAXN], cnt[MAX];
bool visit[MAX]; int gcd(int a, int b)
{
return b==0?a:gcd(b, a%b);
} int Max(const int a, const int b)
{
return a>b?a:b;
} int main()
{
int N;
while(~scanf("%d", &N))
{
int G, ans;
scanf("%d", &A[0]);
G = A[0];
for(int i = 1; i < N; i++)
{
scanf("%d", &A[i]);
G = gcd(G, A[i]);
} memset(cnt, 0, sizeof(cnt));
memset(visit, true, sizeof(visit)); for(int i = 0; i < N; i++)
cnt[A[i]/G]++; visit[0] = visit[1] = false;
ans = 0;
for(int i = 2; i < MAX; i++)
{
int res = cnt[i];
if(visit[i])
{
for(int j = 2*i; j < MAX; j+=i)
{
visit[j] = false;
res += cnt[j];
}
}
ans = Max(ans, res);
}
printf("%d\n", ans==0?-1:N-ans);
}
return 0;
}

  

C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】的更多相关文章

  1. Codeforces Round #511 (Div. 2)

    Codeforces Round #511 (Div. 2) #include <bits/stdc++.h> using namespace std; int n; int main() ...

  2. Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)

    C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...

  3. 2018.9.21 Codeforces Round #511(Div.2)

    只写了AB,甚至还WA了一次A题,暴露了蒟蒻的本质=.= 感觉考的时候有好多正确或和正解有关的思路,但是就想不出具体的解法或者想的不够深(长)(怕不是过于鶸) 话说CF的E题怎么都这么清奇=.= A. ...

  4. Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

    传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...

  5. Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

    题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...

  6. Codeforces Round #511 (Div. 2) C. Enlarge GCD

    题目链接 题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点. 我交了题后,疯狂CE,我以为爆内存,结果是,我对全局数组赋值, 如果直接赋值,会直接在exe内产生内存,否 ...

  7. Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)

    题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...

  8. Codeforces Round #511 Div.1 A Div.2 C

    嗯切一题走人很开心. gzy-50分比我还惨. 题意:有n个数,去掉尽量少的数使得剩下数的gcd变大. 首先把这n个数都除以gcd,就变成了去掉尽量少的数使得gcd不等于1. 可以枚举一个质数,然后统 ...

  9. B. Cover Points Codeforces Round #511 (Div. 2)【数学】

    题目: There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need t ...

随机推荐

  1. memcache 加载(对象)所遇到的问题。资源

    <?php $mem =new memcache(); if($mem->connect('127.0.0.1','11211')){ echo '连接OK'.'<br>'; ...

  2. Angular24 树形菜单 ???

    待更新... 2018年5月21日15:17:47 参考博文01 参考博文02

  3. java面试题 级hr解答 非技术问题 !=!=未看

    Java基础 ● 集合类以及集合框架:HashMap与HashTable实现原理,线程安全性,hash冲突及处理算法:ConcurrentHashMap: ● 进程和线程的区别: ● Java的并发. ...

  4. OpenCV---resize

    转自http://www.cnblogs.com/korbin/p/5612427.html 在图像处理过程中,有时需要把图像调整到同样大小,便于处理,这时需要用到图像resize() 原函数void ...

  5. CentOS双网卡双IP设置

    CentOS双网卡双IP设置 系统环境:CentOS Linux 网络环境: 两个IP地址,192.168.0.10和10.10.30.2,掩码是255.255.255.0,这两个子网的网关地址分别是 ...

  6. Paper: ImageNet Classification with Deep Convolutional Neural Network

    本文介绍了Alex net 在imageNet Classification 中的惊人表现,获得了ImagaNet LSVRC2012第一的好成绩,开启了卷积神经网络在cv领域的广泛应用. 1.数据集 ...

  7. 一步到位带你入门Selenium

    其实,关于这篇文章发布前还是有很多思考的,我是不想发布的,因为关于selenium的文章博客园里面有很多的介绍,写的详细的,也有写的不详细的,那么我的这篇文章的定位是基于selnium从开始到最后的框 ...

  8. phpmyadmin安全预防

    头疼的安全 之前服务器总是本人黑,千疮百孔,只能一步步的去做一些安全防范,如何防范自然先从如何渗透开始. 文章及建议 前段时间看到了个phpmyadmin提权的教程,很多人都说现在那里还有root帐号 ...

  9. 如何解决某个jar包的依赖冲突问题

    我用的是idea集成开发环境,因此以该开发工具讲解. 首先在在Terminal窗口中,键入:mvn dependency:tree -Dincludes=com.google.guava 如果不加-D ...

  10. 跨域Ajax请求(jQuery JSONP MVC)

    通过jQuery的$.ajax方法发送JSONP请求 js代码 <script type="text/javascript"> function jsonptest2( ...