分解

wc087 蹦蹦炸弹 2024-11-30 10:57:35 16

#include #include

int countDistinctPrimeFactors(int n) { std::set primeFactors;

for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
        primeFactors.insert(i);
        n /= i;
    }
}

if (n > 1) {
    primeFactors.insert(n);
}

return primeFactors.size();

}

int main() { int q; std::cin >> q;

while (q--) {
    int number;
    std::cin >> number;

    int distinctPrimeFactorsCount = countDistinctPrimeFactors(number);
    std::cout << distinctPrimeFactorsCount << std::endl;
}

return 0;

}

{{ vote && vote.total.up }}

共 1 条回复

wc087 蹦蹦炸弹

#include #include

int countDistinctPrimeFactors(int n) { std::set primeFactors;

for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
        primeFactors.insert(i);
        n /= i;
    }
}

if (n > 1) {
    primeFactors.insert(n);
}

return primeFactors.size();

}

int main() { int q; std::cin >> q;

while (q--) {
    int number;
    std::cin >> number;

    int distinctPrimeFactorsCount = countDistinctPrimeFactors(number);
    std::cout << distinctPrimeFactorsCount << std::endl;
}

return 0;

}