結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 20:08:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 366 ms / 2,000 ms
コード長 1,781 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 83,400 KB
実行使用メモリ 29,808 KB
最終ジャッジ日時 2023-09-03 21:03:28
合計ジャッジ時間 6,802 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 365 ms
29,216 KB
testcase_04 AC 366 ms
29,260 KB
testcase_05 AC 68 ms
8,300 KB
testcase_06 AC 47 ms
7,324 KB
testcase_07 AC 254 ms
21,872 KB
testcase_08 AC 171 ms
18,288 KB
testcase_09 AC 95 ms
10,356 KB
testcase_10 AC 282 ms
23,452 KB
testcase_11 AC 265 ms
22,700 KB
testcase_12 AC 157 ms
15,332 KB
testcase_13 AC 43 ms
6,140 KB
testcase_14 AC 241 ms
20,748 KB
testcase_15 AC 61 ms
6,980 KB
testcase_16 AC 206 ms
17,832 KB
testcase_17 AC 161 ms
14,744 KB
testcase_18 AC 157 ms
13,972 KB
testcase_19 AC 203 ms
18,596 KB
testcase_20 AC 145 ms
13,728 KB
testcase_21 AC 129 ms
12,968 KB
testcase_22 AC 240 ms
20,860 KB
testcase_23 AC 23 ms
4,400 KB
testcase_24 AC 182 ms
18,360 KB
testcase_25 AC 185 ms
19,316 KB
testcase_26 AC 183 ms
18,624 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 363 ms
29,808 KB
testcase_29 AC 366 ms
29,008 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);
    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(string n) {
    auto m8 = 0;
    foreach (ch; n) {
      (m8 *= 10) %= 8;
      (m8 += ch - '0') %= 8;
    }
    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