結果

問題 No.10 +か×か
ユーザー AreTrashAreTrash
提出日時 2016-04-13 16:03:40
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,921 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 108,928 KB
実行使用メモリ 25,472 KB
最終ジャッジ日時 2024-04-15 00:44:37
合計ジャッジ時間 7,773 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
18,944 KB
testcase_01 AC 25 ms
18,944 KB
testcase_02 AC 27 ms
18,944 KB
testcase_03 AC 40 ms
18,944 KB
testcase_04 AC 43 ms
19,072 KB
testcase_05 AC 26 ms
18,816 KB
testcase_06 AC 41 ms
18,816 KB
testcase_07 TLE -
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 bool flag = an.All(x => x <= 3);

        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) return false;

            if(flag){
                if(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