結果

問題 No.843 Triple Primes
ユーザー TiramisterTiramister
提出日時 2019-06-28 21:32:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 4,113 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 120,292 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 17:30:27
合計ジャッジ時間 2,461 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,348 KB
testcase_01 AC 5 ms
4,348 KB
testcase_02 AC 5 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 5 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 5 ms
4,348 KB
testcase_23 AC 5 ms
4,348 KB
testcase_24 AC 5 ms
4,348 KB
testcase_25 AC 5 ms
4,348 KB
testcase_26 AC 5 ms
4,348 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 5 ms
4,348 KB
testcase_29 AC 5 ms
4,348 KB
testcase_30 AC 5 ms
4,348 KB
testcase_31 AC 5 ms
4,348 KB
testcase_32 AC 5 ms
4,348 KB
testcase_33 AC 5 ms
4,348 KB
testcase_34 AC 5 ms
4,348 KB
testcase_35 AC 5 ms
4,348 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 5 ms
4,348 KB
testcase_38 AC 5 ms
4,348 KB
testcase_39 AC 5 ms
4,348 KB
testcase_40 AC 5 ms
4,348 KB
testcase_41 AC 5 ms
4,348 KB
testcase_42 AC 5 ms
4,348 KB
testcase_43 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>

// type alias
using lint = long long;
using ldouble = long double;
template <class T>
using greater_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;


/* ----- class ----- */

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Edges = std::vector<Edge<Cost>>;

template <class Cost = int>
using Graph = std::vector<std::vector<Edge<Cost>>>;

class Prime {
    using lint = long long;

public:
    int MAX_V;
    std::vector<int> primes;
    std::vector<bool> isp;

    explicit Prime(int N) : MAX_V(N) {
        isp.assign(MAX_V + 1, true);
        isp[0] = isp[1] = false;
        for (int i = 2; i * i <= MAX_V; ++i) {
            if (isp[i]) {
                for (int j = i; i * j <= MAX_V; ++j) {
                    isp[i * j] = false;
                }
            }
        }
        for (int p = 2; p <= MAX_V; ++p) {
            if (isp[p]) primes.push_back(p);
        }
    }

    bool isprime(lint N) const {
        if (N <= MAX_V) return isp[N];
        for (lint p : primes) {
            if (p * p > N) break;
            if (N % p == 0) return false;
        }
        return true;
    }

    std::vector<std::pair<lint, int>> factorization(lint N) const {
        std::vector<std::pair<lint, int>> ret;
        for (lint p : primes) {
            if (p * p > N) break;
            if (N % p != 0) continue;
            int cnt = 0;
            while (N % p == 0) {
                N /= p;
                ++cnt;
            }
            ret.emplace_back(p, cnt);
        }
        if (N > 1) ret.emplace_back(N, 1);
        return ret;
    }
};


/* ----- debug ----- */

#if __has_include("../setting/source/debug.hpp")
#include "../setting/source/debug.hpp"
#endif


/* ----- short functions ----- */

template <class T>
inline T sq(T a) { return a * a; }

template <class T>
inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T>
T gcd(T a, T b) {
    while (b > 0) {
        a %= b;
        std::swap(a, b);
    }
    return a;
}

template <class T, class U>
T ipow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

// 0-indexed
template <class T, class U>
inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U>
inline T mask(T a, U k) { return a & ((1 << k) - 1); }

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < v.size(); ++i) rev[v[i]] = i;
    return rev;
}

template <class T>
T Vec(T v) { return v; }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}


/* ----- constants ----- */

// const int INF = std::numeric_limits<int>::max() / 3;
// const lint INF = std::numeric_limits<lint>::max() / 3;
// const ldouble PI = acos(-1);
// const ldouble EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));


using namespace std;

const Prime P(500010);

int main() {
    int N;
    cin >> N;

    int ans = 0;
    for (lint r : P.primes) {
        if (r > N || sq(r) - 2 > N) break;
        if (r == 2) {
            ++ans;
        } else if (P.isprime(sq(r) - 2)) {
            ans += 2;
        }
    }

    cout << ans << endl;
    return 0;
}
0