题目:

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. iOS与HTML5交互方法总结(修正)

    摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...

  2. 3-No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案

    转载:http://www.360doc.com/content/15/0316/15/9200790_455576135.shtml 由于我在配置安卓环境时也碰到了类似问题,用这篇博客解决了主要问题 ...

  3. postfix配置积累(不断的积累)

    postfix 配置 1.mail_name 默认是Postfix.在收件人信头可以查看,如果不想让别人知道你是用postfix发的,则可以改成其它名字,如:postconf -e mail_name ...

  4. openvswitch安装

    安装前准备软件包及版本 Gcc pkg-config autoconf automake m4 python2.X 下载OVS软件包 http://openvswitch.org/releases/o ...

  5. wireshark抓取qq数据包

    抓包接口设置成本地连接 点击start,登录qq,输入oicq进行过滤qq包 找到第一个OICQ,点击后,点击oicq-IM software 可以看到自己登录的QQ号码为765343409 本机IP ...

  6. 编写高质量代码改善C#程序的157个建议——建议27:在查询中使用Lambda表达式

    建议27:在查询中使用Lambda表达式 LINQ实际上是基于扩展方法和Lambda表达式的.任何LINQ查询都能通过扩展方法的方式来代替. var personWithCompanyList = f ...

  7. 浅谈delphi创建Windows服务程序与窗体实现交互

    我想实现的功能是创建一个服务程序,然后在服务Start时动态创建一个窗体Form,然后把Form缩小时变成TrayIcon放在Windows托盘上. 我在服务程序的OnStart事件中写到 Start ...

  8. Go语言最佳实践——通道和并发

    何时关闭通道: 第一,只有在后面要检查通道是否关闭的时候才需要显式地关闭通道: 第二,应该由发送端的goroutine关闭通道,而不是由接收端的goroutine来完成: 第三,如果通道并不需要检查是 ...

  9. 关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary AS

    1.添加用户"Network Service” 和 “IIS_IUSERS” 读下面目录的读写权限 a) C:\Windows\Temp b) C:\Windows\Microsoft.NE ...

  10. Javascript判断两个点(经纬度)的距离,以及是否在某个区域内(经纬度字符串)

    JS计算两个点(经纬度)的距离 function getGreatCircleDistance(lat1, lng1, lat2, lng2) { var EARTH_RADIUS = 6378137 ...