結果

問題 No.663 セルオートマトンの逆操作
ユーザー te-shte-sh
提出日時 2018-04-05 16:44:31
言語 D
(dmd 2.105.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,936 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 123,552 KB
最終ジャッジ日時 2023-09-03 19:13:36
合計ジャッジ時間 1,257 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

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

ソースコード

diff #

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t)v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

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

void main()
{
  int n; readV(n);
  int[] e; readM(n, e);

  auto calc(int a0, int a1)
  {
    auto dp = new mint[][](n, 4);
    dp[0][a1] = 1;

    foreach (i; 1..n) {
      if (e[i] == 0) {
        dp[i][0] += dp[i-1][0]+dp[i-1][2];
        dp[i][3] += dp[i-1][3];
      } else {
        dp[i][1] += dp[i-1][0]+dp[i-1][2];
        dp[i][2] += dp[i-1][1]+dp[i-1][3];
        dp[i][3] += dp[i-1][1];
      }
    }

    return dp[n-1][a0];
  }

  if (e[0] == 0)
    writeln(calc(0, 0)+calc(2, 0)+calc(3, 3));
  else
    writeln(calc(0, 1)+calc(1, 2)+calc(1, 3)+calc(2, 1)+calc(3, 2));
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }
  alias FR = FactorRing!(m, pos);
  @property static init() { return FR(0); }
  @property int value() { return vi; }
  @property void value(int v) { vi = mod(v); }
  alias value this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref auto opAssign(int v) { vi = v; return this; }

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

  static if (!pos) pure ref auto opUnary(string op: "-")() { return FR(mod(-vi)); }

  static if (m < int.max / 2) {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vi"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vi"~op~"r")); return this; }
  } else {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vl"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vl"~op~"r")); return this; }
  }
  pure ref auto opBinary(string op: "*")(int r) { return FR(mod(vl*r)); }
  ref auto opOpAssign(string op: "*")(int r) { vi = mod(vl*r); return this; }

  pure ref auto opBinary(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opBinary!op(r.vi); }
  ref auto opOpAssign(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(r.vi); }
}
0