using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input5"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("1"); //1 } else if (InputPattern == "Input2") { WillReturn.Add("2"); //1.16667 } else if (InputPattern == "Input3") { WillReturn.Add("3"); //1.36111 } else if (InputPattern == "Input4") { WillReturn.Add("7"); //2.52163 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } //DP表の構造体 struct DPInfoDef { internal int DiceCnt; //ダイスの数 internal int SumNum; //ダイスの目の和 internal decimal Kakuritu; //状態遷移する確率 } //No.65 回数の期待値の練習 static void Main() { List InputList = GetInputList(); int K = int.Parse(InputList[0]); var DPInfoList = new List(); Action MergeAct = (pDiceCnt, pSumNum, pKakuritu) => { DPInfoDef WillMerge; WillMerge.DiceCnt = pDiceCnt; WillMerge.SumNum = pSumNum; WillMerge.Kakuritu = pKakuritu; //{DiceCnt,SumNum}をキーとして、確率の加法定理を使う int wkInd = DPInfoList.FindIndex(X => X.DiceCnt == pDiceCnt && X.SumNum == pSumNum); if (wkInd >= 0) { WillMerge.Kakuritu += DPInfoList[wkInd].Kakuritu; DPInfoList.RemoveAt(wkInd); } DPInfoList.Add(WillMerge); }; //初期化 MergeAct(0, 0, 1M); for (int I = 0; I <= DPInfoList.Count - 1; I++) { if (DPInfoList[I].SumNum >= K) continue; for (int Dice = 1; Dice <= 6; Dice++) { //確率の乗法定理でDP表を更新 MergeAct(DPInfoList[I].DiceCnt + 1, DPInfoList[I].SumNum + Dice, DPInfoList[I].Kakuritu / 6M); } } DPInfoList.RemoveAll(X => X.SumNum < K); Console.WriteLine(DPInfoList.Sum(X => X.DiceCnt * X.Kakuritu)); } }