cf 1034A - Enlarge GCD

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值