結果

問題 No.10 +か×か
ユーザー core123core123
提出日時 2019-04-18 14:16:56
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,918 bytes
コンパイル時間 3,041 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 69,780 KB
最終ジャッジ日時 2023-10-22 08:12:23
合計ジャッジ時間 10,139 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
57,808 KB
testcase_01 AC 140 ms
55,408 KB
testcase_02 AC 139 ms
57,276 KB
testcase_03 AC 272 ms
66,188 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.*;
import static java.lang.Math.*;
public class Main{
	public static void main(String... args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int total=sc.nextInt();
		int[] a=new int[n];
		for(int i=0;i<n;i++){
			a[i]=sc.nextInt();
		}
		boolean[][] dp=new boolean[n][total+1];
		dp[0][a[0]]=true;
		for(int i=1;i<dp.length;i++){
			for(int j=1;j<dp[0].length;j++){
				if(j-a[i]>=0){
					if(dp[i-1][j-a[i]]){
						dp[i][j]=true;
					}
				}
				if(j%a[i]==0){
					if(dp[i-1][j/a[i]]&&!dp[i][j]){
						dp[i][j]=true;
					}
				}
			}
		}
		
		PriorityQueue<String> pq=new PriorityQueue<>();
		dfs(total,n-1,"",pq,dp,a);
		//put(list);
		put(pq.peek());
		
	}
	public static void dfs(int current,int index,String string,PriorityQueue<String> pq,boolean[][] dp,int[] a){
		//put("current:%d,index:%d,string:%s",current,index,string);
		if(index==0){
			if(current==a[0]){
				pq.add(string);
			}
			while(pq.size()>1){
				pq.poll();
			}
			return;
		}
		if(current-a[index]>=0&&dp[index-1][current-a[index]]){
			dfs(current-a[index],index-1,"+"+string,pq,dp,a);
		}
		if(current%a[index]==0&&dp[index-1][current/a[index]]){
			dfs(current/a[index],index-1,"*"+string,pq,dp,a);
		}
	}
	public static class Pair{
		final int x,y;
		Pair(int x,int y){
			this.x=x;
			this.y=y;
		}
		@Override
		public String toString(){
			return String.format("Pair(%d,%d)",x,y);
		}
	}
	public static void print(Object object){
        System.out.print(object);
    }
    public static void put(Object object) {
        System.out.println(object);
    }
    public static void put(){
        System.out.println();
    }
 
    public static void print(String format,Object... args){
        System.out.print(String.format(format,args));
    }
    public static void put(String format,Object... args) {
        System.out.println(String.format(format,args));
    }
}
0