結果

問題 No.843 Triple Primes
ユーザー けーむけーむ
提出日時 2020-05-11 17:19:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 464 ms / 2,000 ms
コード長 1,492 bytes
コンパイル時間 1,715 ms
コンパイル使用メモリ 175,320 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2023-09-25 18:27:34
合計ジャッジ時間 13,929 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,376 KB
testcase_01 AC 464 ms
5,080 KB
testcase_02 AC 11 ms
4,380 KB
testcase_03 AC 10 ms
4,376 KB
testcase_04 AC 10 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 11 ms
4,376 KB
testcase_07 AC 348 ms
5,020 KB
testcase_08 AC 386 ms
4,876 KB
testcase_09 AC 449 ms
5,108 KB
testcase_10 AC 363 ms
4,988 KB
testcase_11 AC 409 ms
4,940 KB
testcase_12 AC 444 ms
5,084 KB
testcase_13 AC 430 ms
4,828 KB
testcase_14 AC 430 ms
5,144 KB
testcase_15 AC 349 ms
4,872 KB
testcase_16 AC 366 ms
4,888 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 9 ms
4,384 KB
testcase_20 AC 76 ms
4,376 KB
testcase_21 AC 35 ms
4,376 KB
testcase_22 AC 192 ms
4,412 KB
testcase_23 AC 220 ms
4,380 KB
testcase_24 AC 77 ms
4,376 KB
testcase_25 AC 57 ms
4,380 KB
testcase_26 AC 440 ms
5,092 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 446 ms
5,156 KB
testcase_29 AC 87 ms
4,376 KB
testcase_30 AC 450 ms
5,224 KB
testcase_31 AC 22 ms
4,376 KB
testcase_32 AC 16 ms
4,376 KB
testcase_33 AC 97 ms
4,376 KB
testcase_34 AC 170 ms
4,380 KB
testcase_35 AC 454 ms
5,136 KB
testcase_36 AC 49 ms
4,376 KB
testcase_37 AC 317 ms
4,568 KB
testcase_38 AC 244 ms
4,488 KB
testcase_39 AC 442 ms
5,248 KB
testcase_40 AC 9 ms
4,376 KB
testcase_41 AC 10 ms
4,380 KB
testcase_42 AC 379 ms
4,932 KB
testcase_43 AC 416 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

vector<bool> eratosthenes;

void create_eratosthenes(int n) {
    eratosthenes.resize(n + 1, true);
    eratosthenes[0] = eratosthenes[1] = false;
    
    for(int i = 2; (i * i) <= n; i++) {
        if (!eratosthenes[i]) {
            continue;
        }
        
        for (int ii = 2; (i * ii) <= n; ii++) {
            eratosthenes[i * ii] = false;
        }
    }
}

bool is_prime(int n) {
    return eratosthenes[n];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    create_eratosthenes(1e6);
    set<ll> prime, exp;
    reps(i, 1, 1e6 + 1) {
        if (!is_prime(i)) continue;
        if (i <= n) prime.insert(i);
        if ((i * i) <= (2 * n)) exp.insert(i);
    }
    ll ans = 0;
    for(auto r : exp) {
        for(auto p : prime) {
            ll q = r * r - p;
            if (prime.count(q) > 0) {
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0