题目传送门
题目如下
描述
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≤3e5) — the number of integers Mr. F has.
The second line contains nn integers, a1,a2,…,ana1,a2,…,an (1≤ai≤1.5e7).
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).
input
3
1 2 4
output
1
input
4
6 9 15 30
output
2
input
3
1 1 1
output
-1
***题目大意:给你n个数,去掉尽量少的数使这n个数的最大公因数变大,输出这尽量少的数的个数 ***
代码如下
/*题解
分解质因子,首先算出n个数的最大公因数g
用计数数组cnt来记录(每个数除g)有多少个
然后就是找一个质数因子,让尽量多的数包含它(代码第19-22行,第23行记录有多少个数包含这个质因子)
求得最大值情况就是n个÷g后的数中具有某个质因数最多的情况,
如果为0,则都是1,就输出-1,否则输出n-ans
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
const int maxm=1.5e7+5;
int n,a[maxn],g=0,ma=0,cnt[maxm],ans=0,prime[maxm];
void init() {
cin>>n;
for(int i=0; i<n; i++) cin>>a[i],g=__gcd(g,a[i]);
for(int i=0; i<n; i++) cnt[a[i]/g]++,ma=max(ma,a[i]/g);///ma记录整个数组中的最大值
for(int i=2; i<=ma; i++) if(!prime[i]) {///素数筛(紫书312页)
int res=0;
for(int j=i; j<=ma; j+=i) {///遍历质数i的所有倍数,记录数组a中以质数i为质因数的数有多少个
prime[j]=1;
res+=cnt[j];
}
ans=max(ans,res);///记录数组中所有数中最多的数具有同一个质因子的情况,并更新这个具有同一质因子的数的个数
}
}
int main() {
ios::sync_with_stdio(0),cin.tie(0);
init();
cout<<((ans)?(n-ans):-1)<<endl;
return 0;
}