結果

問題 No.103 素因数ゲーム リターンズ
ユーザー te-sh
提出日時 2017-04-14 12:39:53
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,083 bytes
コンパイル時間 993 ms
コンパイル使用メモリ 120,212 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 18:43:08
合計ジャッジ時間 1,533 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

void main()
{
  auto n = readln.chomp.to!size_t;
  auto mi = readln.split.to!(int[]);

  auto maxM = mi.fold!max;
  auto pi = primes(maxM.to!real.sqrt.to!int + 1);

  auto r = mi.map!(m => factors(m, pi)).joiner.map!"a % 3".fold!"a ^ b";
  writeln(r ? "Alice" : "Bob");
}

int[] factors(int n, int[] pi)
{
  int[int] buf;
  while (n > 1) {
    auto f = factor(n, pi);
    ++buf[f];
    n /= f;
  }
  return buf.values;
}

pure int[] primes(int n)
{
  import std.math, std.bitmanip;

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

  foreach (p; 1..((n.to!real.sqrt.to!int - 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!int).map!("a * 2 + 1").array;
  r[0] = 2;

  return r;
}

int factor(int i, int[] pi)
{
  auto ma = i.to!real.sqrt.to!int + 1;
  foreach (p; pi)
    if (p > ma) return i;
    else if (i % p == 0) return p;
  return i;
}
0