1. 程式人生 > >C. Enlarge GCD Codeforces Round #511 (Div. 2)【數學】

C. Enlarge GCD Codeforces Round #511 (Div. 2)【數學】

ios ssis 進行 原理 als namespace 最大 cor http

題目:

Mr. F has nn 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 nn (2n3?10^5) — the number of integers Mr. F has.

The second line contains nn integers,

a1,a2,,an (1ai1.5?10^7).

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個數會有一個最大公約數G,那麽需要去掉K個數,使余下的N-K個數的最大公約數變大。求最小K。

我們從gcd的原理分析,這N個數都除以gcd後,余下的數的最大公約數無法變大時因為不存在公因子,所以我們需要對這N個數進行分類,分類的標準就是是否還有共同的公因子,然後找出包含的數目最多的類別,假設這個類別有M個數。

那麽N-M就是我們最終的結果。

這裏需要註意的技巧是,在進行類別劃分的時候,我們用的素數打表的原理。

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 3e5+5;
const int MAX = 1.5e7+5;
int A[MAXN], cnt[MAX];
bool visit[MAX];

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

int Max(const int a, const int b)
{
    return a>b?a:b;
}


int main()
{
    int N;
    while(~scanf("%d", &N))
    {
        int G, ans;
        scanf("%d", &A[0]);
        G = A[0];
        for(int i = 1; i < N; i++)
        {
            scanf("%d", &A[i]);
            G = gcd(G, A[i]);
        }

        memset(cnt, 0, sizeof(cnt));
        memset(visit, true, sizeof(visit));

        for(int i = 0; i < N; i++)
            cnt[A[i]/G]++;

        visit[0] = visit[1] = false;
        ans = 0;
        for(int i = 2; i < MAX; i++)
        {
            int res = cnt[i];
            if(visit[i])
            {
                for(int j = 2*i; j < MAX; j+=i)
                {
                    visit[j] = false;
                    res += cnt[j];
                }
            }
            ans = Max(ans, res);
        }
        printf("%d\n", ans==0?-1:N-ans);
    }
    return 0;
}

  

C. Enlarge GCD Codeforces Round #511 (Div. 2)【數學】