結果

問題 No.319 happy b1rthday 2 me
ユーザー ikdikd
提出日時 2018-10-05 08:52:15
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,198 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 84,532 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 21:09:24
合計ジャッジ時間 2,460 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

  long a, b;
  rd(a, b);

  long f(long _n) {
    int[] n;
    do {
      n = _n % 10 ~ n;
      _n /= 10;
    }
    while (_n);
    auto memog = new long[][][][](n.length + 1, 2, 10, n.length + 1);
    afill(memog, -1L);
    long g(size_t i, bool less, int prev, int count12) {
      if (i == n.length) {
        return count12;
      } else {
        if (memog[i][less][prev][count12] >= 0) {
          return memog[i][less][prev][count12];
        }
        long ret = 0;
        auto digit = less ? 9 : n[i];
        for (int d = 0; d <= digit; d++) {
          auto l = less || (d < digit);
          auto p = d;
          auto c12 = count12 + (i > 0 && prev == 1 && d == 2);
          ret += g(i + 1, l, p, c12);
        }
        return memog[i][less][prev][count12] = ret;
      }
    }

    auto memoh = new long[][][][](n.length + 1, 2, 10, 10);
    afill(memoh, -1L);
    long h(size_t i, bool less, int first, int last) {
      if (i == n.length) {
        return first == 2 && last == 2;
      } else {
        if (memoh[i][less][first][last] >= 0) {
          return memoh[i][less][first][last];
        }
        long ret = 0;
        auto digit = less ? 9 : n[i];
        for (int d = 0; d <= digit; d++) {
          auto l = less || (d < digit);
          auto fi = first == 0 && d > 0 ? d : first;
          auto la = d;
          ret += h(i + 1, l, fi, la);
        }
        return memoh[i][less][first][last] = ret;
      }
    }

    return g(0, 0, 0, 0) + h(0, 0, 0, 0);
  }

  auto res = f(b) - f(a - 1);
  {
    auto last = a % 10, first = -1;
    while (a) {
      first = a % 10;
      a /= 10;
    }
    if (first == 2 && last == 2) {
      res--;
    }
  }
  writeln(res);
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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