結果

問題 No.732 3PrimeCounting
ユーザー te-shte-sh
提出日時 2018-09-20 15:19:53
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2,000 ms / 3,000 ms
コード長 1,408 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 115,704 KB
実行使用メモリ 5,668 KB
最終ジャッジ日時 2023-09-03 20:59:48
合計ジャッジ時間 24,276 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 8 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 6 ms
4,376 KB
testcase_34 AC 6 ms
4,380 KB
testcase_35 AC 4 ms
4,376 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 9 ms
4,376 KB
testcase_49 AC 9 ms
4,380 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 27 ms
4,376 KB
testcase_54 AC 600 ms
5,444 KB
testcase_55 AC 598 ms
4,376 KB
testcase_56 AC 603 ms
4,376 KB
testcase_57 AC 96 ms
4,380 KB
testcase_58 AC 97 ms
4,504 KB
testcase_59 AC 30 ms
4,376 KB
testcase_60 AC 154 ms
4,376 KB
testcase_61 AC 153 ms
4,380 KB
testcase_62 AC 821 ms
4,380 KB
testcase_63 AC 314 ms
4,380 KB
testcase_64 AC 206 ms
4,380 KB
testcase_65 AC 205 ms
4,380 KB
testcase_66 AC 1 ms
4,376 KB
testcase_67 AC 1 ms
4,376 KB
testcase_68 AC 696 ms
5,668 KB
testcase_69 AC 696 ms
4,380 KB
testcase_70 AC 620 ms
4,376 KB
testcase_71 AC 616 ms
4,376 KB
testcase_72 AC 289 ms
4,376 KB
testcase_73 AC 1,420 ms
4,912 KB
testcase_74 AC 1,431 ms
4,900 KB
testcase_75 AC 7 ms
4,380 KB
testcase_76 AC 677 ms
5,136 KB
testcase_77 AC 106 ms
4,376 KB
testcase_78 AC 1,065 ms
4,376 KB
testcase_79 AC 712 ms
4,380 KB
testcase_80 AC 932 ms
4,380 KB
testcase_81 AC 623 ms
4,996 KB
testcase_82 AC 2 ms
4,380 KB
testcase_83 AC 101 ms
4,376 KB
testcase_84 AC 116 ms
4,380 KB
testcase_85 AC 513 ms
4,380 KB
testcase_86 AC 972 ms
4,380 KB
testcase_87 AC 2,000 ms
5,376 KB
testcase_88 AC 1,997 ms
4,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n; readV(n);

  auto p = primes(n*3).assumeSorted;
  auto ca = new int[](n*2), cb = new int[](n*2), ans = 0L;

  foreach (b; p.upperBound(2).lowerBound(n+1)) {
    ca[] = 0;
    foreach (c; p.upperBound(b).lowerBound(n+1)) ++ca[b+c];

    foreach (i; 1..n+1) ans += ca[i*2]*cb[i*2];

    foreach (d; p.upperBound(b).lowerBound(b+n*2+1)) ++cb[d-b];
  }

  writeln(ans);
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n+1)/2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n)-1)/2+1))
    if (sieve[p])
      for (auto q = p*3+1; q < (n+1)/2; q += p*2+1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array;
  r[0] = 2;

  return r;
}

pure T factor(T)(T n, const T[] p)
{
  auto ma = nsqrt(n)+1;
  foreach (pi; p)
    if (pi > ma) return n;
    else if (n%pi == 0) return pi;
  return n;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = T(1) << (n.bsr/2+1);
  return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
0