結果

問題 No.732 3PrimeCounting
コンテスト
ユーザー nebukuro09
提出日時 2018-09-07 23:07:32
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 492 ms / 3,000 ms
コード長 1,252 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 613 ms
コンパイル使用メモリ 100,656 KB
実行使用メモリ 9,880 KB
最終ジャッジ日時 2026-03-05 20:59:54
合計ジャッジ時間 6,083 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;

    int[] P;
    auto is_P = new bool[](3*N+1);
    fill(is_P, true);
    is_P[0] = is_P[1] = false;

    foreach (i; 2..3*N+1) {
        if (!is_P[i]) continue;
        P ~= i;
        for (int j = i + i; j < 3*N+1; j += i) is_P[j] = false;
    }

    int M = P.length.to!int;
    auto P1 = P.filter!(p => p <= N && p % 3 == 1).array;
    auto P2 = P.filter!(p => p <= N && p % 3 == 2 && p != 2).array;
    auto A = new long[](3*N+1);
    auto B = new long[](3*N+1);
    foreach (i; 0..P1.length) foreach (j; i+1..P1.length) A[P1[i]+P1[j]] += 1;
    foreach (i; 0..P2.length) foreach (j; i+1..P2.length) B[P2[i]+P2[j]] += 1;

    long ans = 0;
    foreach (p; P) if (p >= 3) ans += A[p - 3] + B[p - 3];
    foreach (p1; P1) foreach (p; P) if (p - p1 > 0) ans += B[p - p1]; //, writeln(p, " ", p1, " ", B[p - p1]);
    foreach (p2; P2) foreach (p; P) if (p - p2 > 0) ans += A[p - p2]; //, writeln(p, " ", p2, " ", A[p - p2]);
    ans.writeln;
}
0