結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 19:58:34
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,987 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 83,504 KB
実行使用メモリ 42,216 KB
最終ジャッジ日時 2023-09-03 21:02:55
合計ジャッジ時間 7,019 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 73 ms
10,612 KB
testcase_06 AC 49 ms
7,764 KB
testcase_07 AC 259 ms
28,904 KB
testcase_08 AC 182 ms
22,480 KB
testcase_09 AC 100 ms
13,964 KB
testcase_10 AC 292 ms
31,968 KB
testcase_11 AC 275 ms
30,880 KB
testcase_12 AC 167 ms
20,504 KB
testcase_13 AC 47 ms
7,200 KB
testcase_14 WA -
testcase_15 AC 65 ms
10,108 KB
testcase_16 AC 220 ms
25,996 KB
testcase_17 AC 174 ms
20,768 KB
testcase_18 WA -
testcase_19 AC 219 ms
26,896 KB
testcase_20 AC 154 ms
18,436 KB
testcase_21 WA -
testcase_22 AC 251 ms
27,416 KB
testcase_23 AC 24 ms
4,892 KB
testcase_24 AC 193 ms
23,668 KB
testcase_25 AC 199 ms
24,752 KB
testcase_26 AC 195 ms
24,960 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 379 ms
40,424 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

  auto args = readln.split.to!(string[]);
  const long MOD = 10 ^^ 9 + 7;
  long solve(string n) {
    auto memo = new long[][][][][](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;
            }
          }
        }
      }
    }
    long 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';
        long 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 = 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