結果

問題 No.362 門松ナンバー
ユーザー ikdikd
提出日時 2018-10-05 06:23:41
言語 D
(dmd 2.106.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,032 bytes
コンパイル時間 3,345 ms
コンパイル使用メモリ 99,968 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 03:58:56
合計ジャッジ時間 5,354 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

  int t;
  rd(t);
  while (t--) {
    long k;
    rd(k);
    long kadocount(long _n) {
      int[] n;
      do {
        n = _n % 10 ~ n;
        _n /= 10;
      }
      while (_n);
      auto memo = new long[][][][](n.length + 1, 2, 1000, 4);
      afill(memo, -1L);
      long f(size_t i, bool less, int last3, int trailing) {
        if (i == n.length) {
          return trailing >= 3 && kadoma2(last3);
        } else {
          if (memo[i][less][last3][trailing] >= 0) {
            return memo[i][less][last3][trailing];
          }
          long ret = 0;
          auto digit = less ? 9 : n[i];
          for (int d = 0; d <= digit; d++) {
            auto l = less || (d < digit);
            auto t = min(3, trailing + (trailing > 0 || d > 0));
            if (i < 2 || t < 3 || kadoma2((last3 * 10 + d) % 1000)) {
              ret += f(i + 1, l, (last3 * 10 + d) % 1000, t);
            }
          }
          return memo[i][less][last3][trailing] = ret;
        }
      }

      return f(0, 0, 0, 0);
    }

    bool enough(long n) {
      auto kk = kadocount(n);
      return kk >= k;
    }

    long ng = 100, ok = 10L ^^ 18;
    while (ok - ng > 1) {
      auto m = (ng + ok) / 2;
      (enough(m) ? ok : ng) = m;
    }
    writeln(ok);
  }
}

bool kadoma2(int n) {
  int x = n / 100, y = n / 10 % 10, z = n % 10;
  if (x == y || y == z || z == x) {
    return false;
  } else if (x < y && y > z) {
    return true;
  } else if (x > y && y < z) {
    return true;
  } else {
    return false;
  }
}

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