結果
問題 | No.12 限定された素数 |
ユーザー | mban |
提出日時 | 2017-02-09 02:15:00 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 129 ms / 5,000 ms |
コード長 | 5,308 bytes |
コンパイル時間 | 909 ms |
コンパイル使用メモリ | 111,380 KB |
実行使用メモリ | 39,624 KB |
最終ジャッジ日時 | 2024-05-03 09:57:57 |
合計ジャッジ時間 | 4,750 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 108 ms
36,764 KB |
testcase_01 | AC | 107 ms
38,940 KB |
testcase_02 | AC | 107 ms
39,196 KB |
testcase_03 | AC | 116 ms
34,844 KB |
testcase_04 | AC | 108 ms
38,936 KB |
testcase_05 | AC | 112 ms
39,064 KB |
testcase_06 | AC | 112 ms
38,940 KB |
testcase_07 | AC | 112 ms
37,712 KB |
testcase_08 | AC | 109 ms
36,896 KB |
testcase_09 | AC | 108 ms
37,024 KB |
testcase_10 | AC | 112 ms
34,848 KB |
testcase_11 | AC | 125 ms
37,144 KB |
testcase_12 | AC | 119 ms
39,064 KB |
testcase_13 | AC | 116 ms
39,188 KB |
testcase_14 | AC | 115 ms
39,064 KB |
testcase_15 | AC | 115 ms
38,812 KB |
testcase_16 | AC | 129 ms
39,208 KB |
testcase_17 | AC | 109 ms
39,624 KB |
testcase_18 | AC | 111 ms
37,032 KB |
testcase_19 | AC | 112 ms
36,756 KB |
testcase_20 | AC | 108 ms
37,156 KB |
testcase_21 | AC | 107 ms
38,940 KB |
testcase_22 | AC | 106 ms
37,708 KB |
testcase_23 | AC | 107 ms
37,020 KB |
testcase_24 | AC | 110 ms
39,068 KB |
testcase_25 | AC | 111 ms
36,888 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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(); } }