結果

問題 No.10 +か×か
ユーザー AreTrashAreTrash
提出日時 2016-04-13 15:58:18
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,795 bytes
コンパイル時間 965 ms
コンパイル使用メモリ 112,612 KB
実行使用メモリ 33,468 KB
最終ジャッジ日時 2024-04-15 00:44:28
合計ジャッジ時間 7,685 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
24,860 KB
testcase_01 AC 27 ms
22,968 KB
testcase_02 AC 28 ms
24,988 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

namespace No10_1{
    public class Program{
        public static int n = Read(Convert.ToInt32);
        public static int total = Read(Convert.ToInt32);
        public static List<int> an = ReadList(Convert.ToInt32);
        public static List<int>[] dplist = new List<int>[n + 1]; 
        public static Stack<bool> stk = new Stack<bool>();

        public static void Main(string[] args){
            for(var i = 0; i < dplist.Length; i++){
                dplist[i] = new List<int>();
            }
            an.Insert(0, 0);
            an.Add(int.MaxValue);
            Solve(true, 0, 0);

            stk.Pop();
            stk.Pop();

            var result = "";
            foreach(var s in stk){
                result += s ? "+" : "*";
            }
            Console.WriteLine(result);
            Console.ReadLine();
        }

        public static bool Solve(bool op, int sum, int cnt){
            if(sum == total && cnt == n + 1) return true;
            if(sum > total || cnt > n || dplist[cnt].Contains(sum)) return false;

            dplist[cnt].Add(sum);

            if(Solve(true, sum + an[cnt], cnt + 1)){
                stk.Push(true);
                return true;
            }
            if(Solve(false, sum * an[cnt], cnt + 1)){
                stk.Push(false);
                return true;
            }

            return false;
        }

        public static TOutput Read<TOutput>(Converter<string, TOutput> converter){
            return converter(Console.ReadLine());
        }

        public static List<TOutput> ReadList<TOutput>(Converter<string, TOutput> converter, char c = ' '){
            return Console.ReadLine()?.Split(c).ToList().ConvertAll(converter);
        }
    }
}
0