codeforces 1047C. Enlarge GCD(数学)

C. Enlarge GCD

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

The second line contains nn integers, a1,a2,…,ana1,a2,…,an (1≤ai≤1.5⋅1071≤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

Copy

3
1 2 4

output

Copy

1

input

Copy

4
6 9 15 30

output

Copy

2

input

Copy

3
1 1 1

output

Copy

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

 

一、原题地址

点我传送

 

二、大致题意

给出n个数,记它们的gcd为g,现在询问最少去掉多少个数可以使剩下的数的gcd大于g。

 

三、思路

先求出所有数的gcd记作gg,然后把原数组中所有的数除去这个因数gg。然后再把除去后的数组中所有的数分解质因数,用一个cnt[ ]来记录一个因数在这个除去gg的数组中共出现了几次。现在我们的目的是想让gcd增大,那么其实就是需要删掉那些在除去gg后数组中没有相同因数的数字。暴力竟然没有超时,CF机器真快。

 

四、代码

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }



int n;
int a[300005];
const int MAXN = 2e7;
int Mark[MAXN];
int prime[MAXN],cnt[MAXN];
int Prime() {
	int tot = 0;
	memset(Mark, 0, sizeof(Mark));
	Mark[0] = 1; Mark[1] = 1;
	for (int i = 2; i < MAXN; i++)
	{
		if (Mark[i] == 0) {
			prime[tot++] = i;
		}
		for (int j = 0; j < tot && prime[j] * i < MAXN; j++)
		{
			Mark[i * prime[j]] = 1;
			if (i % prime[j] == 0) {
				break;
			}
		}
	}
	return tot;
}
int main()
{
	memset(cnt, 0, sizeof(cnt));
	int tot = Prime();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)scanf("%d", &a[i]);
	int gg = a[1];for (int i = 2; i <= n; i++)gg = gcd(gg, a[i]);
	for (int i = 1; i <= n; i++)
	{
		a[i] /= gg;
		for (int j = 0; prime[j] * prime[j] <= a[i]; j++)
		{
			if (a[i] % prime[j] == 0)cnt[prime[j]]++;
			while (a[i] % prime[j] == 0)
				a[i] /= prime[j];
		}
		if (a[i] != 1)cnt[a[i]]++;
	}
	int ans = n;
	for (int i = 2; i < MAXN; i++)
	{
		ans = min(ans, n - cnt[i]);
	}
	if (ans == n)printf("-1\n");
	else
		printf("%d\n", ans);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值