結果

問題 No.2027 (1, 2, 3, …, N) 's Subset Sum
ユーザー MAHAYANAMAHAYANA
提出日時 2023-10-26 00:36:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,003 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 177,288 KB
実行使用メモリ 814,376 KB
最終ジャッジ日時 2023-10-26 00:37:04
合計ジャッジ時間 4,483 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,m,n) for(int i=m; i<n; ++i)
#define repl(i,m,n) for(ll i=m; i<n; ++i)

int main(){
    int N; ll S;
    cin >> N >> S;

    vector<ll> A(N);
    rep(i, 0, N) A[i] = ll(i+1);

    vector<vector<bool>> dp(N+1, vector<bool>(S+1, false));
    dp[0][0] = true;

    rep(i, 0, N){
        repl(j, 0, S+1){
            if(!dp[i][j]) continue;
            dp[i+1][j] = true;
            dp[i+1][j+A[i]] = true;
        }
    }

    if(!dp[N][S]){
        cout << -1 << endl;
    }else{
        vector<int> ans;
        ll S2 = S;
        for(int i = N; i > 0; --i){
            if(dp[i-1][S2 - A[i-1]]){
                ans.push_back(A[i-1]);
                S2 -= A[i-1];
            }
        }

        int K = ans.size();
        reverse(ans.begin(), ans.end());
        
        cout << K << endl;
        rep(i, 0, K){
            cout << ans[i];
            cout << (i == K-1 ? '\n' : ' ');
        }
    }

    return 0;
}
0