結果

問題 No.10 +か×か
ユーザー te-sh
提出日時 2017-02-08 15:26:28
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 77 ms / 5,000 ms
コード長 636 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 101,788 KB
実行使用メモリ 82,508 KB
最終ジャッジ日時 2024-06-12 06:59:00
合計ジャッジ時間 1,700 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto t = readln.chomp.to!int;
  auto ai = readln.split.to!(int[]);

  auto dp = new string[][](n, t + 1);
  auto calc(size_t i, int acc) {
    if (i == n) return acc == t ? "" : "E";
    if (acc > t) return "E";
    if (!dp[i][acc].empty) return dp[i][acc];

    auto r1 = calc(i + 1, acc + ai[i]);
    if (r1 != "E") return dp[i][acc] = "+" ~ r1;
    auto r2 = calc(i + 1, acc * ai[i]);
    if (r2 != "E") return dp[i][acc] = "*" ~ r2;

    return dp[i][acc] = "E";
  }

  auto r = calc(1, ai.front);
  writeln(r);
}
0