結果
問題 |
No.843 Triple Primes
|
ユーザー |
![]() |
提出日時 | 2021-03-26 10:24:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 16 ms / 2,000 ms |
コード長 | 3,645 bytes |
コンパイル時間 | 732 ms |
コンパイル使用メモリ | 78,096 KB |
最終ジャッジ日時 | 2025-01-19 21:46:33 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 42 |
ソースコード
#line 1 "prime_util.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/843" #line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/int_alias.cpp" #include <cstdint> #include <cstddef> using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using isize = std::ptrdiff_t; using usize = std::size_t; #line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/rep.cpp" #include <algorithm> class rep { struct Iter { usize itr; constexpr Iter(const usize pos) noexcept: itr(pos) { } constexpr void operator ++ () noexcept { ++itr; } constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; } constexpr usize operator * () const noexcept { return itr; } }; const Iter first, last; public: explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { } constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; #line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/bit/ceil_log2.cpp" constexpr u64 ceil_log2(const u64 x) { u64 e = 0; while (((u64) 1 << e) < x) ++e; return e; } #line 4 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/auto_realloc.cpp" #include <utility> #include <vector> template <class F> class AutoRealloc { using R = typename decltype(std::declval<F>().operator()(std::declval<usize>()))::value_type; F func; std::vector<R> data; public: explicit AutoRealloc(F&& func): func(std::forward<F>(func)), data() { } explicit AutoRealloc(F&& func, const usize capacity): func(std::forward<F>(func)) { reserve(capacity); } void reserve(const usize size) { if (data.size() < size) { const usize pow2 = ((usize) 1 << ceil_log2(size)); data = func(pow2); } } R operator [] (const usize i) { reserve(i + 1); return data[i]; } }; #line 7 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/math/prime_util.cpp" #include <numeric> #include <cassert> struct PrimeUtil { static inline auto min_prime = AutoRealloc([](const usize n) { std::vector<usize> ret(n); std::iota(ret.begin(), ret.end(), (usize) 0); std::vector<usize> list; for (const usize i: rep(2, n)) { if (ret[i] == i) list.push_back(i); for (const usize p: list) { if (p * i >= n || p > ret[i]) break; ret[p * i] = p; } } return ret; }); static bool is_prime(const usize n) { if (n <= 1) return false; return min_prime[n] == n; } template <class T> static std::vector<std::pair<T, usize>> factorize(T x) { assert(x > 0); std::vector<std::pair<T, usize>> ret; while (x != 1) { const usize p = min_prime[x]; ret.emplace_back((T) p, 0); while (min_prime[x] == p) { ret.back().second++; x /= p; } } return ret; } }; #line 5 "prime_util.test.cpp" #include <iostream> int main() { u32 N; std::cin >> N; if (N == 1) { std::cout << 0 << '\n'; return 0; } u32 ans = 1; for (const u32 r: rep(3, N + 1)) { if (r * r - 2 > N) break; if (PrimeUtil::is_prime(r) && PrimeUtil::is_prime(r * r - 2)) { ans += 2; } } std::cout << ans << '\n'; return 0; }