using System; using System.Collections.Generic; public class Program { public void Proc() { Reader.IsDebug = false; // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); int numCount = int.Parse(Reader.ReadLine()); this.RequireList.AddRange(Reader.GetInt()); if(this.RequireList.Count == 10) { Console.WriteLine((MaxRange - 1).ToString()); return; } this.CreatePrimeNumberList(); Dictionary countDic = new Dictionary(); int endIdx = 1; int startIdx = 0; int maxRangeIdxFrom = 0; int maxRangeIdxTo = 0; int maxRange = -1; while (startIdx < this.PrimeNumberList.Count) { countDic = new Dictionary(); if(!this.CountUp(PrimeNumberList[startIdx], countDic)) { startIdx++; continue; } endIdx = Math.Max(startIdx + 1, endIdx); if(endIdx >= this.PrimeNumberList.Count) { break; } for(int i=endIdx; i 0) { start = PrimeNumberList[startIdx - 1]+1; } int end = PrimeNumberList[i] - 1; if(maxRange < end - start) { bool hasNum = true; this.RequireList.ForEach((num)=>{ if(!countDic.ContainsKey(num)) { hasNum = false; } }); if(hasNum) { maxRangeIdxFrom = startIdx; maxRangeIdxTo = endIdx; maxRange = end - start; } } startIdx = i + 1; endIdx = startIdx; break; } else { endIdx = i; } } if(endIdx >= this.PrimeNumberList.Count - 1) { break; } } if(endIdx >= this.PrimeNumberList.Count - 1) { int start = 1; if(startIdx > 0) { start = PrimeNumberList[startIdx - 1]+1; } int end = this.MaxRange; if(maxRange < end - start) { bool hasNum = true; this.RequireList.ForEach((num)=>{ if(!countDic.ContainsKey(num)) { hasNum = false; } }); if(hasNum) { maxRangeIdxFrom = startIdx; maxRangeIdxTo = endIdx; maxRange = end - start; } } } Console.WriteLine(maxRange); // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); } private Dictionary> charDic = new Dictionary>(); private bool CountUp(int number, Dictionary dic) { List charList = null; if(this.charDic.ContainsKey(number)) { charList = this.charDic[number]; } else { charList = new List(); foreach (char c in number.ToString()) { int n = int.Parse(c.ToString()); if(!charList.Contains(n)) { charList.Add(n); } } this.charDic.Add(number, charList); } foreach (int item in charList) { if(!this.RequireList.Contains(item)) { return false; } } charList.ForEach((num)=>{ if(dic.ContainsKey(num)) { dic[num]++; } else { dic.Add(num, 1); } }); return true; } private void CreatePrimeNumberList() { bool[] flag = new bool[MaxRange + 1]; for(int i=2; i<=MaxRange; i++) { if(!flag[i]) { this.PrimeNumberList.Add(i); int maxIdx = MaxRange / i; for(int j=1; j<=maxIdx; j++) { flag[i*j] = true; } } } } private List PrimeNumberList = new List(); private int MaxRange = 5000000; private List RequireList = new List(); public class Reader { private static String InitText = @" 9 0 1 2 3 4 5 6 8 9 "; private static System.IO.StringReader sr = null; public static bool IsDebug = true; public static string ReadLine() { if(IsDebug) { if(sr == null) { sr = new System.IO.StringReader(InitText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ') { string[] inpt = ReadLine().Split(delimiter); int[] ret = new int[inpt.Length]; for(int i=0; i