Codeforce #511(Div 2) C. Enlarge GCD(GCD+ 사유)

3203 단어 수론사유
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가 원래보다 크다는 것을 보증하는 상황에서 최소한 몇 개의 요소를 삭제합니까?크면 출력 -1.
문제풀이: 각 수에 대해 질인자를 분해하면 수량이 가장 많은 질인자를 포함하지 않는 수를 모두 삭제하고 나머지 수의 GCD는 원래의 GCD보다 크며 가장 적은 요소를 삭제하는 것을 만족시킨다.
#include
using namespace std;
typedef long long ll;
const int maxn=1.5e7+7;

int a[maxn],prime[maxn],cot,cnt[maxn];
bool vis[maxn];

void primeall()
{
    memset(vis,true,sizeof(vis));
    for(int i=2;imaxn-7) break;
            vis[i*prime[j]]=false;
            if(i%prime[j]==0) break;
        }
    }
}

int main()
{
    primeall();
    int n;
    scanf("%d",&n);

    int gcd=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        gcd=__gcd(gcd,a[i]);
    }

    for(int i=1;i<=n;i++)
    {
        a[i]/=gcd;
        for(int j=1;prime[j]*prime[j]<=a[i];j++)
        {
            int p=prime[j];
            if(a[i]%p==0) cnt[p]++;
            while(a[i]%p==0) a[i]/=p;
        }
        if(a[i]>1) cnt[a[i]]++;
    }

    int ans=n;
    for(int i=2;i

좋은 웹페이지 즐겨찾기