結果

問題 No.843 Triple Primes
ユーザー tottoripapertottoripaper
提出日時 2019-07-03 23:06:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 1,129 bytes
コンパイル時間 1,528 ms
コンパイル使用メモリ 170,760 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 17:58:22
合計ジャッジ時間 6,260 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 176 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 132 ms
4,348 KB
testcase_08 AC 146 ms
4,348 KB
testcase_09 AC 175 ms
4,348 KB
testcase_10 AC 138 ms
4,348 KB
testcase_11 AC 157 ms
4,348 KB
testcase_12 AC 169 ms
4,348 KB
testcase_13 AC 163 ms
4,348 KB
testcase_14 AC 163 ms
4,348 KB
testcase_15 AC 134 ms
4,348 KB
testcase_16 AC 136 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 31 ms
4,348 KB
testcase_21 AC 15 ms
4,348 KB
testcase_22 AC 80 ms
4,348 KB
testcase_23 AC 84 ms
4,348 KB
testcase_24 AC 33 ms
4,348 KB
testcase_25 AC 24 ms
4,348 KB
testcase_26 AC 173 ms
4,348 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 165 ms
4,348 KB
testcase_29 AC 36 ms
4,348 KB
testcase_30 AC 169 ms
4,348 KB
testcase_31 AC 8 ms
4,348 KB
testcase_32 AC 5 ms
4,348 KB
testcase_33 AC 41 ms
4,348 KB
testcase_34 AC 72 ms
4,348 KB
testcase_35 AC 174 ms
4,348 KB
testcase_36 AC 21 ms
4,348 KB
testcase_37 AC 124 ms
4,348 KB
testcase_38 AC 99 ms
4,348 KB
testcase_39 AC 166 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 142 ms
4,348 KB
testcase_43 AC 156 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

int N;
std::vector<int> primes;

bool isPrime(int n){
    if(n == 1){
        return false;
    }else if(n == 2){
        return true;
    }

    if(n % 2 == 0){
        return false;
    }

    for(int i=3;i*i<=n;i+=2){
        if(n % i == 0){
            return false;
        }
    }

    return true;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N;

    if(N >= 2){
        primes.emplace_back(2);
    }

    for(int i=3;i<=N;i+=2){
        if(isPrime(i)){
            primes.emplace_back(i);
        }
    }

    int cnt = 0;

    for(int r : primes){
        if(r > 2 * N / r){
            break;
        }

        for(int p : primes){
            int q = r * r - p;

            if(q > 0 && std::binary_search(primes.begin(), primes.end(), q)){
                cnt += 1;
            }
        }
    }

    std::cout << cnt << std::endl;
}
0