結果

問題 No.226 0-1パズル
ユーザー te-shte-sh
提出日時 2017-05-22 17:32:53
言語 D
(dmd 2.107.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,177 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 143,220 KB
最終ジャッジ日時 2023-09-03 13:29:41
合計ジャッジ時間 1,135 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

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

ソースコード

diff #

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

const p = 10 ^^ 9 + 7;
alias FactorRing!p mint;

void main()
{
  auto rd = readln.split.to!(size_t[]), h = rd[0], w = rd[1];
  auto sij = h.iota.map!(_ => readln.chomp.map!(c => c.predSwitch('0', 0, '1', 1, '?', 2)).array).array;
  auto r1 = calc1(sij, h, w), r2 = calc2(sij, h, w), r3 = calc3(sij, h, w);
  writeln(r1 + r2 - r3);
}

auto calc1(int[][] sij, size_t h, size_t w)
{
  mint r = 1;
  foreach (x; 0..w)
    r *= can0101(sij.transversal(x));
  return r;
}

auto calc2(int[][] sij, size_t h, size_t w)
{
  mint r = 1;
  foreach (y; 0..h)
    r *= can0101(sij[y]);
  return r;
}

auto calc3(int[][] sij, size_t h, size_t w)
{
  mint r = 0;
  if (sij.enumerate.all!(c => c.value.enumerate.all!(d => d.value == 2 || (c.index + d.index) % 2 == d.value))) ++r;
  if (sij.enumerate.all!(c => c.value.enumerate.all!(d => d.value == 2 || (c.index + d.index) % 2 != d.value))) ++r;
  return r;
}

auto can0101(R)(R s)
{
  auto r = 0;
  if (s.enumerate.all!(c => c.value == 2 || c.value == c.index % 2)) ++r;
  if (s.enumerate.all!(c => c.value == 2 || c.value != c.index % 2)) ++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;
  }

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

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

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

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

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