Enlarge GCD

https://codeforces.com/problemset/problem/1034/A

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−1.


题意:去掉最少的数使得剩下数字的gcd变大。

思路:gcd是最多的共有因子的乘积。先把整个序列的gcd求出来,然后每个数/=gcd,每个数都剩下一部分因子数。再在每个剩下的因子数中找一个出现次数最多的,这样删去的数最少并且新的gcd也能变大。

这个题的启发:gcd是多个共有因子的乘积,思考的时候可以把每个数都分解质因数去思考。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1.5e7+5;
typedef long long LL;
int v[maxn],primes[maxn];//v[i]存的是i的最小质因子,primes[m]里面存筛出来的质数
int a[maxn];
int p[maxn];
int m=0;//质数数量
void getprimes(int n)
{
//	memset(v,0,sizeof(v));//最小质因子 
	m=0;//质数数量	
	for(int i=2;i<=n;i++)
	{
		if(v[i]==0) {v[i]=i;primes[++m]=i;}//i是质数 
		//给当前的数i乘上一个质因子
		for(int j=1;j<=m;j++)
		{
			//i有比primes[j]更小的质因子,或者超出n的范围,停止循环 
			if(primes[j]>v[i]||primes[j]>n/i)	break;
			//primes[j]是合数i*primes[j]的最小质因子 
			v[i*primes[j]]=primes[j];
		} 
	}
//	for(LL i=1;i<=m;i++) cout<<primes[i]<<endl; 
} 
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  getprimes(maxn);
  int n;cin>>n;
  int k=0;
  for(int i=1;i<=n;i++){
	  cin>>a[i];
	  k=__gcd(k,a[i]);
  }

  for(int i=1;i<=n;i++){
  	a[i]/=k;
  	for(int j=1;j<=m;j++){
  		if(primes[j]*primes[j]>a[i]) break;
		if(a[i]%primes[j]==0){
			p[primes[j]]++;
			while(a[i]%primes[j]==0){
				a[i]/=primes[j];
			}
		}  	
	}
	if(a[i]>1) p[a[i]]++; 
  }
  int ans=0;
  for(int i=2;i<maxn;i++)
  {
  	ans=max(ans,p[i]);
  }
  if(ans) cout<<(n-ans)<<endl;
  else cout<<"-1"<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108352506
今日推荐