C - Enlarge GCD

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

The second line contains n 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 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 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,因为a[i]的范围是很大的

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;
//using namespace __gnu_cxx;

#define gt(x) x = read()
//#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 2e7 + 10;
const int M = 3 * N;
const int mod = 998244353;
const int PP = 131;
const int inf = 0x3f3f3f3f;
//const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int primes[N], tot;
int cnt[N];
int a[500000];
bool st[N];
int n;

void get_primes(int n){
	for (int i = 2; i <= n; i ++){
		if (!st[i])   primes[tot ++] = i;
		for (int j = 0; primes[j] * i <= n; j ++){
			st[primes[j] * i] = true;
			if (i % primes[j] == 0)    break;
		}
	}
}

int gcd(int a, int b){
	return b ? gcd(b, a % b) : a;
}

signed main(){
	ios;
	cin >> n;
	get_primes(N);
	for (int i = 1; i <= n; i ++){
		cin >> a[i];
	}
	
	int Gcd = a[1];
	for (int i = 2; i <= n; i ++){
		Gcd = gcd(a[i], Gcd);
	}
	
	for (int i = 1; i <= n; i ++){
         a[i] /= Gcd;
		for (int j = 0; primes[j] * primes[j] <= a[i]; j ++){
			if (a[i] % primes[j] == 0){
				cnt[primes[j]] ++;
			}
			while(a[i] % primes[j] == 0)   a[i] /= primes[j];
		}
		if (a[i] != 1)    cnt[a[i]] ++;
	}
	
	int ans = n;
	for (int i = 2; i < N; i ++){
		ans = min(ans, n - cnt[i]);
	}
	
	if (ans == n)   cout << "-1" << endl;
	else cout << ans << endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值