#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_primes" #include <iostream> #include <vector> #include "Mylib/IO/join.cpp" #include "Mylib/Number/Prime/sieve_atkin.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, A, B; std::cin >> N >> A >> B; const auto is_prime = hl::atkin_sieve(N); int pi = 0; std::vector<int> ans; for (int i = 2; i <= N; ++i) { if (is_prime(i)) { if ((pi - B) % A == 0) { ans.push_back(i); } ++pi; } } std::cout << pi << " " << ans.size() << "\n" << hl::join(ans.begin(), ans.end()) << "\n"; return 0; }
#line 1 "test/yosupo-judge/enumerate_primes/main.atkin.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/enumerate_primes" #include <iostream> #include <vector> #line 3 "Mylib/IO/join.cpp" #include <sstream> #include <string> namespace haar_lib { template <typename Iter> std::string join(Iter first, Iter last, std::string delim = " ") { std::stringstream s; for (auto it = first; it != last; ++it) { if (it != first) s << delim; s << *it; } return s.str(); } } // namespace haar_lib #line 2 "Mylib/Number/Prime/sieve_atkin.cpp" #include <cstdint> #line 4 "Mylib/Number/Prime/sieve_atkin.cpp" namespace haar_lib { class atkin_sieve { std::vector<bool> is_prime_; public: atkin_sieve() {} atkin_sieve(int MAX) : is_prime_(MAX + 1) { for (int64_t i = 1; i * i <= MAX; ++i) { for (int64_t j = 1; j * j <= MAX; ++j) { { auto n = 4LL * i * i + j * j; if (n <= MAX and (n % 12 == 1 or n % 12 == 5)) { is_prime_[n] = not is_prime_[n]; } } { auto n = 3LL * i * i + j * j; if (n <= MAX and n % 12 == 7) { is_prime_[n] = not is_prime_[n]; } } if (i > j) { auto n = 3LL * i * i - j * j; if (n <= MAX and n % 12 == 11) { is_prime_[n] = not is_prime_[n]; } } } } for (int64_t i = 5; i * i <= MAX; ++i) { if (is_prime_[i]) { for (int64_t k = i * i, j = k; j <= MAX; j += k) { is_prime_[j] = false; } } } is_prime_[2] = is_prime_[3] = true; } bool operator()(int i) const { return is_prime_[i]; } }; } // namespace haar_lib #line 7 "test/yosupo-judge/enumerate_primes/main.atkin.test.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, A, B; std::cin >> N >> A >> B; const auto is_prime = hl::atkin_sieve(N); int pi = 0; std::vector<int> ans; for (int i = 2; i <= N; ++i) { if (is_prime(i)) { if ((pi - B) % A == 0) { ans.push_back(i); } ++pi; } } std::cout << pi << " " << ans.size() << "\n" << hl::join(ans.begin(), ans.end()) << "\n"; return 0; }