結果

問題 No.15 カタログショッピング
ユーザー GrenacheGrenache
提出日時 2016-06-09 20:24:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 347 ms / 5,000 ms
コード長 1,629 bytes
コンパイル時間 3,848 ms
コンパイル使用メモリ 79,348 KB
実行使用メモリ 56,452 KB
最終ジャッジ日時 2024-04-17 12:19:29
合計ジャッジ時間 6,846 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
41,268 KB
testcase_01 AC 130 ms
41,108 KB
testcase_02 AC 153 ms
42,456 KB
testcase_03 AC 205 ms
47,796 KB
testcase_04 AC 130 ms
41,484 KB
testcase_05 AC 317 ms
53,052 KB
testcase_06 AC 314 ms
53,140 KB
testcase_07 AC 347 ms
56,316 KB
testcase_08 AC 343 ms
56,404 KB
testcase_09 AC 338 ms
56,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder15 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        long s = sc.nextLong();

        long[] p = new long[n];
        for (int i = 0; i < n; i++) {
        	p[i] = sc.nextInt();
        }

        int n1 = Math.min(n, 16);
        int n2 = n - n1;

        Map<Long, List<Integer>> hm = new HashMap<>();

        for (int i = 0; i < 0x1 << n1; i++) {
        	long tmp = 0;
        	for (int j = 0; j < n1; j++) {
        		if ((i & 0x1 << j) != 0) {
        			tmp += p[j];
        		}
        	}

        	if (hm.containsKey(tmp)) {
        		hm.get(tmp).add(i);
        	} else {
        		hm.put(tmp, new ArrayList<Integer>(Arrays.asList(i)));
        	}
        }

        List<Integer> ret = new ArrayList<>();

        for (int i = 0; i < 0x1 << n2; i++) {
        	long tmp = 0;
        	for (int j = 0; j < n2; j++) {
        		if ((i & 0x1 << j) != 0) {
        			tmp += p[j + n1];
        		}
        	}

        	if (hm.containsKey(s - tmp)) {
        		for (int e : hm.get(s - tmp)) {
        			ret.add(Integer.reverse(i << n1 | e) >>> 32 - n);
        		}
        	}
        }

        Collections.sort(ret);

        for (int i = ret.size() - 1; i >= 0; i--) {
        	int e = ret.get(i);
        	for (int j = n - 1; j >= 0; j--) {
        		if ((e & 0x1 << j) != 0) {
        			System.out.print(n - j);
            		if (j > 0) {
            			System.out.print(" ");
            		}
        		}
        	}
        	System.out.println();
        }

        sc.close();
    }
}
0