cf 1034A - Enlarge GCD
题目链接
思路,求出全局gcd,将所有元素除gcd, 再求出每个质因子在所有的数中出现的次数,取出现最多次的,记为x,n - x为答案
(又读错题了,辣鸡,只要能让原本的gcd变大就好啊,and 没想到这么暴力,但是确实这么暴力,如果是a的范围是1e8就无了,怪不得是1.5e7)
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+10;
const int M = 15000010;
int p[N], cnt;
bool vis[N];
int cntp[M];
int a[N], n;
void sieve(int n) {
vis[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) p[cnt++] = i;
for(int j = 0; j < cnt && i <= n / p[j]; j++) {
vis[i * p[j]] = 1;
if(i % p[j] == 0) break;
}
}
}
int exgcd(int a, int b, int& x, int& y) {
if(!b) {
x = 1, y = 0;
return a;
}
int res = exgcd(b, a%b, y, x);
y -= a / b * x;
return res;
}
int main() {
sieve(N-1);
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
int x, y;
int gcd = exgcd(a[2], a[1], x, y);
for(int i = 3; i <= n; i++)
gcd = exgcd(gcd, a[i], x, y);
for(int i = 1; i <= n; i++) a[i] /= gcd;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < cnt && p[j] <= a[i] / p[j]; j++) {
if(a[i] % p[j] == 0) {
cntp[p[j]]++;
while(a[i] % p[j] == 0) a[i] /= p[j];
}
}
if(a[i] > 1) cntp[a[i]]++;
}
int ans = 0;
for(int i = 0; i < M; i++) {
ans = max(ans, cntp[i]);
}
printf("%d\n", ans? n - ans : -1);
return 0;
}