結果

問題 No.260 世界のなんとか3
ユーザー te-shte-sh
提出日時 2017-06-01 12:01:04
言語 D
(dmd 2.107.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,112 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 126,760 KB
最終ジャッジ日時 2023-09-03 13:48:49
合計ジャッジ時間 749 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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(13):        instantiated from here: `writeln!(FactorRing!(1000000007, false))`

ソースコード

diff #

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

const p = 10 ^^ 9 + 7;
alias FactorRing!(p, true) pint;
alias FactorRing!p mint;

void main()
{
  auto rd = readln.split.map!(s => s.map!(c => (c - '0').to!int).array), a = rd[0], b = rd[1];

  auto r = mint(calc!true(b)) - mint(calc!false(a));

  writeln(r);
}

auto calc(bool includeMax)(int[] ai)
{
  auto n = ai.length;

  auto dp = new pint[][][][][](n+1, 2, 2, 3, 8);
  dp[0][0][0][0][0] = 1;

  foreach (i; 0..n)
    foreach (j; 0..2)
      foreach (k; 0..2)
        foreach (l; 0..3)
          foreach (m; 0..8) {
            int lim = j ? 9 : ai[i];
            foreach (d; 0..lim+1)
              dp[i+1][j || d < lim][k || d == 3][(l + d) % 3][(m * 10 + d) % 8] += dp[i][j][k][l][m];
          }

  auto r = mint(0);

  auto jMin = includeMax ? 0 : 1;
  foreach (j; jMin..2)
    foreach (k; 0..2)
      foreach (l; 0..3)
        foreach (m; 1..8)
          if (k == 1 || l == 0)
            r += dp[$-1][j][k][l][m];

  return r;
}

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: "+")(int rhs) const { return FactorRing!(m, pos)(v + rhs); }
  pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(v - rhs); }
  pure auto opBinary(string op: "*")(int 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: "+")(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, pos) rhs)
    if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(rhs.v); }
}
0