結果

問題 No.843 Triple Primes
ユーザー dnishdnish
提出日時 2019-06-28 21:56:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 1,569 ms
コンパイル使用メモリ 170,356 KB
実行使用メモリ 4,976 KB
最終ジャッジ日時 2023-10-19 17:42:41
合計ジャッジ時間 3,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,348 KB
testcase_01 AC 10 ms
4,976 KB
testcase_02 AC 5 ms
4,348 KB
testcase_03 AC 6 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 9 ms
4,876 KB
testcase_08 AC 9 ms
4,916 KB
testcase_09 AC 10 ms
4,972 KB
testcase_10 AC 9 ms
4,892 KB
testcase_11 AC 9 ms
4,936 KB
testcase_12 AC 10 ms
4,960 KB
testcase_13 AC 10 ms
4,948 KB
testcase_14 AC 9 ms
4,948 KB
testcase_15 AC 9 ms
4,884 KB
testcase_16 AC 9 ms
4,888 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 6 ms
4,348 KB
testcase_21 AC 6 ms
4,368 KB
testcase_22 AC 7 ms
4,496 KB
testcase_23 AC 7 ms
4,508 KB
testcase_24 AC 7 ms
4,348 KB
testcase_25 AC 6 ms
4,408 KB
testcase_26 AC 9 ms
4,972 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 10 ms
4,952 KB
testcase_29 AC 7 ms
4,348 KB
testcase_30 AC 9 ms
4,964 KB
testcase_31 AC 5 ms
4,348 KB
testcase_32 AC 6 ms
4,348 KB
testcase_33 AC 7 ms
4,348 KB
testcase_34 AC 7 ms
4,472 KB
testcase_35 AC 10 ms
4,972 KB
testcase_36 AC 6 ms
4,396 KB
testcase_37 AC 8 ms
4,604 KB
testcase_38 AC 8 ms
4,544 KB
testcase_39 AC 10 ms
4,956 KB
testcase_40 AC 5 ms
4,348 KB
testcase_41 AC 5 ms
4,348 KB
testcase_42 AC 9 ms
4,904 KB
testcase_43 AC 9 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i, n, N) for(ll i = n; i < (N); i++)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) <<" "<< (b) << endl
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
ll inf = 1e18;
vector<ll> hurui_v(ll n) {
    vector<ll> v;
    bool *isPrime_v;
    isPrime_v = new bool[n + 1]; // 処理用のメモリ確保

    // 初期化
    isPrime_v[0] = isPrime_v[1] = false;
    for (ll i = 2; i < n + 1; i++) isPrime_v[i] = true;

    // isPrime[0~n]で素数のものだけtrueにする
    for (ll i = 2; i*i <= n; i++) {
        for (ll j = i * 2; j < n + 1; j += i) {
            isPrime_v[j] = false;
        }
    }

    // 素数のみ追加
    for (ll i = 0; i < n + 1; i++) {
        if (isPrime_v[i]) v.push_back(i);
    }

    delete[] isPrime_v; //メモリ解放
    return v;
}
bool isPrime[500050];
void hurui(ll n) {
    // 初期化
    isPrime[0] = isPrime[1] = false;
    for (ll i = 2; i < n + 1; i++) isPrime[i] = true;

    for (ll i = 2; i*i <= n; i++) {  // iがsqrt(n)となるまで繰り返す
        for (ll j = i * 2; j < n + 1; j += i) {
            isPrime[j] = false;       // 素数でない
        }
    }
    return;
}
int main() {
   ll N, ans;
   ans = 0;
   cin >> N;
   hurui(500010);
    vector<ll> vp = hurui_v(N);
    for(ll i=0; i < vp.size(); i++){
        for(ll k=i; (vp[k]*vp[k] <= 2*N) && k<vp.size(); k++){
            ll j = (vp[k]*vp[k]) - vp[i];
            if(j > N) break;
            if(isPrime[j]){
                ans++;
                if(vp[i]!=j) ans++;
            }
        }
    }
    p(ans);
    return 0;
}
0