原題鏈接:http://codeforces.com/contest/1047/problem/C
Enlarge GCD
Mr. F has positive integers, .
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 — the number of integers Mr. F has.
The second line contains integers, .
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 in the beginning. You can remove so that the greatest common divisor is enlarged to . The answer is .
In the second example, the greatest common divisor is in the beginning. You can remove and so that the greatest common divisor is enlarged to . There is no solution which removes only one integer. So the answer is .
In the third example, there is no solution to enlarge the greatest common divisor. So the answer is .
題解
先把所有數除個,再開個桶記錄除後的數,枚舉一下所有質數,統計含有該質數的數的個數取,最後的答案就是。
因爲質數的個數大約爲個,枚舉倍數的複雜度大約也是的,所以總複雜度。
代碼
#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();}