結果

問題 No.15 カタログショッピング
コンテスト
ユーザー pessimist
提出日時 2026-01-16 23:42:46
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 1,452 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,220 ms
コンパイル使用メモリ 210,044 KB
実行使用メモリ 12,136 KB
最終ジャッジ日時 2026-01-16 23:42:50
合計ジャッジ時間 2,966 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=10010;
void solve(){
    ll N,S;cin>>N>>S;
    vector<ll> P(N);
    for(auto&x:P)cin>>x;
    auto get_subset_sums=[&](const vector<ll>& A) -> vector<ll>{
        const int N=A.size();
        vector<ll> dp(1<<N);
        for(int s=1;s<(1<<N);++s){
            int last_bit=s&-s;
            int i=__lg(last_bit);
            dp[s]=dp[s^last_bit]+A[i];
        }
        return dp;
    };

    auto dpl=get_subset_sums(vector<ll>(P.begin(),P.begin()+N/2));
    auto dpr=get_subset_sums(vector<ll>(P.begin()+N/2,P.end()));

    unordered_map<ll,vector<int>> dp_memo_r;
    for(size_t s=0;s<dpr.size();++s){
        dp_memo_r[dpr[s]].emplace_back(s);
    }

    vector<vector<int>> ans;
    auto decode=[&](int s,int t){
        const int n=N/2;
        vector<int> tans;
        for(int i=0;i<n;++i){
            if(s>>i&1)tans.emplace_back(i+1);
        }
        for(int i=0;i<N-n;++i){
            if(t>>i&1)tans.emplace_back(n+i+1);
        }
        ans.emplace_back(tans);
    };
    for(size_t l=0;l<dpl.size();++l){
        const int need=S-dpl[l];
        for(auto&c:dp_memo_r[need]){
            decode(l,c);
        }
    }
    sort(ans.begin(),ans.end());
    for(auto&r:ans){
        for(auto&x:r)cout<<x<<' ';
        cout<<'\n';
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    int T=1; //cin>>T;
    while(T--)solve();
    return 0;
}
0