結果

問題 No.15 カタログショッピング
ユーザー ktgktg
提出日時 2016-04-27 23:39:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 112 ms / 5,000 ms
コード長 2,698 bytes
コンパイル時間 1,286 ms
コンパイル使用メモリ 103,512 KB
実行使用メモリ 23,424 KB
最終ジャッジ日時 2024-04-15 04:36:31
合計ジャッジ時間 2,222 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 23 ms
7,680 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 108 ms
23,424 KB
testcase_06 AC 107 ms
23,296 KB
testcase_07 AC 112 ms
23,296 KB
testcase_08 AC 109 ms
23,168 KB
testcase_09 AC 106 ms
23,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <iterator>

//#define pb push_back
#define puts(x) cout << #x << " : " << x << endl;
//#pragma GCC diagnostic ignored "-Wconversion"
//#define REP(i,n) for (int i=0;i<(n);i++)
//#define REPE(i,n) for (int i=0;i<=(n);i++)
//#define init(a,b) memset((a), (b), (sizeof(a)));
//#define PI 3.14159265
//#define EPS (1e-10)
//#define EQ(a,b) (abs((a)-(b)) < EPS)

using namespace std;

typedef long long ll;
//#define int long long

int dxy[] = {0, 1, 0, -1, 0};

typedef pair<int, int> P;

int n;
ll s, p[100];

map<ll, set<ll> > fmp, smp;

void rec(int i, ll current, ll list) {
//    printf("%d\n", i);
    if(i==16||i>=n) return;
    if(current <= s){
        fmp[current].insert(list);
        rec(i + 1, current, list);
        if(current + p[i] <= s){
            fmp[current + p[i]].insert(list | (1ll<<i));
            rec(i + 1, current + p[i], list | (1ll<<i));
        }

    }
}

void rec2(int i, ll current, ll list) {
    if(i>=n) return;
    if(current <= s){
        smp[current].insert(list);
        rec2(i + 1, current, list);
        if(current + p[i] <= s){
            smp[current + p[i]].insert(list | (1ll<<i));
            rec2(i + 1, current + p[i], list | (1ll<<i));
        }
    }
}
ll bit_rev(ll src) {
    ll ret = 0;
    for(int i=0; i<64; i++) {
        if (src & (1ll<<i))
            ret |= (1ll<<(63-i));
    }
    return ret;
}
signed main() {
    cin>>n>>s;
    for(int i=0;i<n;i++) cin>>p[i];

    fmp[0].insert(0);
    smp[0].insert(0);
    rec(0, 0, 0);
    rec2(16, 0, 0);

    set< set<ll> > all;

    for(auto f : fmp){
        ll total_half = f.first;
        set<ll> sll = f.second;
        ll find_half = s - total_half;
//        printf("total_lalf = %lld\n", total_half);
//        puts(total_half);
        if(smp[find_half].empty()) continue;
        set<ll> sll2 = smp[find_half];
        for(auto k1 : sll){
            ll idx = 0;
            idx |= k1;
            for(auto k2 : sll2){
                idx |= k2;

                set<ll> ans;
                for(int i=0;i<32;i++){
                    if((1ll<<i)&idx){
                        ans.insert(i+1);
                    }
                }
                all.insert(ans);

            }
        }
//        puts(sll.size());
//        puts(sll2.size());
    }

    for( auto k : all){
        bool bo = false;
        for( auto v : k){
            if(bo)cout<<' ';
            cout<<v;
            bo = true;
        }
        cout<<endl;
    }

    return 0;
}
0