結果

問題 No.1701 half price
ユーザー tentententen
提出日時 2024-04-24 16:24:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 79 ms / 3,000 ms
コード長 1,861 bytes
コンパイル時間 2,175 ms
コンパイル使用メモリ 79,136 KB
実行使用メモリ 38,204 KB
最終ジャッジ日時 2024-04-24 16:24:18
合計ジャッジ時間 4,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
36,712 KB
testcase_01 AC 43 ms
37,120 KB
testcase_02 AC 55 ms
36,940 KB
testcase_03 AC 47 ms
37,128 KB
testcase_04 AC 54 ms
36,980 KB
testcase_05 AC 46 ms
36,856 KB
testcase_06 AC 79 ms
37,948 KB
testcase_07 AC 68 ms
38,008 KB
testcase_08 AC 45 ms
36,724 KB
testcase_09 AC 47 ms
36,960 KB
testcase_10 AC 46 ms
36,932 KB
testcase_11 AC 45 ms
36,944 KB
testcase_12 AC 45 ms
36,976 KB
testcase_13 AC 45 ms
36,844 KB
testcase_14 AC 45 ms
37,012 KB
testcase_15 AC 49 ms
36,944 KB
testcase_16 AC 47 ms
37,036 KB
testcase_17 AC 47 ms
36,900 KB
testcase_18 AC 45 ms
36,496 KB
testcase_19 AC 46 ms
37,020 KB
testcase_20 AC 44 ms
36,580 KB
testcase_21 AC 69 ms
38,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int w = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        long[] dp = new long[1 << n];
        int ans = 0;
        for (int i = 1; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp[i] = dp[i ^ (1 << j)] + values[j];
                break;
            } 
            boolean enable = dp[i] == w;
            for (int j = i; j > 0 && !enable; j = ((j - 1) & i)) {
                enable = dp[i] - dp[j] / 2 == w;
            }
            if (enable) {
                ans++;
            }
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0