結果

問題 No.15 カタログショッピング
ユーザー btkbtk
提出日時 2015-05-05 00:27:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,302 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 102,384 KB
実行使用メモリ 6,936 KB
最終ジャッジ日時 2023-09-19 06:56:38
合計ジャッジ時間 2,060 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 24 ms
6,920 KB
testcase_06 AC 30 ms
6,912 KB
testcase_07 AC 32 ms
6,904 KB
testcase_08 AC 32 ms
6,896 KB
testcase_09 AC 30 ms
6,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
vector<int> ans;
#define LL long long
map < LL, list<int>> memo;
int N, half;
LL S;
LL P[40];
void left(int n,LL val,int bit){
	if (n == half){
		memo[val].push_back(bit);
	}
	else{
		left(n + 1, val, bit);
		left(n + 1, val + P[n], bit + (1 << (N - 1 - n)));
	}
}

void right(int n, LL val, int bit){
	if (n == N){
		if (memo.count(S - val) > 0){
			for (auto &b : memo[S - val])
				ans.push_back(b + bit);
		}
	}
	else{
		right(n + 1, val, bit);
		right(n + 1, val + P[n], bit + (1 << (N - 1 - n)));
	}

}

int main(void){
	ans.reserve(50);
	cin >> N >> S;
	for (int i = 0; i < N; i++)
	{
		cin >> P[i];
	}
	half = N / 2;
	left(0, 0, 0);
	right(half, 0, 0);
	sort(ans.begin(), ans.end(), greater<int>());
	for (auto &bit : ans){
		list<int> tmp;
		for (int i = N - 1; i >= 0; i--)
			if (bit&(1 << i))tmp.push_back(N - i);
		for (auto it = tmp.begin(); it != tmp.end(); ++it){
			if (it != tmp.begin())cout << " ";
			cout << *it;
		}
		cout << endl;
	}

	return 0;
}
0