結果

問題 No.144 エラトステネスのざる
ユーザー te-sh
提出日時 2016-09-12 13:48:27
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 1,117 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 174,412 KB
実行使用メモリ 23,252 KB
最終ジャッジ日時 2024-06-12 04:16:56
合計ジャッジ時間 11,672 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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