結果

問題 No.10 +か×か
ユーザー tentententen
提出日時 2023-08-09 18:24:31
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,004 bytes
コンパイル時間 2,921 ms
コンパイル使用メモリ 92,964 KB
実行使用メモリ 104,800 KB
最終ジャッジ日時 2024-04-27 13:09:24
合計ジャッジ時間 4,445 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,168 KB
testcase_01 AC 54 ms
50,132 KB
testcase_02 AC 55 ms
50,404 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[][] dp;
    static int[] values;
    static int[] sums;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int total = sc.nextInt();
        values = new int[n];
        sums = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            if (i == 0) {
                sums[i] = values[i];
            } else {
                sums[i] = sums[i - 1] + values[i];
            }
        }
        dp = new long[n][total + 1];
        for (long[] arr : dp) {
            Arrays.fill(arr, Long.MIN_VALUE);
        }
        long x = dfw(n - 1, total);
        String[] OPE = new String[]{"+", "*"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n - 1; i++) {
            sb.append(OPE[(int)(x % 2)]);
            x >>= 1;
        }
        System.out.println(sb.reverse());
    }
    
    static long dfw(int idx, int v) {
        if (v < 0) {
            return -1;
        }
        if (v == sums[idx]) {
            return 0;
        }
        if (idx == 0) {
            return -1;
        }
        if (dp[idx][v] == Long.MIN_VALUE) {
            long a = -1;
            if (v % values[idx] == 0) {
                a = dfw(idx - 1, v / values[idx]);
            }
            long b = dfw(idx - 1, v - values[idx]);
            if (a < 0) {
                dp[idx][v] = b * 10;
            } else {
                if (b < 0 || a < b) {
                    dp[idx][v] = a * 10 + 1;
                } else {
                    dp[idx][v] = b * 10;
                }
            }
        }
        return dp[idx][v];
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0