結果

問題 No.260 世界のなんとか3
ユーザー ikdikd
提出日時 2018-10-04 20:26:50
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 515 ms / 2,000 ms
コード長 2,009 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 83,316 KB
実行使用メモリ 24,040 KB
最終ジャッジ日時 2023-09-03 21:03:48
合計ジャッジ時間 7,469 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 415 ms
24,040 KB
testcase_04 AC 412 ms
23,008 KB
testcase_05 AC 76 ms
6,400 KB
testcase_06 AC 52 ms
5,108 KB
testcase_07 AC 283 ms
17,644 KB
testcase_08 AC 193 ms
12,572 KB
testcase_09 AC 108 ms
9,252 KB
testcase_10 AC 320 ms
18,308 KB
testcase_11 AC 298 ms
17,344 KB
testcase_12 AC 177 ms
12,120 KB
testcase_13 AC 50 ms
5,152 KB
testcase_14 AC 273 ms
16,012 KB
testcase_15 AC 69 ms
5,880 KB
testcase_16 AC 233 ms
13,852 KB
testcase_17 AC 183 ms
12,924 KB
testcase_18 AC 176 ms
11,348 KB
testcase_19 AC 228 ms
13,880 KB
testcase_20 AC 163 ms
11,876 KB
testcase_21 AC 147 ms
10,000 KB
testcase_22 AC 270 ms
17,688 KB
testcase_23 AC 27 ms
4,376 KB
testcase_24 AC 208 ms
13,200 KB
testcase_25 AC 265 ms
12,780 KB
testcase_26 AC 155 ms
12,804 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 308 ms
23,132 KB
testcase_29 AC 515 ms
23,388 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 dp = new int[][][][][](n.length + 1, 2, 2, 3, 8);
    dp[0][0][0][0][0] = 1;
    foreach (i; 0 .. n.length) {
      foreach (less; 0 .. 2) {
        foreach (ok; 0 .. 2) {
          foreach (mod3; 0 .. 3) {
            foreach (mod8; 0 .. 8) {
              auto digit = less ? 9 : n[i] - '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;
                (dp[i + 1][l][o][m3][m8] += dp[i][less][ok][mod3][mod8]) %= MOD;
              }
            }
          }
        }
      }
    }
    int ret = 0;
    foreach (l; 0 .. 2) {
      foreach (o; 0 .. 2) {
        foreach (m3; 0 .. 3) {
          foreach (m8; 0 .. 8) {
            if ((o || m3 == 0) && (m8 > 0)) {
              (ret += dp[n.length][l][o][m3][m8]) %= MOD;
            }
          }
        }
      }
    }
    return ret;
  }

  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