結果

問題 No.434 占い
ユーザー te-shte-sh
提出日時 2017-12-14 16:10:34
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 110,652 KB
実行使用メモリ 20,928 KB
最終ジャッジ日時 2023-09-03 17:33:33
合計ジャッジ時間 3,071 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 10 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 105 ms
17,596 KB
testcase_16 AC 63 ms
5,900 KB
testcase_17 AC 44 ms
4,384 KB
testcase_18 AC 25 ms
4,380 KB
testcase_19 AC 21 ms
4,400 KB
testcase_20 AC 90 ms
20,928 KB
testcase_21 AC 105 ms
15,304 KB
testcase_22 AC 64 ms
4,444 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 63 ms
4,448 KB
testcase_25 AC 103 ms
15,340 KB
testcase_26 AC 44 ms
9,100 KB
testcase_27 AC 35 ms
4,400 KB
testcase_28 AC 33 ms
4,452 KB
testcase_29 AC 26 ms
5,488 KB
testcase_30 AC 73 ms
15,364 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(26): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(28): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto t = readln.chomp.to!int;
  auto s = new int[][](t);
  foreach (i; 0..t)
    s[i] = readln.chomp.map!(c => cast(int)(c - '0')).array;

  auto m = s.map!(si => si.length.to!int).reduce!max;
  auto p = primes(m);

  auto f = new long[][](m+1, 10);
  foreach (i; 2..m+1) {
    auto fi = i.factor(p);
    ++f[i][fi.toSingle];

    auto j = i/fi;
    if (j > 1) f[i][] += f[j][];
  }

  foreach (si; s) {
    auto n = si.length.to!int;
    auto c = new long[](10);
    auto r = 0;
    foreach (int j, sij; si) {
      auto ct = 1;
      foreach (int i, ci; c)
        if (ci)
          ct = (ct * repeatedSquare(i, ci)).toSingle;

      r = (r + ct * sij).toSingle;

      c[] += f[n-j-1][];
      c[] -= f[j+1][];
    }
    writeln(r);
  }
}

pure T repeatedSquare(T, alias pred = "a * b", U)(T a, U n)
{
  return repeatedSquare(a, n, T(1));
}

pure T repeatedSquare(T, alias pred = "a * b", U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if ((n & 1) == 1)
      r = predFun(r, a).toSingle;
    a = predFun(a, a).toSingle;
    n >>= 1;
  }

  return r;
}

auto toSingle(int n)
{
  while (n >= 10) n = n/10 + n%10;
  return n;
}

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 = 1 << (n.bsr / 2 + 1);
  return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
0