結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 20:06:40
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,026 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 83,556 KB
実行使用メモリ 29,744 KB
最終ジャッジ日時 2023-09-03 21:03:07
合計ジャッジ時間 7,010 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 194 ms
18,584 KB
testcase_26 AC 190 ms
18,352 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 378 ms
28,348 KB
testcase_29 AC 380 ms
28,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  auto args = readln.split.to!(string[]);
  const int MOD = 10 ^^ 9 + 7;
  int solve(string n) {
    auto memo = new int[][][][][](n.length + 1, 2, 2, 3, 8);
    foreach (a; 0 .. (n.length + 1)) {
      foreach (b; 0 .. 2) {
        foreach (c; 0 .. 2) {
          foreach (d; 0 .. 3) {
            foreach (e; 0 .. 8) {
              memo[a][b][c][d][e] = -1;
            }
          }
        }
      }
    }
    int f(size_t i, bool less, bool ok, int mod3, int mod8) {
      if (memo[i][less][ok][mod3][mod8] >= 0) {
        return memo[i][less][ok][mod3][mod8];
      }
      if (i == n.length) {
        return (ok || mod3 == 0) && (mod8 > 0);
      } else {
        auto digit = less ? 9 : n[i] - '0';
        int ret = 0;
        for (int d = 0; d <= digit; d++) {
          auto l = less || (d < digit);
          auto o = ok || (d == 3);
          auto m3 = (mod3 * 10 + d) % 3;
          auto m8 = (mod8 * 10 + d) % 8;
          (ret += f(i + 1, l, o, m3, m8)) %= MOD;
        }
        return memo[i][less][ok][mod3][mod8] = ret;
      }
    }

    return f(0, 0, 0, 0, 0);
  }

  bool yes(string n) {
    auto m8 = 0;
    foreach (ch; n) {
      (m8 *= 10) %= 8;
      m8 += ch - '0';
    }
    if (m8 == 0) {
      return false;
    } else if (n.count('3') > 0) {
      writeln("3");
      return true;
    } else {
      auto m3 = reduce!((res, ch) => (res + (ch - '0')) % 3)(0, n);
      return m3 == 0;
    }
  }

  auto res = (solve(args[1]) - solve(args[0]) + MOD) % MOD;
  writeln((res + yes(args[0])) % MOD);
}

void afill(Range, Type)(ref Range r, Type v) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref e; r) {
      e = v;
    }
  } else {
    foreach (ref _r; r) {
      afill(_r, v);
    }
  }
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}
0