結果

問題 No.362 門松ナンバー
ユーザー ikdikd
提出日時 2018-10-05 07:22:33
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 875 ms / 3,000 ms
コード長 2,020 bytes
コンパイル時間 601 ms
コンパイル使用メモリ 85,136 KB
実行使用メモリ 14,656 KB
最終ジャッジ日時 2023-09-03 21:06:58
合計ジャッジ時間 14,680 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
13,696 KB
testcase_01 AC 227 ms
14,120 KB
testcase_02 AC 226 ms
13,464 KB
testcase_03 AC 114 ms
9,276 KB
testcase_04 AC 61 ms
5,492 KB
testcase_05 AC 362 ms
13,696 KB
testcase_06 AC 635 ms
13,988 KB
testcase_07 AC 344 ms
14,656 KB
testcase_08 AC 449 ms
13,696 KB
testcase_09 AC 714 ms
13,848 KB
testcase_10 AC 868 ms
13,892 KB
testcase_11 AC 842 ms
13,720 KB
testcase_12 AC 855 ms
13,696 KB
testcase_13 AC 856 ms
14,132 KB
testcase_14 AC 851 ms
14,116 KB
testcase_15 AC 860 ms
13,916 KB
testcase_16 AC 847 ms
13,656 KB
testcase_17 AC 794 ms
13,664 KB
testcase_18 AC 870 ms
13,672 KB
testcase_19 AC 560 ms
13,960 KB
testcase_20 AC 875 ms
13,660 KB
testcase_21 AC 91 ms
7,000 KB
権限があれば一括ダウンロードができます

ソースコード

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);
    auto memo = new long[][][][](20, 2, 1000, 4);
    long kadocount(long _n) {
      int[] n;
      do {
        n = _n % 10 ~ n;
        _n /= 10;
      }
      while (_n);
      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