C. Enlarge GCD(贪心)

    xiaoxiao2022-07-14  166

    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 .

     

    思路:先从小到大排序,取出第一个和最后一个gcd求一下,在依次gcd。

    #include<iostream> #include<iomanip> //#include<bits/stdc++.h> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #include<cstring> #include<vector> #include<sstream> #include<queue> #include<set> #define PI 3.14159265358979 #define LL long long using namespace std; int gcd(int x,int y)//辗转相除法 { if(x<y) swap(x,y); int r=x%y; while(r) { x=y; y=r; r=x%y; } return y; } int ans[1000000]; int main() { ios::sync_with_stdio(false); cin.tie(0); int T,n,m; cin>>T; for(int i=1;i<=T;++i) cin>>ans[i]; sort(ans+1,ans+T+1); int M=ans[T]; int k,h;int sum=0; k=gcd(ans[1],M); for(int i=2;i<T;++i)//保留最后一个,所以到T-1 { h=gcd(ans[i],M); if(h!=k){ sum++; if(h>k) k=h; } } if(sum==0) cout<<-1<<endl; else cout<<sum<<endl; }

     

     

    最新回复(0)