using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input3"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("1"); WillReturn.Add("11"); //3 //まず[1,11] の中に含まれる素数列は 2,3,5,7,11 である //Frankのハッシュアルゴリズムを適用すると 2,3,5,7,2 となる // ここでコリジョンが起こらない(値が重ならない) //最大の長さの素数列を取り出すと2,3,5,7と3,5,7,11 となる。 //数が大きい方を選択するので素数列の最初は3となる。 } else if (InputPattern == "Input2") { WillReturn.Add("10"); WillReturn.Add("100"); //31 //[10,100] の素数列を考えると以下の長さが最高となる //31,37,41,43,47,53→4,1,5,7,2,8 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } //No.6 使いものにならないハッシュ static void Main() { List InputList = GetInputList(); int K = int.Parse(InputList[0]); int N = int.Parse(InputList[1]); Eratosthenes(N); //素数情報のListを作成 List SosuuInfoList = DeriveSosuuInfoList(K, N); //しゃくとり法を使う int UB = SosuuInfoList.Count - 1; //開始素数の最大値[長さ]なDict var AnswerDict = new Dictionary(); int L = 0; for (int R = 0; R <= UB; R++) { int CurrHashVal = SosuuInfoList[R].HashVal; //再登場のハッシュ値の場合 int wkInd = SosuuInfoList.FindIndex(L, R - L, X => X.HashVal == CurrHashVal); if (wkInd >= 0) { L = wkInd + 1; //下限値枝切り int RestMaxCnt = UB - L + 1; if (RestMaxCnt < AnswerDict.Keys.Max()) break; } AnswerDict[R - L + 1] = SosuuInfoList[L].SosuuVal; } //foreach (var EachPair in AnswerDict) { // Console.WriteLine("AnswerDict[{0}]={1}", EachPair.Key, EachPair.Value); //} var Tmp = AnswerDict.OrderByDescending(X => X.Key).ThenByDescending(X => X.Value); Console.WriteLine(Tmp.First().Value); } struct SosuuInfoDef { internal int SosuuVal; internal int HashVal; } //素数情報のListを作成 static List DeriveSosuuInfoList(int pK, int pN) { var WillReturn = new List(); foreach (int EachInt in SosuuArr) { if (EachInt < pK) continue; if (EachInt > pN) break; SosuuInfoDef WillAdd; WillAdd.SosuuVal = EachInt; WillAdd.HashVal = DeriveHashVal(EachInt); WillReturn.Add(WillAdd); } //foreach (SosuuInfoDef EachSosuuInfo in WillReturn) { // Console.WriteLine("{0}のハッシュ値={1}", // EachSosuuInfo.SosuuVal, EachSosuuInfo.HashVal); //} return WillReturn; } static int[] SosuuArr; //エラトステネスの篩 static void Eratosthenes(int pJyougen) { var CheckArr = new System.Collections.BitArray(pJyougen + 1); for (int I = 2; I <= CheckArr.Count - 1; I++) { CheckArr[I] = true; } for (int I = 2; I <= CheckArr.Count - 1; I++) { if (I != 2 && I % 2 == 0) continue; if (CheckArr[I]) { for (int J = I * 2; J <= CheckArr.Count - 1; J += I) { CheckArr[J] = false; } } } var SosuuList = new List(); for (int I = 2; I <= CheckArr.Count - 1; I++) if (CheckArr[I]) SosuuList.Add(I); SosuuArr = SosuuList.ToArray(); } //ハッシュ値を求める static int DeriveHashVal(int pTarget) { int CurrHashVal = pTarget; while (CurrHashVal >= 10) { int CopiedVal = CurrHashVal; CurrHashVal = 0; while (CopiedVal > 0) { CurrHashVal += CopiedVal % 10; CopiedVal /= 10; } } return CurrHashVal; } }