E8本(数学)    次のE8本(数学)の問題へ

E8本(数学) 009 Brute Force 2


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3 11");
            WillReturn.Add("2 5 9");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 11");
            WillReturn.Add("3 1 4 5");
            //No
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int S = wkArr[1];
        int UB = S;

        int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        // 場合の数[合計]なDP表
        int[] DPArr = new int[UB + 1];
        DPArr[0] = 1;

        foreach (int EachA in AArr) {
            for (int I = UB; 0 <= I; I--) {
                if (DPArr[I] == 0) continue;

                int NewI = I + EachA;
                if (NewI > UB) continue;

                DPArr[NewI] += DPArr[I];
            }
        }

        if (DPArr[UB] > 0) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}


解説