Codeforce A. Enlarge GCD

A. Enlarge GCD

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mr. F has nn positive integers, a1,a2,…,ana1,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⋅1052≤n≤3⋅105) — the number of integers Mr. F has.

The second line contains nn integers, a1,a2,…,ana1,a2,…,an (1≤ai≤1.5⋅1071≤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

Copy

3
1 2 4

output

Copy

1

input

Copy

4
6 9 15 30

output

Copy

2

input

Copy

3
1 1 1

output

Copy

-1

Note

In the first example, the greatest common divisor is 11 in the beginning. You can remove 11 so that the greatest common divisor is enlarged to 22. The answer is 11.

In the second example, the greatest common divisor is 33 in the beginning. You can remove 66 and 99 so that the greatest common divisor is enlarged to 1515. There is no solution which removes only one integer. So the answer is 22.

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

 

本来的想法是通过把每个数的因子都拆掉这样的话找出因子出现次数第二多的数T,那么答案就是Ans=n-t

然而虽然只有300000个数但是数的大小却有 1.5*10的7次方 要是每个数都很大那就绝对爆炸啊

 

真正的题解是:找出n个数的gcd 然后只要找大于这个gcd的ans_gcd能被n个数整除次数最多的就好了(想到这我一开始绝对可能会TLE因为O(n*n)复杂度太高 但是关键是 比如找能被2整除的那么 一个数如果因子有 4 或者 6 或者 8那么他的因子只要规定是2就好了)

#include <stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
using namespace std; 
int a[20000001];
int need[20000001];
int main(int argc, char *argv[])
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		int i;
		int min1=INF;
		int maxn=-1;
		for(i=0;i<n;i++)
		{
			int t;
			scanf("%d",&t);
			a[t]++;
			maxn=max(maxn,t);
			if(i==0)
			{
				min1=t;			
			}
			else
			{
				min1=__gcd(min1,t);
			}
		}
		//printf("%d\n",min1);
		memset(need,0,sizeof(need));
		int start=min1+1;
		int ans=INF;
		for(i=start;i<=maxn;i++)
		{
			if(!need[i])
			{
				int tot=0;
				int j;
				for(j=i;j<=maxn;j+=i)
				{
					need[j]=1;
					//printf("%d\n",j);			
					tot+=a[j];
				}
				//printf("%d %d\n",tot,i); 
				ans=min(ans,(n-tot));
			}	
		}
		if(ans==INF||ans==n)
		printf("-1\n");
		else
		printf("%d\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值