結果

問題 No.15 カタログショッピング
ユーザー atn112323atn112323
提出日時 2016-05-04 22:53:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,468 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 78,836 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-04-15 11:31:44
合計ジャッジ時間 1,983 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 44 ms
7,040 KB
testcase_06 AC 51 ms
7,040 KB
testcase_07 AC 58 ms
7,040 KB
testcase_08 AC 60 ms
7,040 KB
testcase_09 AC 55 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

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> > M;

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 << (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];
				}
			}
			M[sum].push_back(mask);
		}
		for (int mask = (1 << 16) - 1; mask >= 0; mask--) {
			string str0;
			long long sum = 0;
			for (int i = 0; i < 16; i++) {
				if ((mask >> (16 - i - 1)) & 1) {
					str0 += to_string(i + 1) + " ";
					sum += P[i];
				}
			}
			if (M.find(S - sum) != M.end()) {
				for (const int& mask1 : M[S - sum]) {
					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