結果

問題 No.144 エラトステネスのざる
ユーザー te-shte-sh
提出日時 2016-09-12 13:48:27
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,117 bytes
コンパイル時間 2,520 ms
コンパイル使用メモリ 171,652 KB
実行使用メモリ 23,596 KB
最終ジャッジ日時 2023-09-02 22:02:44
合計ジャッジ時間 11,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 1,090 ms
23,392 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,121 ms
21,836 KB
testcase_19 AC 1,096 ms
22,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto rd = readln.split;
  auto n = rd[0].to!int, p = rd[1].to!real;

  auto primes = primes(n);

  auto ri = new real[](n + 1);
  foreach (i; 2..n + 1) {
    auto d = divisors(i, primes).length;
    if (d == 0)
      ri[i] = 1;
    else
      ri[i] = (1 - p) ^^ d;
  }

  writefln("%.7f", ri[2..$].sum);
}

int[] primes(int n)
{
  auto sieve = new bool[](n + 1);
  sieve[0..2] = false;
  sieve[2..$] = true;

  foreach (p; 2..n.to!real.sqrt.to!int + 1)
    if (sieve[p])
      foreach (q; iota(p * 2, n + 1, p))
        sieve[q] = false;

  return sieve.enumerate
    .filter!("a[1]")
    .map!("a[0]").map!(to!int).array;
}

int[] divisors(int n, int[] primes)
{
  int[] ri;
  auto limit = n.to!real.sqrt.to!int;
  foreach (p; primes) {
    if (p > n.to!real.sqrt) break;
    if (n % p == 0) {
      if (n == p * p)
        ri ~= p;
      else
        ri ~= [p, n / p];
    }
  }
  return ri;
}
0