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 nn (2≤n≤3⋅105) — the number of integers Mr. F has.
The second line contains nn 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 4Output
1Input
4 6 9 15 30Output
2Input
3 1 1 1Output
-1Note
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.
题意:给你n个数,最少删除n个数中的几个可以使得 剩下 的数的最大公约数大于原本n个数的最大公约数。
思路:先找出n个数的最大 公约数,然后把n个数 依次除去 他们的最大公约数得到全新的N个数。这N个数的最大公约数为1.
然后对这N个全新的数【唯一分解定理】进行 分解。 统计分解成每个素数的 个数。最多的那个 就是去除m个数后,剩余
数 的最大公约数。m = n(n个数) - num[数量最多的素数];(num记录的是每个素数的个数)
代码:
这里 运用了一种比素数打表更高效 的素数筛:https://blog.csdn.net/weixin_45177251/article/details/106130112
#include
#include
#include
#include
using namespace std;
int a[300010];
int cnt[15000010];
bool book[15000010];
int su[15000010];
int r;
int gcd(int a,int b)//欧几里得求最大公约数
{
if(b==0)
return a;
return gcd(b,a%b);
}
void prime()//求1~15000000的素数
{
memset(book,0,sizeof(book));
book[0]=book[1]=1;
for(int i=2; i<15000000; i++)
{
if(book[i]==0)
{
su[r++]=i;
}
for(int j=0; j1)
cnt[a[i]]++;
}
int ans=n;
for(int i=2; i<15000000; i++)
ans=min(ans,n-cnt[i]);
if(ans==n)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}