結果

問題 No.12 限定された素数
ユーザー 14番14番
提出日時 2016-03-31 10:22:48
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 976 ms / 5,000 ms
コード長 5,651 bytes
コンパイル時間 4,452 ms
コンパイル使用メモリ 108,088 KB
実行使用メモリ 89,380 KB
最終ジャッジ日時 2023-08-15 23:29:33
合計ジャッジ時間 26,143 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 807 ms
86,804 KB
testcase_01 AC 832 ms
85,224 KB
testcase_02 AC 804 ms
89,380 KB
testcase_03 AC 59 ms
20,840 KB
testcase_04 AC 808 ms
84,012 KB
testcase_05 AC 836 ms
83,844 KB
testcase_06 AC 855 ms
81,920 KB
testcase_07 AC 910 ms
86,132 KB
testcase_08 AC 833 ms
88,912 KB
testcase_09 AC 819 ms
84,252 KB
testcase_10 AC 846 ms
85,804 KB
testcase_11 AC 976 ms
87,384 KB
testcase_12 AC 900 ms
80,200 KB
testcase_13 AC 854 ms
81,788 KB
testcase_14 AC 839 ms
83,364 KB
testcase_15 AC 854 ms
81,304 KB
testcase_16 AC 967 ms
87,240 KB
testcase_17 AC 807 ms
86,684 KB
testcase_18 AC 812 ms
86,900 KB
testcase_19 AC 817 ms
87,988 KB
testcase_20 AC 808 ms
87,396 KB
testcase_21 AC 816 ms
86,148 KB
testcase_22 AC 817 ms
85,420 KB
testcase_23 AC 817 ms
85,832 KB
testcase_24 AC 808 ms
84,356 KB
testcase_25 AC 839 ms
83,864 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

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<int, int> countDic = new Dictionary<int, int>();
        int endIdx = 1;
        int startIdx = 0;
        int maxRangeIdxFrom = 0;
        int maxRangeIdxTo = 0;
        int maxRange = -1;
        while (startIdx < this.PrimeNumberList.Count)
        {
            countDic = new Dictionary<int, int>();
            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<this.PrimeNumberList.Count; i++) {
                if(!this.CountUp(PrimeNumberList[i], countDic)) {
                    int start = 1;
                    if(startIdx > 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<int, List<int>> charDic = new Dictionary<int, List<int>>();
    private bool CountUp(int number, Dictionary<int, int> dic) {
        List<int> charList = null;
        if(this.charDic.ContainsKey(number)) {
            charList = this.charDic[number];
        } else
        {
            charList = new List<int>();
            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<int> PrimeNumberList = new List<int>();
    
    private int MaxRange = 5000000;
    private List<int> RequireList = new List<int>();



    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<inpt.Length; i++) {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0