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

The second line contains nn 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個數的最大公約數。

思路:先找出n個數的最大 公約數,然後把n個數 依次除去 他們的最大公約數得到全新的N個數。這N個數的最大公約數爲1.

          然後對這N個全新的數【唯一分解定理】進行 分解。 統計分解成每個素數的 個數。最多的那個 就是去除m個數後,剩餘

         數 的最大公約數。m = n(n個數) - num[數量最多的素數];(num記錄的是每個素數的個數)

 代碼:

這裏 運用了一種比素數打表更高效 的素數篩:https://blog.csdn.net/weixin_45177251/article/details/106130112

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[300010];
int cnt[15000010];
bool book[15000010];
int su[15000010];
int r;
int gcd(int a,int b)//歐幾里得求最大公約數
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void prime()//求1~15000000的素數
{
    memset(book,0,sizeof(book));
    book[0]=book[1]=1;
    for(int i=2; i<15000000; i++)
    {
        if(book[i]==0)
        {
            su[r++]=i;
        }
        for(int j=0; j<r&&su[j]*i<15000000; j++)
        {
            book[i*su[j]]=1;
            if(i%su[j]==0)
                break;
        }
    }
}
int main()
{
    prime();
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        memset(cnt,0,sizeof(cnt));
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        int g=a[0];
        for(int i=1; i<n; i++)//求n個數的最大公約數g
            g=gcd(g,a[i]);
        for(int i=0; i<n; i++)
        {
            a[i]/=g;//每個數都除以最大公約數
            for(int j=0; su[j]*su[j]<=a[i]; j++)//----
            {
                if(a[i]%su[j]==0)
                    cnt[su[j]]++;
                while(a[i]%su[j]==0)
                    a[i]/=su[j];
            }
            if(a[i]>1)
                cnt[a[i]]++;
        }
        int ans=n;
        for(int i=2; i<15000000; i++)
            ans=min(ans,n-cnt[i]);
        if(ans==n)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章