CF1047C Enlarge GCD

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

Enlarge GCD

Mr. F has n n n positive integers, a 1 , a 2 , ⋯   , a n a_1,a_2,\cdots,a_n 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 ⋅ 1 0 5 ) n (2≤n≤3⋅10^5) n(2n3105) — the number of integers Mr. F has.

The second line contains n n n integers, a 1 , a 2 , ⋯   , a n ( 1 ≤ a i ≤ 1.5 ⋅ 1 0 7 ) a_1,a_2,\cdots,a_n (1≤a_i≤1.5⋅10^7) a1,a2,,an(1ai1.5107).

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 1 1 in the beginning. You can remove 1 1 1 so that the greatest common divisor is enlarged to 2 2 2. The answer is 1 1 1.

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

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

题解

先把所有数除个 g c d gcd gcd,再开个桶记录除 g c d gcd gcd后的数,枚举一下所有质数,统计含有该质数的数的个数取 m a x max max,最后的答案就是 n − m a x n-max nmax

因为质数的个数大约为 n log ⁡ n \frac{n}{\log n} lognn个,枚举倍数的复杂度大约也是 log ⁡ n \log n logn的,所以总复杂度 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();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值