結果

問題 No.10 +か×か
ユーザー te-shte-sh
提出日時 2017-02-08 15:58:20
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 750 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 89,152 KB
実行使用メモリ 7,732 KB
最終ジャッジ日時 2023-09-03 01:25:13
合計ジャッジ時間 1,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 11 ms
7,696 KB
testcase_04 AC 7 ms
6,224 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 11 ms
7,732 KB
testcase_07 AC 7 ms
6,100 KB
testcase_08 AC 4 ms
4,508 KB
testcase_09 AC 4 ms
5,432 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 bool[][](n + 1, t + 1);
  dp[n][t] = true;

  foreach_reverse (i; 0..n)
    foreach (j; 0..t+1)
      if (dp[i + 1][j]) {
        auto a = ai[i];
        if (j >= a) dp[i][j - a] = true;
        if (j % a == 0) dp[i][j / a] = true;
      }

  auto calc(size_t i, int acc) {
    if (i == n) return "";

    auto a = ai[i];
    if (acc + a <= t && dp[i + 1][acc + a]) return "+" ~ calc(i + 1, acc + a);
    if (acc * a <= t && dp[i + 1][acc * a]) return "*" ~ calc(i + 1, acc * a);

    return "E";
  }

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