結果

問題 No.623 fudan no modulus to tigau
ユーザー te-shte-sh
提出日時 2017-12-23 00:13:41
言語 D
(dmd 2.107.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,946 bytes
コンパイル時間 557 ms
コンパイル使用メモリ 126,416 KB
最終ジャッジ日時 2023-09-03 17:46:49
合計ジャッジ時間 1,647 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

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

ソースコード

diff #

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

const mod = 998_244_353;
alias mint = FactorRing!mod;

void main()
{
  auto n = readln.chomp.to!int;
  auto t = new int[](n-1), a = new int[](n-1), b = new int[](n-1);
  foreach (i; 0..n-1) {
    auto rd = readln.splitter;
    t[i] = rd.front.to!int; rd.popFront();
    a[i] = rd.front.to!int; rd.popFront();
    b[i] = rd.front.to!int;
  }

  auto calc(mint x)
  {
    auto v = new mint[](n+1);
    v[0] = 1; v[1] = x;
    foreach (i; 2..n+1) {
      switch (t[i-2]) {
      case 1:
        v[i] = v[a[i-2]] + v[b[i-2]];
        break;
      case 2:
        v[i] = v[b[i-2]] * a[i-2];
        break;
      case 3:
        v[i] = v[a[i-2]] * v[b[i-2]];
        break;
      default:
        assert(0);
      }
    }
    return v[n];
  }

  auto q = readln.chomp.to!int;
  auto x = readln.split.to!(int[]);
  foreach (xi; x) writeln(calc(mint(xi)));
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) {
    union { long vl; struct { int vi2; int vi; } }
  } else {
    union { long vl; int vi; }
  }

  static init() { return FactorRing!(m, pos)(0); }

  @property int toInt() { return vi; }
  alias toInt this;

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

  ref FactorRing!(m, pos) 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 auto opUnary(string op: "-")() const { return FactorRing!(m, pos)(mod(-vi)); }
  }

  static if (m < int.max / 2) {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vi + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vi - rhs)); }
  } else {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vl + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vl - rhs)); }
  }
  pure auto opBinary(string op: "*")(int rhs) const { return FactorRing!(m, pos)(mod(vl * rhs)); }

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

  static if (m < int.max / 2) {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vi + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vi - rhs); }
  } else {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vl + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vl - rhs); }
  }
  auto opOpAssign(string op: "*")(int rhs) { vi = mod(vl * rhs); }

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