abc170D 不能整除其他元素的个数

29 阅读1分钟

题面:给定数组A[n],问A中存在多少个下标x,满足A[x]不能整除数组中所有其他元素?

范围:1 <= n <= 2E5; 1 <= A[i] <= 1E6

分析:先计数,然后用类似质数筛的方式处理出整除关系,遍历一次统计结果。

#include <bits/stdc++.h>
const int N = 1000000;
int cnt[N+1], nok[N+1];
void solve() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        int a;
        std::cin >> a;
        cnt[a] += 1;
    }
    for (int i = 1; i <= N; i++) if (cnt[i]) {
        for (int j = 2 * i; j <= N; j += i) {
            nok[j] = 1;
        }
    }
    int ans = 0;
    for (int i = 1; i <= N; i++) {
        if (cnt[i] == 1 && nok[i] == 0) {
            ans += 1;
        }
    }
    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

标签:数论