結果

問題 No.28 末尾最適化
ユーザー te-shte-sh
提出日時 2017-02-15 11:33:04
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,792 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 101,656 KB
実行使用メモリ 6,664 KB
最終ジャッジ日時 2023-09-03 01:30:00
合計ジャッジ時間 1,819 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const p = 100_000_009;
alias FactorRing!p mint;

void main()
{
  auto q = readln.chomp.to!size_t;
  foreach (_; q.iota) {
    auto rd = readln.split.to!(int[]);
    auto seed = mint(rd[0]), n = rd[1], k = rd[2], b = rd[3];

    auto fs = factors(b);

    auto xi = new mint[](n + 1);
    xi[0] = seed;
    foreach (i; n.iota)
      xi[i + 1] = mint(1) + (xi[i] * (xi[i] + 12345));

    auto r = int.max;
    foreach (f; fs) {
      auto pi = xi.map!(x => hasPower(x, f.index)).array;
      auto s = pi.sort().take(k).sum;
      r = min(r, s / f.value);
    }

    writeln(r);
  }
}

auto factors(int b)
{
  auto r = new int[](32);
  while (b > 1)
    foreach (p; 2..b+1)
      if (b % p == 0) {
        ++r[p];
        b /= p;
        continue;
      }
  return r.enumerate!int.filter!"a[1] > 0";
}

auto hasPower(int a, int b)
{
  auto r = 0;
  for (; a > 0 && a % b == 0; a /= b) ++r;
  return r;
}

struct FactorRing(int m) {
  long v;

  @property int toInt() { return v.to!int; }
  alias toInt this;

  this(T)(T _v) { v = mod(_v); }

  ref FactorRing!m opAssign(int _v) {
    v = mod(_v);
    return this;
  }

  auto mod(long _v) { return _v > 0 ? _v % m : ((_v % m) + m) % m; }

  auto opBinary(string op: "+")(FactorRing!m rhs) { return FactorRing!m(v + rhs.v); }
  auto opBinary(string op: "-")(FactorRing!m rhs) { return FactorRing!m(v - rhs.v); }
  auto opBinary(string op: "*")(FactorRing!m rhs) { return FactorRing!m(v * rhs.v); }
  auto opBinary(string op: "^^")(FactorRing!m rhs) { return pow(this, rhs.toInt); }

  auto opBinary(string op)(int rhs)
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(FactorRing!m(rhs)); }
  auto opBinary(string op: "^^")(int rhs) { return pow(this, rhs); }
}
0