結果

問題 No.15 カタログショッピング
ユーザー chocoruskchocorusk
提出日時 2020-01-23 23:52:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,549 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 129,204 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 12:34:56
合計ジャッジ時間 1,971 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	int n;
	ll s;
	cin>>n>>s;
	ll p[33];
	for(int i=0; i<n; i++) cin>>p[i];
	if(n==1){
		if(s==p[0]) cout<<1<<endl;
		return 0;
	}
	using Pl=pair<ll, int>;
	vector<Pl> v1;
	for(int i=0; i<(1<<(n/2)); i++){
		ll t=0;
		for(int j=0; j<n/2; j++){
			if(i&(1<<j)) t+=p[j];
		}
		v1.push_back({t, i});
	}
	sort(v1.begin(), v1.end());
	vector<vector<int>> ans;
	for(int i=0; i<(1<<(n-n/2)); i++){
		ll t=0;
		for(int j=0; j<n-n/2; j++){
			if(i&(1<<j)) t+=p[n/2+j];
		}
		int k1=lower_bound(v1.begin(), v1.end(), Pl(s-t, 0))-v1.begin();
		if(k1==v1.size() || v1[k1].first!=s-t) continue;
		int k2=lower_bound(v1.begin(), v1.end(), Pl(s-t, 1e9))-v1.begin();
		for(int j=k1; j<k2; j++){
			vector<int> v;
			int x=v1[j].second;
			for(int l=0; l<n/2; l++){
				if(x&(1<<l)) v.push_back(l);
			}
			for(int l=0; l<n-n/2; l++){
				if(i&(1<<l)) v.push_back(l+n/2);
			}
			ans.push_back(v);
		}
	}
	sort(ans.begin(), ans.end());
	for(auto v:ans){
		for(auto i:v) cout<<i+1<<" ";
		cout<<endl;
	}
	return 0;
}
0