結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 19:13:59
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,560 bytes
コンパイル時間 698 ms
コンパイル使用メモリ 84,700 KB
実行使用メモリ 8,548 KB
最終ジャッジ日時 2023-09-03 21:01:50
合計ジャッジ時間 4,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

  auto args = readln.split.to!(char[][]);
  const int MOD = 10 ^^ 9 + 7;
  int solve(char[] n) {
    int[] pow10mod3 = [1];
    int[] pow10mod8 = [1];
    foreach (_; 0 .. n.length) {
      pow10mod3 ~= pow10mod3[$ - 1] * 10 % 3;
      pow10mod8 ~= pow10mod8[$ - 1] * 10 % 8;
    }
    int f(size_t i, bool less, bool ok, int mod3, int 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 + d * pow10mod3[n.length - i - 1]) % 3;
          auto m8 = (mod8 + d * pow10mod8[n.length - i - 1]) % 8;
          (ret += f(i + 1, l, o, m3, m8)) %= MOD;
        }
        return ret;
      }
    }

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

  bool yes(char[] n) {
    auto m8 = reduce!((res, ch) => (res + (ch - '0')) % 8)(0, n);
    if (m8 == 0) {
      return false;
    } else if (n.count('3') > 0) {
      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]));
}

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