algorithm-dev

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub today2098/algorithm-dev

:heavy_check_mark: verify/aoj-ALDS1_1_C-is_prime.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/1/ALDS1_1_C"

#include <iostream>

#include "../algorithm/Math/NumberTheory/is_prime.hpp"

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

    int ans = 0;
    for(int i = 0; i < n; ++i) {
        int a;
        std::cin >> a;

        if(algorithm::is_prime(a)) ans++;
    }

    std::cout << ans << std::endl;
}
#line 1 "verify/aoj-ALDS1_1_C-is_prime.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/1/ALDS1_1_C"

#include <iostream>

#line 1 "algorithm/Math/NumberTheory/is_prime.hpp"



#include <cassert>

namespace algorithm {

// 素数判定.O(√N).
template <typename Type>
constexpr bool is_prime(Type n) {
    assert(n >= 0);
    if(n < 2) return false;
    if(n == 2) return true;
    if(n % 2 == 0) return false;
    for(Type p = 3; p * p <= n; p += 2) {
        if(n % p == 0) return false;
    }
    return true;
}

}  // namespace algorithm


#line 6 "verify/aoj-ALDS1_1_C-is_prime.test.cpp"

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

    int ans = 0;
    for(int i = 0; i < n; ++i) {
        int a;
        std::cin >> a;

        if(algorithm::is_prime(a)) ans++;
    }

    std::cout << ans << std::endl;
}
Back to top page