結果
| 問題 | No.15 カタログショッピング | 
| コンテスト | |
| ユーザー |  vjudge1 | 
| 提出日時 | 2025-09-08 10:29:36 | 
| 言語 | Crystal (1.14.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 56 ms / 5,000 ms | 
| コード長 | 986 bytes | 
| コンパイル時間 | 14,104 ms | 
| コンパイル使用メモリ | 316,376 KB | 
| 実行使用メモリ | 20,224 KB | 
| 最終ジャッジ日時 | 2025-09-08 10:29:51 | 
| 合計ジャッジ時間 | 15,172 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
ソースコード
record Subset, sum : Int32, indices : Array(Int32)
def solve(m)
  p = Array.new(m) { gets.not_nil!.to_i }
  
  res = [] of Subset
  (0...(1 << m)).each do |bit|
    sum = 0
    indices = [] of Int32
    m.times do |i|
      if (bit >> i) & 1 == 1
        sum += p[i]
        indices << i
      end
    end
    res << Subset.new(sum, indices)
  end
  
  res
end
n, s = gets.not_nil!.split.map(&.to_i)
half = n // 2
p1 = solve(half)
p2 = solve(n - half)
# Use a hash for O(1) lookups
p2_map = Hash(Int32, Array(Array(Int32))).new { |h, k| h[k] = [] of Array(Int32) }
p2.each do |subset|
  p2_map[subset.sum] << subset.indices
end
ans = [] of Array(Int32)
p1.each do |subset1|
  target = s - subset1.sum
  next unless p2_map.has_key?(target)
  
  p2_map[target].each do |indices2|
    combined = subset1.indices.dup
    indices2.each { |x| combined << x + half }
    ans << combined
  end
end
# Sort and output
ans.sort.each do |indices|
  puts indices.map { |x| x + 1 }.join(" ")
end
            
            
            
        