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
3 1 2 4Output
1Input
4 6 9 15 30Output
2Input
3 1 1 1Output
-1Note
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 .
题目大意:就是给一组数让求最少要删除几个数它们的共同最大公约数会比原来的大
思路与解析:利用质因数分解原理,求出素数,简化计算的过程。
具体如下:见代码;
#include <iostream> #include <string.h> #include <map> #include <queue> #include <stdio.h> #include <algorithm> using namespace std; #define LL long long LL gcd(LL a, LL b)//求两个数之间最大公约数 { return b ? gcd(b, a%b) : a; } int n; int a[300005]; const int MAXN = 2e7; int book[MAXN]; int prime[MAXN],s[MAXN]; int Prime()//素数筛选 { int tot = 0; memset(book, 0, sizeof(book)); book[0] = 1; book[1] = 1; for (int i = 2; i < MAXN; i++) { if (book[i] == 0) { prime[tot++] = i; } for (int j = 0; j < tot && prime[j] * i < MAXN; j++) { book[i * prime[j]] = 1; if (i % prime[j] == 0) { break; } } } return tot; } int main() { memset(s, 0, sizeof(s)); 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) s[prime[j]]++;//对每个质因数进行记录 while (a[i]%prime[j]==0) a[i]/= prime[j]; } if (a[i]!= 1) s[a[i]]++;//同样记录不为1的因数 } int ans = n; for (int i = 2; i < MAXN; i++)/*遍历得到在除去原来的公约数后他们共同拥有相同因数哪个个数最多,进而得到要除去最少的个数为多少*/ { ans = min(ans, n - s[i]); } if (ans == n) printf("-1\n"); else printf("%d\n", ans); }
Enlarge GCD CodeForces - 1034A
最新推荐文章于 2024-05-17 20:28:51 发布