結果

問題 No.15 カタログショッピング
ユーザー atn112323atn112323
提出日時 2016-05-04 22:47:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,764 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 80,880 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-04-15 11:31:20
合計ジャッジ時間 1,770 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int N;
long long S;
int P[31];
map<long long, vector<int> > first;
map<long long, vector<int> > second;

int main() {
	cin >> N >> S;
	for (int i = 0; i < N; i++) {
		cin >> P[i];
	}
	if (N <= 16) {
		for (int mask = (1 << N) - 1; mask >= 0; mask--) {
			long long sum = 0;
			for (int i = 0; i < N; i++) {
				if ((mask >> (N - i - 1)) & 1) {
					sum += P[i];
				}
			}
			if (sum == S) {
				string str;
				for (int i = 0; i < N; i++) {
					if ((mask >> (N - i - 1)) & 1) {
						str += to_string(i + 1) + " ";
					}
				}
				cout << str.substr(0, str.length() - 1) << endl;
			}
		}
	} else {
		for (int mask = (1 << 16) - 1; mask >= 0; mask--) {
			long long sum = 0;
			for (int i = 0; i < 16; i++) {
				if ((mask >> (16 - i - 1)) & 1) {
					sum += P[i];
				}
			}
			first[sum].push_back(mask);
		}
		for (int mask = (1 << (N - 16)) - 1; mask >= 0; mask--) {
			long long sum = 0;
			for (int i = 0; i < N - 16; i++) {
				if ((mask >> (N - 16 - i - 1)) & 1) {
					sum += P[i + 16];
				}
			}
			second[sum].push_back(mask);
		}
		for (const auto& itr : first) {
			if (second.find(S - itr.first) != second.end()) {
				for (const int& mask0 : itr.second) {
					string str0;
					for (int i = 0; i < 16; i++) {
						if ((mask0 >> (16 - i - 1)) & 1) {
							str0 += to_string(i + 1) + " ";
						}
					}
					for (const int& mask1 : second[S - itr.first]) {
						string str1;
						for (int i = 0; i < N - 16; i++) {
							if ((mask1 >> (N - 16 - i - 1)) & 1) {
								str1 += to_string(i + 16 + 1) + " ";
							}
						}
						string str = str0 + str1;
						cout << str.substr(0, str.length() - 1) << endl;
					}
				}
			}
		}
	}
	return 0;
}
0