結果

問題 No.1861 Required Number
ユーザー tentententen
提出日時 2022-03-07 10:15:04
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,574 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 76,064 KB
実行使用メモリ 592,964 KB
最終ジャッジ日時 2023-09-29 08:38:11
合計ジャッジ時間 10,483 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,844 KB
testcase_01 AC 41 ms
49,300 KB
testcase_02 AC 41 ms
47,312 KB
testcase_03 MLE -
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 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
04_evil_1.txt -- -
04_evil_2.txt -- -
04_evil_3.txt -- -
04_evil_4.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, HashSet<Integer>>> dp = new ArrayList<>();
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        values = new int[n];
        for (int i = 0; i < n; i++) {
            dp.add(new HashMap<>());
            values[i] = sc.nextInt();
        }
        HashSet<Integer> ans = dfw(n - 1, k);
        if (ans == null) {
            System.out.println(-1);
        } else {
            System.out.println(ans.size());
        }
    }
    
    static HashSet<Integer> dfw(int idx, int count) {
        if (count == 0) {
            return new HashSet<>();
        }
        if (count < 0 || idx < 0) {
            return null;
        }
        if (!dp.get(idx).containsKey(count)) {
            HashSet<Integer> ans1 = dfw(idx - 1, count);
            HashSet<Integer> ans2 = dfw(idx - 1, count - values[idx]);
            if (ans1 == null) {
                if (ans2 == null) {
                    dp.get(idx).put(count, null);
                } else {
                    HashSet<Integer> ans = new HashSet<>();
                    ans.addAll(ans2);
                    ans.add(idx);
                    dp.get(idx).put(count, ans);
                }
            } else {
                if (ans2 == null) {
                    dp.get(idx).put(count, ans1);
                } else {
                    HashSet<Integer> ans = new HashSet<>();
                    for (int x : ans1) {
                        if (ans2.contains(x)) {
                            ans.add(x);
                        }
                    }
                    dp.get(idx).put(count, ans);
                }
            }
        }
        return dp.get(idx).get(count);
    }
}


class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0