結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 19:50:54
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,941 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 85,388 KB
実行使用メモリ 30,604 KB
最終ジャッジ日時 2023-09-03 21:02:23
合計ジャッジ時間 6,797 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 69 ms
8,516 KB
testcase_06 AC 46 ms
6,976 KB
testcase_07 AC 254 ms
22,184 KB
testcase_08 AC 176 ms
17,616 KB
testcase_09 AC 97 ms
10,344 KB
testcase_10 AC 287 ms
24,464 KB
testcase_11 AC 270 ms
24,084 KB
testcase_12 AC 159 ms
17,228 KB
testcase_13 AC 44 ms
6,476 KB
testcase_14 WA -
testcase_15 AC 61 ms
6,916 KB
testcase_16 AC 209 ms
18,184 KB
testcase_17 AC 165 ms
14,980 KB
testcase_18 WA -
testcase_19 AC 207 ms
18,884 KB
testcase_20 AC 146 ms
14,504 KB
testcase_21 WA -
testcase_22 AC 244 ms
20,944 KB
testcase_23 AC 23 ms
4,440 KB
testcase_24 AC 185 ms
17,380 KB
testcase_25 AC 187 ms
18,892 KB
testcase_26 AC 185 ms
19,180 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 371 ms
28,824 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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 - 1)) {
      pow10mod3 ~= pow10mod3[$ - 1] * 10 % 3;
      pow10mod8 ~= pow10mod8[$ - 1] * 10 % 8;
    }
    auto memo = new int[][][][][](n.length + 1, 2, 2, 3, 8);
    afill(memo, -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(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])) % 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