結果

問題 No.15 カタログショッピング
コンテスト
ユーザー te-sh
提出日時 2016-09-26 15:14:47
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 988 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 710 ms
コンパイル使用メモリ 117,020 KB
実行使用メモリ 14,048 KB
最終ジャッジ日時 2026-03-05 07:49:43
合計ジャッジ時間 1,464 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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 rd = readln.split;
  auto n = rd[0].to!size_t, s = rd[1].to!int;
  auto pi = iota(n).map!(_ => readln.chomp.to!int).array;

  if (n == 1) {
    if (pi[0] == s) writeln(1);
    else writeln(-1);
    return;
  }

  auto m1 = n / 2;
  auto m2 = n - m1;

  size_t[][int] hi;
  foreach (i; 0..1 << m2) {
    auto u = i.bitsSet.map!(j => pi[j + m1]).sum;
    hi[u] ~= i;
  }

  size_t[] ri;
  foreach (i; 0..1 << m1) {
    auto u = i.bitsSet.map!(j => pi[j]).sum;
    auto w = s - u;
    if (w < 0 || !(w in hi)) continue;
    ri ~= hi[w].map!(x => (x << m1) | i).array;
  }

  if (ri.empty) {
    writeln(-1);
  } else {
    auto ri2 = ri.map!(r => r.bitsSet.array).array;
    ri2.sort();
    ri2.each!(r => writeln(r.map!("a + 1").map!(to!string).join(" ")));
  }
}
0