Codeforces Round #511 (Div. 2) Enlarge GCD 分解质因数
Mr. F has 𝑛n positive integers, 𝑎1,𝑎2,…,𝑎𝑛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.
The first line contains an integer 𝑛n (2≤𝑛≤3⋅1052≤n≤3⋅105) — the number of integers Mr. F has.
The second line contains 𝑛n integers, 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1.5⋅1071≤ai≤1.5⋅107).
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).
3
1 2 4
1
4
6 9 15 30
2
3
1 1 1
-1
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个数,在这n个数中删除k个,使得他们的GCD比之前的大,问最少要删除几个。
分析:
对每一个数分解质因子,找到出现次数最多的质因子X,删掉不包含质因子X的数,剩余数的GCD一定比之前的大(因为都有公共因子X)。
/// author :Kissheart /// #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<vector> #include<stdlib.h> #include<math.h> #include<queue> #include<deque> #include<ctype.h> #include<map> #include<set> #include<stack> #include<string> #include<algorithm> #define gcd __gcd #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define FAST_IO ios::sync_with_stdio(false) const double PI = acos(-1.0); const double eps = 1e-6; const int MAX=3e5+10; const int mod=1e9+7; typedef long long ll; using namespace std; inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t);b>>=1;t=(t*t);}return r;} inline ll inv1(ll b){return qpow(b,mod-2);} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;} inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;} const ll maxn=1.5e7+7; int n,a[MAX],GCD; int vis[maxn],prime[maxn],cur; int s[maxn]; void GetPrime() { memset(vis,1,sizeof(vis)); for(int i=2;i<maxn;i++) { if(vis[i]) prime[++cur]=i; for(int j=1;j<=cur;j++) { if(i*prime[j]>maxn) break; vis[i*prime[j]]=0; if(i%prime[j]==0) break; } } } int main() { GetPrime(); scanf("%d",&n); for(ll i=1;i<=n;i++) scanf("%d",&a[i]); GCD=a[1]; for(int i=2;i<=n;i++) GCD=gcd(GCD,a[i]); int ans=0; for(int i=1;i<=n;i++) { int sum=0,p; a[i]/=GCD; for(int j=1;j<=cur && prime[j]*prime[j]<=a[i];j++) { p=prime[j]; if(a[i]%p==0) s[p]++; while(a[i]%p==0) a[i]/=p; } if(a[i]!=1) s[a[i]]++; } for(int i=2;i<maxn;i++) ans=max(ans,s[i]); printf("%d\n",ans==0?-1:n-ans); return 0; }

浙公网安备 33010602011771号