結果
| 問題 | No.15 カタログショッピング | 
| コンテスト | |
| ユーザー |  codershifth | 
| 提出日時 | 2015-07-20 14:54:13 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 22 ms / 5,000 ms | 
| コード長 | 2,661 bytes | 
| コンパイル時間 | 1,949 ms | 
| コンパイル使用メモリ | 177,272 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-08 11:14:25 | 
| 合計ジャッジ時間 | 2,624 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
ソースコード
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()
using namespace std;
class CatalogueShopping {
public:
    void solve(void) {
            int N;
            ll  S;
            cin>>N>>S;
            vector<ll> P(N);
            REP(i, N)
                cin>>P[i];
            // ∑P[i] = S なる I をみつければよい
            // i in I
            //
            // S が大きすぎるので DP は無理
            // N が小さいので 2^N 全探索試すか?
            // 2^31 = 2147483648 > 10^7*200 なので無理
            // 前半・後半列挙でやる
            vector<pair<ll,int>> S1;
            int m = N;
            if (m > 16)
                m = 16;
            // O(2^17*m*log(N))
            for (int b = 0; b < (1<<m); ++b)
            {
                ll sum = 0;
                FOR(i,1,m+1) if (b & (1<<(m-i)))
                    sum += P[i-1];
                S1.emplace_back(sum, b);
            }
            sort(RANGE(S1));
            vector<ll> S2((1<<(N-m)), 0);
            // O(2^17*N)
            for (int b = 0; b < (1<<(N-m)); ++b)
            {
                FOR(i,1,N-m+1) if (b & (1<<(N-m-i)))
                    S2[b] += P[i+m-1];
            }
            // 二分探索
            // O(2^(N-m)*16)
            vector<int> combs;
            REP(b2, 1<<(N-m))
            {
                ll s2 = S2[b2];
                if (s2 == S)
                {
                    combs.push_back(b2);
                    continue;
                }
                auto it1 = lower_bound(RANGE(S1), make_pair(S-s2, -1));
                auto it2 = upper_bound(RANGE(S1), make_pair(S-s2, (1<<30)));
                if (it1 == S1.end() || it1->first != S-s2)
                    continue;
                while (it1 != it2)
                {
                    int b1;
                    ll  s1;
                    tie(s1,b1) = *it1;
                    combs.push_back(b1<<(N-m) | b2);
                    ++it1;
                }
            }
            sort(RANGE(combs), greater<int>());
            for (auto b : combs)
            {
                string ans;
                FOR(i,1,N+1) if (b & (1<<(N-i)))
                    ans+=to_string(i)+" ";
                cout<<ans.substr(0,ans.length()-1)<<endl;
            }
    }
};
#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new CatalogueShopping();
        obj->solve();
        delete obj;
        return 0;
}
#endif
            
            
            
        