結果

問題 No.10 +か×か
ユーザー te-sh
提出日時 2016-09-26 11:58:54
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,785 ms / 5,000 ms
コード長 819 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 118,844 KB
実行使用メモリ 83,484 KB
最終ジャッジ日時 2024-06-12 04:31:19
合計ジャッジ時間 4,741 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

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

  auto memo = new string[][](n, t + 1);

  string rec(int s, string ops) {
    auto i = ops.length;
    if (s > t) return "E";

    if (i == n - 1)
      return s == t ? ops : "E";

    if (memo[i][s] != "")
      return memo[i][s];

    auto a = ai[i + 1];
    auto r1 = rec(s + a, ops ~ "+");
    if (r1 != "E")
      return memo[i][s] = r1;
    auto r2 = rec(s * a, ops ~ "*");
    if (r2 != "E")
      return memo[i][s] = r2;
    return memo[i][s] = "E";
  }

  writeln(rec(ai[0], ""));
}
0