原题链接:http://codeforces.com/contest/1047/problem/C
Enlarge GCD
Mr. F has n n n positive integers, a 1 , a 2 , ⋯   , a n a_1,a_2,\cdots,a_n 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 ⋅ 1 0 5 ) n (2≤n≤3⋅10^5) n(2≤n≤3⋅105) — the number of integers Mr. F has.
The second line contains n n n integers, a 1 , a 2 , ⋯   , a n ( 1 ≤ a i ≤ 1.5 ⋅ 1 0 7 ) a_1,a_2,\cdots,a_n (1≤a_i≤1.5⋅10^7) 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 1 1 in the beginning. You can remove 1 1 1 so that the greatest common divisor is enlarged to 2 2 2. The answer is 1 1 1.
In the second example, the greatest common divisor is 3 3 3 in the beginning. You can remove 6 6 6 and 9 9 9 so that the greatest common divisor is enlarged to 15 15 15. There is no solution which removes only one integer. So the answer is 2 2 2.
In the third example, there is no solution to enlarge the greatest common divisor. So the answer is − 1 −1 −1.
题解
先把所有数除个 g c d gcd gcd,再开个桶记录除 g c d gcd gcd后的数,枚举一下所有质数,统计含有该质数的数的个数取 m a x max max,最后的答案就是 n − m a x n-max n−max。
因为质数的个数大约为 n log n \frac{n}{\log n} lognn个,枚举倍数的复杂度大约也是 log n \log n logn的,所以总复杂度 O ( 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();}