Link Search Menu Expand Document
あるまかんライブラリ

:heavy_check_mark: test/AOJ/ALDS1_1_C-Prime-Numbers.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C"

#include <iostream>

#include "../../Math/Number-Theory/is-prime.hpp"

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);

    int N;
    std::cin >> N;

    int ans = 0;
    while (N--) {
        int x;
        std::cin >> x;
        if (isPrime(x)) ++ans;
    }

    std::cout << ans << std::endl;

    return 0;
}
#line 1 "test/AOJ/ALDS1_1_C-Prime-Numbers.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C"

#include <iostream>

#line 2 "Math/Number-Theory/is-prime.hpp"
#include <cstdint>

/**
 * @brief isPrime() (素数判定 $O(\sqrt n)$)
 */
constexpr bool isPrime(int64_t n) {
    if (n == 2) return true;
    if (n <= 1 || n % 2 == 0) return false;
    for (int64_t i = 3; i * i <= n; i += 2) {
        if (n % i == 0) return false;
    }
    return true;
}
#line 6 "test/AOJ/ALDS1_1_C-Prime-Numbers.test.cpp"

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);

    int N;
    std::cin >> N;

    int ans = 0;
    while (N--) {
        int x;
        std::cin >> x;
        if (isPrime(x)) ++ans;
    }

    std::cout << ans << std::endl;

    return 0;
}
Back to top page