結果

問題 No.15 カタログショッピング
ユーザー cormorancormoran
提出日時 2016-11-08 18:19:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 5,000 ms
コード長 2,349 bytes
コンパイル時間 1,978 ms
コンパイル使用メモリ 170,948 KB
実行使用メモリ 7,004 KB
最終ジャッジ日時 2023-08-16 15:08:39
合計ジャッジ時間 2,462 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,504 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 12 ms
5,392 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 28 ms
6,960 KB
testcase_06 AC 36 ms
6,896 KB
testcase_07 AC 39 ms
6,964 KB
testcase_08 AC 34 ms
6,904 KB
testcase_09 AC 35 ms
7,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;


class Solver {
  public:
    bool solve() {
        int N; cin >> N;
        ll S; cin >> S;
        vector<ll> P(N); cin >> P;

        set<uint32_t> ans;
        map<ll, vector<uint32_t>> memo;
        
        int N1 = min(N, 15);        
        for(uint32_t bit = 0; bit < (1 << N1); bit++) {
            ll sum = 0;
            rep(j, N1) if(bit & (1 << j)) sum += P[j];
            memo[sum].push_back(bit);
        }
        /*
        for(auto &p : memo) {
            cerr << "金額 " << p.first << p.second << endl;
        }
        */
        
        int N2 = N - N1;
        for(uint32_t bit = 0; bit < (1 << N2); bit++) {
            ll sum = 0;
            rep(j, N2) if(bit & (1 << j)) sum += P[j + N1];
            ll need = S - sum;
            if(memo.count(need)) {
                for(auto &b : memo[need]) {
                    ans.insert(b | (bit << N1));
                }
            }
        }

        set<set<int>> ans_n;
        for(auto bit : ans) {
            set<int> a;
            rep(i, N) if(bit & (1 << i)) a.insert(i + 1);
            if(a.size()) ans_n.insert(a);
        }
        for(auto &s : ans_n) {            
            for(auto &i : s) {
                cout << i << (i == *s.rbegin() ? "\n" : " ");
            }
        }
        
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0