CF1047C Enlarge GCD

原題鏈接:http://codeforces.com/contest/1047/problem/C

Enlarge GCD

Mr. F has nn positive integers, a1,a2, ,ana_1,a_2,\cdots,a_n.

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(2n3105)n (2≤n≤3⋅10^5) — the number of integers Mr. F has.

The second line contains nn integers, a1,a2, ,an(1ai1.5107)a_1,a_2,\cdots,a_n (1≤a_i≤1.5⋅10^7).

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

題解

先把所有數除個gcdgcd,再開個桶記錄除gcdgcd後的數,枚舉一下所有質數,統計含有該質數的數的個數取maxmax,最後的答案就是nmaxn-max

因爲質數的個數大約爲nlogn\frac{n}{\log n}個,枚舉倍數的複雜度大約也是logn\log n的,所以總複雜度O(n)O(n)

代碼
#include<bits/stdc++.h>
using namespace std;
const int M=2e7+5,N=3e5+5;
int cot[M],val[N],n,g,mx,ans;
bool vis[M];
void in(){scanf("%d",&n);}
void ac()
{
	for(int i=1;i<=n;++i)scanf("%d",&val[i]),g=__gcd(g,val[i]);
	for(int i=1;i<=n;++i)++cot[mx=max(val[i]/g,mx),val[i]/g];
	for(int i=2,j,s;i<=mx;++i,ans=max(ans,s))
	if(!vis[i])for(j=i,s=0;j<=mx;j+=i)s+=cot[j],vis[j]=1;
	printf("%d",ans?n-ans:-1);
}
int main(){in();ac();}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章