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

本文探讨了一种算法,旨在解决一个数学问题:如何通过移除最少数量的正整数来增大这些数的最大公约数。该算法首先计算初始最大公约数,然后通过分解每个数并统计各质因数的出现频率,找到能够使最大公约数增大的关键质因数,从而得出移除最少数目的解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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⁡(a1,a2,⋯ ,an)g=\gcd(a_1,a_2,\cdots,a_n)g=gcd(a1,a2,,an)bi=ai/gb_i=a_i/gbi=ai/g,求出 bib_ibi 中所有因数,个数最多的那个因数即为所求。

#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、付费专栏及课程。

余额充值