cf1627 D. Not Adding(数论)

题意:

数组中的数两两不同,每次操作任选数组中的两个数并把它们的gcd加入数组。问最多能操作多少次。

思路:

设原数组中的最大数为 \(A\),则 \(x\in [1,A]\) 会被加入数组当且仅当 \(x\) 的所有倍数的 gcd 恰为 \(x\)。否则,这个gcd与其他 \(x\) 的倍数取gcd仍等于自己,永远不会得到 \(x\)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, c[N];
signed main()
{
    scanf("%d", &n);
    for(int i = 1, x; i <= n; i++) scanf("%d", &x), c[x]++;

    int ans = 0;
    for(int i = 1; i < N; i++) if(!c[i])
    {
        int g = 0;
        for(int j = i + i; j < N; j += i)
            if(c[j]) g = __gcd(g, j);
        ans += g == i;
    }

    printf("%d", ans);

    return 0;
}

posted @ 2022-01-17 01:42  Bellala  阅读(144)  评论(0)    收藏  举报