結果

問題 No.15 カタログショッピング
ユーザー furonfuron
提出日時 2023-05-30 19:36:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 1,982 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 144,492 KB
実行使用メモリ 11,116 KB
最終ジャッジ日時 2023-08-28 01:00:02
合計ジャッジ時間 2,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 40 ms
10,876 KB
testcase_06 AC 42 ms
11,024 KB
testcase_07 AC 43 ms
11,024 KB
testcase_08 AC 45 ms
11,116 KB
testcase_09 AC 43 ms
10,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;


int main() {
	int n, s;
	cin >> n >> s;
	vi a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	int n1 = n / 2;
	int n2 = n - n1;
	int n1max = 1 << n1;
	int n2max = 1 << n2;
	vl s1(n1max, 0), s2(n2max, 0);
	bitset<16> bs;
	for (int i = 1; i < n1max; i++) {
		bs = i;
		ll sum = 0;
		for (int i = 0; i < n1; i++) {
			if (bs[i]) sum += a[i];
		}
		s1[i] = sum;
	}
	for (int i = 1; i < n2max; i++) {
		bs = i;
		ll sum = 0;
		for (int i = 0; i < n2; i++) {
			if (bs[i]) sum += a[i + n1];
		}
		s2[i] = sum;
	}

	map<int, vi> mp;
	for (int i = 0; i < n2max; i++) {
		mp[s2[i]].push_back(i);
	}
	vvi ans;
	for (int i = 0; i < n1max; i++) {
		auto itr = mp.find(s - s1[i]);
		if (itr == mp.end()) continue;
		vi tmp;
		for (int j = 0; j < n1; j++) {
			if ((i >> j) & 1) tmp.push_back(j + 1);
		}

		for (auto& x : itr->second) {
			ans.push_back(tmp);
			for (int j = 0; j < n2; j++) {
				if ((x >> j) & 1) {
					ans.back().push_back(n1 + j + 1);
				}
			}
		}

	}

	sort(ans.begin(), ans.end());
	for (size_t y = 0; y < ans.size(); y++) {
		for (size_t x = 0; x < ans[y].size(); x++) {
			if (x > 0) cout << " ";
			cout << ans[y][x];
		}
		cout << endl;
	}

	return 0;
}
0