結果

問題 No.137 貯金箱の焦り
ユーザー te-shte-sh
提出日時 2017-08-01 12:28:46
言語 D
(dmd 2.107.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,662 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 127,832 KB
最終ジャッジ日時 2023-09-03 15:35:20
合計ジャッジ時間 1,641 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/format/internal/write.d(164): Error: cannot implicitly convert expression `obj` of type `const(FactorRing!(1234567891, true))` to `int`
/dmd2/linux/bin64/../../src/phobos/std/format/write.d(1239): Error: template instance `std.format.internal.write.formatValueImpl!(LockingTextWriter, FactorRing!(1234567891, true), char)` error instantiating
/dmd2/linux/bin64/../../src/phobos/std/format/write.d(632):        instantiated from here: `formatValue!(LockingTextWriter, FactorRing!(1234567891, true), char)`
/dmd2/linux/bin64/../../src/phobos/std/stdio.d(1722):        instantiated from here: `formattedWrite!(LockingTextWriter, char, FactorRing!(1234567891, true))`
/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4237):        instantiated from here: `write!(FactorRing!(1234567891, true), char)`
Main.d(24):        instantiated from here: `writeln!(FactorRing!(1234567891, true))`

ソースコード

diff #

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

const mod = 1234567891;
alias FactorRing!(mod, true) mint;

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, m = rd[1].to!long;
  auto a = readln.split.to!(int[]), sa = a.sum;

  auto dp1 = new mint[](sa*2+2), dp2 = new mint[](sa*2+2);
  dp1[0] = mint(1);

  for (; m > 0; m /= 2) {
    foreach (i; 0..n) {
      dp2[] = dp1[];
      dp1[a[i]..$] += dp2[0..$-a[i]];
    }

    foreach (j; 0..sa+1) dp1[j] = dp1[j*2+m%2];
    dp1[sa+1..$] = mint(0);
  }

  writeln(dp1[0]);
}

struct FactorRing(int m, bool pos = false)
{
  long v;

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

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

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

  pure auto mod(long _v) const
  {
    static if (pos) return _v % m;
    else return (_v % m + m) % m;
  }

  pure auto opBinary(string op: "+")(long rhs) const { return FactorRing!(m, pos)(v + rhs); }
  pure auto opBinary(string op: "-")(long rhs) const { return FactorRing!(m, pos)(v - rhs); }
  pure auto opBinary(string op: "*")(long rhs) const { return FactorRing!(m, pos)(v * rhs); }

  pure auto opBinary(string op)(FactorRing!(m, pos) rhs) const
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(rhs.v); }

  auto opOpAssign(string op: "+")(long rhs) { v = mod(v + rhs); }
  auto opOpAssign(string op: "-")(long rhs) { v = mod(v - rhs); }
  auto opOpAssign(string op: "*")(long rhs) { v = mod(v * rhs); }

  auto opOpAssign(string op)(FactorRing!(m, pos) rhs)
    if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(rhs.v); }
}
0