結果

問題 No.12 限定された素数
ユーザー mbanmban
提出日時 2017-02-09 02:15:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 5,308 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 106,256 KB
実行使用メモリ 38,256 KB
最終ジャッジ日時 2023-08-15 23:37:51
合計ジャッジ時間 7,668 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
34,148 KB
testcase_01 AC 151 ms
34,304 KB
testcase_02 AC 151 ms
34,120 KB
testcase_03 AC 158 ms
36,272 KB
testcase_04 AC 151 ms
34,108 KB
testcase_05 AC 156 ms
34,152 KB
testcase_06 AC 157 ms
34,068 KB
testcase_07 AC 160 ms
36,356 KB
testcase_08 AC 150 ms
32,244 KB
testcase_09 AC 152 ms
36,168 KB
testcase_10 AC 154 ms
34,144 KB
testcase_11 AC 163 ms
36,140 KB
testcase_12 AC 158 ms
32,244 KB
testcase_13 AC 156 ms
34,148 KB
testcase_14 AC 152 ms
38,256 KB
testcase_15 AC 156 ms
34,108 KB
testcase_16 AC 170 ms
36,176 KB
testcase_17 AC 150 ms
36,216 KB
testcase_18 AC 148 ms
36,108 KB
testcase_19 AC 149 ms
34,156 KB
testcase_20 AC 150 ms
34,300 KB
testcase_21 AC 153 ms
34,148 KB
testcase_22 AC 149 ms
36,340 KB
testcase_23 AC 148 ms
36,200 KB
testcase_24 AC 149 ms
36,136 KB
testcase_25 AC 153 ms
36,204 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        new Magatro().Solve();
    }
}

public class Scanner
{
    private StreamReader Sr;
    private string[] S;
    private int Index;
    private const char Separator = ' ';
    public Scanner()
    {
        Index = 0;
        S = new string[0];
        Sr = new StreamReader(Console.OpenStandardInput());
    }
    private string[] Line()
    {
        return Sr.ReadLine().Split(Separator);
    }
    public string Next()
    {
        string result;
        if (Index >= S.Length)
        {
            S = Line();
            Index = 0;
        }
        result = S[Index];
        Index++;
        return result;
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public decimal NextDecimal()
    {
        return decimal.Parse(Next());
    }
    public string[] StringArray(int index = 0)
    {
        Next();
        Index = S.Length;
        return S.Skip(index).ToArray();
    }
    public int[] IntArray(int index = 0)
    {
        return StringArray(index).Select(int.Parse).ToArray();
    }
    public long[] LongArray(int index = 0)
    {
        return StringArray(index).Select(long.Parse).ToArray();
    }
    public bool EndOfStream
    {
        get { return Sr.EndOfStream; }
    }
}

public class Magatro
{
    private Scanner Cin;
    private int N;
    private bool[] A;

    private int[] Primes;
    const int Max = 5000000;
    bool[] check;
    private void Scan()
    {
        Cin = new Scanner();
        N = Cin.NextInt();
        A = new bool[10];
        for (int i = 0; i < N; i++)
        {
            A[Cin.NextInt()] = true;
        }
    }

    public void Solve()
    {
        Scan();
        Primes = SieveOfEratosthenes(Max);
        check = new bool[10];
        int length = -1;
        bool flag = false;
        for (int i = 0; i < Primes.Length;)
        {
            bool[] temp = UseNum(Primes[i]);
            bool canUse = true;
            for (int j = 0; j < 10; j++)
            {
                if (temp[j] && !A[j]) canUse = false;
            }
            if (!canUse)
            {
                i++;
                continue;
            }
            else
            {
                bool f = false;
               // Console.WriteLine(Primes[i]);
                for (int j = i; j < Primes.Length; j++)
                {
                    bool[] b = UseNum(Primes[j]);
                    for (int k = 0; k < 10; k++)
                    {
                        check[k] |= b[k];
                    }

                    bool equal = true;
                    bool bad = false;
                    for (int k = 0; k < 10; k++)
                    {
                        if (check[k] != A[k]) equal = false;
                        if (check[k] && !A[k]) bad = true;
                    }
                    if (equal)
                    {
                        int left;
                        if (i == 0)
                        {
                            left = 1;
                        }
                        else
                        {
                            left = Primes[i - 1] + 1;
                        }
                        int right;
                        if (j == Primes.Length - 1)
                        {
                            right = Max;
                        }
                        else
                        {
                            right = Primes[j + 1] - 1;
                        }
                        length = Math.Max(length, right - left);
                    }
                    if (bad)
                    {
                        i = j + 1;
                        check = new bool[10];
                        f = true;
                        break;
                    }
                    
                }
                if (!f)
                {
                    break;
                }
            }

        }
        Console.WriteLine(length);
    }
    private bool[] UseNum(int n)
    {
        bool[] result = new bool[10];
        while (n > 0)
        {
            result[n % 10] = true;
            n /= 10;
        }
        return result;
    }

    private int[] SieveOfEratosthenes(int max)
    {
        bool[] table = new bool[max + 1];
        table[0] = table[1] = true;
        int i = 2;

        for (int j = i * 2; j <= max; j += 2)
        {
            table[j] = true;
        }

        int loop = (int)Math.Sqrt(max);

        for (i = 3; i <= loop; i += 2)
        {
            if (!table[i])
            {
                for (int j = i * 2; j < max; j += i)
                {
                    table[j] = true;
                }
            }
        }

        List<int> result = new List<int>();

        for (int j = 0; j <= max; j++)
        {
            if (!table[j]) result.Add(j);
        }

        return result.ToArray();
    }
}



0