【题解】codeforces1047C[Codeforces Round #511 (Div. 2)]C.Enlarge GCD 最大公约数

32 篇文章 0 订阅
11 篇文章 0 订阅

Description

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.


g = gcd ⁡ ( a 1 , a 2 , ⋯   , a n ) g=\gcd(a_1,a_2,\cdots,a_n) g=gcd(a1,a2,,an) b i = a i / g b_i=a_i/g bi=ai/g,求出 b i b_i bi 中所有因数,个数最多的那个因数即为所求。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=3e5+10,M=15e6+10;
int n,a[N],g,prime[M/10],p,ans,Div[M],cnt[M];
void primetable()
{
	for(int i=2;i<M;i++)
	{
		if(!Div[i])prime[p++]=i,Div[i]=i;
		for(int j=0;j<p&&i*prime[j]<M;j++)
		{
			Div[i*prime[j]]=prime[j];
			if(i%prime[j]==0)break;
		}
	}
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main()
{
	//freopen("in.txt","r",stdin);
	primetable();
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	    scanf("%d",&a[i]);
	g=gcd(a[1],a[2]);
	for(int i=3;i<=n;i++)
	    g=gcd(g,a[i]);
	for(int i=1;i<=n;i++)
	{
		int res=a[i]/g;
		while(res>1)
		{
			int fac=Div[res];
			cnt[fac]++;
			while(res%fac==0)res/=fac;
		}
	}
	for(int i=1;i<M;i++)
	    ans=max(ans,cnt[i]);
	printf("%d\n",ans?n-ans:-1);
	return 0;
}

总结

仔细审题把题意读明白。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值