結果

問題 No.362 門松ナンバー
ユーザー te-shte-sh
提出日時 2017-07-12 16:46:42
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 197 ms / 3,000 ms
コード長 1,771 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 95,908 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 15:10:47
合計ジャッジ時間 4,018 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
4,380 KB
testcase_01 AC 53 ms
4,380 KB
testcase_02 AC 49 ms
4,376 KB
testcase_03 AC 27 ms
4,376 KB
testcase_04 AC 15 ms
4,380 KB
testcase_05 AC 69 ms
4,380 KB
testcase_06 AC 122 ms
4,376 KB
testcase_07 AC 72 ms
4,376 KB
testcase_08 AC 70 ms
4,376 KB
testcase_09 AC 140 ms
4,376 KB
testcase_10 AC 125 ms
4,376 KB
testcase_11 AC 135 ms
4,376 KB
testcase_12 AC 143 ms
4,376 KB
testcase_13 AC 119 ms
4,376 KB
testcase_14 AC 147 ms
4,380 KB
testcase_15 AC 135 ms
4,380 KB
testcase_16 AC 197 ms
4,376 KB
testcase_17 AC 152 ms
4,376 KB
testcase_18 AC 113 ms
4,376 KB
testcase_19 AC 105 ms
4,376 KB
testcase_20 AC 94 ms
4,380 KB
testcase_21 AC 14 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

const ma = 37294859064823L;

void main()
{
  auto t = readln.chomp.to!size_t;

  auto memo = new long[](14);
  foreach (i; 3..14)
    memo[i] = calcDp((10L ^^ i - 1).toArray);
  foreach (i; 4..14)
    memo[i] += memo[i-1];

  auto calcNum(long n)
  {
    auto m = n.toArray;
    auto r = calcDp(m);
    r += memo[m.length-1];
    return r;
  }

  auto calc()
  {
    auto n = readln.chomp.to!long;
    auto r = iota(1, ma+1)
      .map!(i => Tuple!(long, long)(i, calcNum(i)))
      .assumeSorted!"a[1] < b[1]"
      .equalRange(Tuple!(long, long)(0, n));
    writeln(r.front[0]);
  }

  foreach (_; 0..t) calc;
}

auto calcDp(int[] x)
{
  if (x.length < 3) return 0L;
  auto n = x.length;
  auto dp = new long[][][][](n-2, 2, 10, 10);

  auto t3 = x[0..3].toLong;
  foreach (i; 100..t3+1) {
    auto t = i.toArray;
    if (isKadomatsu(t[0], t[1], t[2]))
      ++dp[0][i == t3][t[1]][t[2]];
  }

  foreach (i; 1..n-2)
    foreach (j; 0..2)
      foreach (k; 0..10)
        foreach (l; 0..10) {
          auto maxD = j ? x[i+2] : 9;
          foreach (d; 0..maxD+1)
            if (isKadomatsu(k, l, d))
              dp[i][j && d == maxD][l][d] += dp[i-1][j][k][l];
        }

  auto r = 0L;
  foreach (j; 0..2)
    foreach (k; 0..10)
      foreach (l; 0..10)
        r += dp[$-1][j][k][l];

  return r;
}

int[] toArray(long s)
{
  if (s == 0) return [0];

  int[] r;
  for (; s > 0; s /= 10)
    r = (s % 10) ~ r;

  return r;
}

long toLong(int[] s)
{
  auto r = 0;

  foreach (si; s) {
    r *= 10;
    r += si;
  }

  return r;
}

auto isKadomatsu(int a, int b, int c)
{
  return a != b && b != c && c != a && (a > b && c > b || a < b && c < b);
}
0