結果

問題 No.12 限定された素数
ユーザー femtofemto
提出日時 2015-04-18 18:32:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 642 ms / 5,000 ms
コード長 5,560 bytes
コンパイル時間 5,877 ms
コンパイル使用メモリ 110,428 KB
実行使用メモリ 37,272 KB
最終ジャッジ日時 2023-08-15 22:58:30
合計ジャッジ時間 21,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 556 ms
35,208 KB
testcase_01 AC 528 ms
33,096 KB
testcase_02 AC 484 ms
37,272 KB
testcase_03 AC 529 ms
35,808 KB
testcase_04 AC 503 ms
37,108 KB
testcase_05 AC 571 ms
37,264 KB
testcase_06 AC 606 ms
37,176 KB
testcase_07 AC 563 ms
33,040 KB
testcase_08 AC 540 ms
37,152 KB
testcase_09 AC 514 ms
37,180 KB
testcase_10 AC 530 ms
37,176 KB
testcase_11 AC 574 ms
35,048 KB
testcase_12 AC 636 ms
35,004 KB
testcase_13 AC 604 ms
35,152 KB
testcase_14 AC 624 ms
35,108 KB
testcase_15 AC 628 ms
35,320 KB
testcase_16 AC 642 ms
35,260 KB
testcase_17 AC 621 ms
37,172 KB
testcase_18 AC 510 ms
35,212 KB
testcase_19 AC 510 ms
35,136 KB
testcase_20 AC 515 ms
35,124 KB
testcase_21 AC 537 ms
37,108 KB
testcase_22 AC 502 ms
37,104 KB
testcase_23 AC 530 ms
34,988 KB
testcase_24 AC 572 ms
37,100 KB
testcase_25 AC 523 ms
37,140 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.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using Enu = System.Linq.Enumerable;

class Solution {
    int N;
    int [] A, used, nowused;
    void calc() {
        N = ri();
        A = ria(N);

        var primes = sieveOfEratosthenes(5000000).ToArray();

        used = new int [10];
        nowused = new int [10];
        foreach (var i in A) used [i]++;

        int head = 0, tail = 0, ans = 0;
        while (tail <= primes.Length) {
            // 足らない
            if (used.Zip(nowused, (u, nu) => u > 0 && nu == 0).Any(b => b)) {
                if (tail == primes.Length) break;
                add(primes [tail]);
                tail++;
            }
            // 余分
            else if (used.Zip(nowused, (u, nu) => u == 0 && nu > 0).Any(b => b)) {
                sub(primes [head]);
                head++;
            }
            else {
                int min = head - 1 >= 0 ? primes [head - 1] + 1 : 1;
                int max = tail < primes.Length ? primes [tail] - 1 : 5000000;
                ans = Math.Max(ans, max - min);
                if (tail == primes.Length) break;
                add(primes [tail]);
                tail++;
            }
        }

        Console.WriteLine(ans == 0 ? -1 : ans);
    }

    public static List<int> sieveOfEratosthenes(int n) {
        List<int> primes = new List<int>();
        bool [] isPrime = Enumerable.Repeat(true, n + 1).ToArray();
        isPrime [0] = isPrime [1] = false;
        for (int i = 2; i <= n; i++) {
            if (isPrime [i]) {
                primes.Add(i);
                for (int j = 2 * i; j <= n; j += i) isPrime [j] = false;
            }
        }
        return primes;
    }

    public void add(int n) {
        while (n != 0) {
            nowused [n % 10]++;
            n /= 10;
        }
    }

    public void sub(int n) {
        while (n != 0) {
            nowused [n % 10]--;
            n /= 10;
        }
    }

    public int convert(int [] a) {
        int used = 0;
        for (int i = 0; i < a.Length; i++) {
            if (a [i] > 0) used &= 1 << i;
        }
        return used;
    }

    static void Main(string [] args) {
        new Solution().calc();
    }

    #region
    static int ri() { return int.Parse(Console.ReadLine()); }
    static int [] ria(int n) {
        if (n <= 0) { Console.ReadLine(); return new int [0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
    }
    static void rio(out int p1) { p1 = ri(); }
    static void rio(out int p1, out int p2) { var r = ria(2); p1 = r [0]; p2 = r [1]; }
    static void rio(out int p1, out int p2, out int p3) { var r = ria(3); p1 = r [0]; p2 = r [1]; p3 = r [2]; }
    static void rio(out int p1, out int p2, out int p3, out int p4) { var r = ria(4); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; }
    static void rio(out int p1, out int p2, out int p3, out int p4, out int p5) { var r = ria(5); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; p5 = r [4]; }
    static long rl() { return long.Parse(Console.ReadLine()); }
    static long [] rla(int n) {
        if (n <= 0) { Console.ReadLine(); return new long [0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
    }
    static void rlo(out long p1) { p1 = rl(); }
    static void rlo(out long p1, out long p2) { var r = rla(2); p1 = r [0]; p2 = r [1]; }
    static void rlo(out long p1, out long p2, out long p3) { var r = rla(3); p1 = r [0]; p2 = r [1]; p3 = r [2]; }
    static void rlo(out long p1, out long p2, out long p3, out long p4) { var r = rla(4); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; }
    static void rlo(out long p1, out long p2, out long p3, out long p4, out long p5) { var r = rla(5); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; p5 = r [4]; }
    static double rd() { return double.Parse(Console.ReadLine()); }
    static double [] rda(int n) {
        if (n <= 0) { Console.ReadLine(); return new double [0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray();
    }
    static void rdo(out double p1) { p1 = rd(); }
    static void rdo(out double p1, out double p2) { var r = rda(2); p1 = r [0]; p2 = r [1]; }
    static void rdo(out double p1, out double p2, out double p3) { var r = rda(3); p1 = r [0]; p2 = r [1]; p3 = r [2]; }
    static void rdo(out double p1, out double p2, out double p3, out double p4) { var r = rda(4); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; }
    static void rdo(out double p1, out double p2, out double p3, out double p4, out double p5) { var r = rda(5); p1 = r [0]; p2 = r [1]; p3 = r [2]; p4 = r [3]; p5 = r [4]; }
    static void swap<T>(ref T x, ref T y) { T temp = x; x = y; y = temp; }
    static void wa1<T>(T [] a) { Debug.WriteLine(string.Join(" ", a)); }
    static void wa2<T>(T [] [] a) {
        foreach (var row in a) {
            Debug.WriteLine(String.Join(" ", row));
        }
    }
    [DebuggerDisplay("{x} , {y}")]
    class point {
        public int x, y;
        public point(int x, int y) {
            this.x = x; this.y = y;
        }
    }
    #endregion
}

static class Extention {
    public static T [] [] ToJagArray<T>(this T [,] a) {
        int n = a.GetLength(0), m = a.GetLength(1);
        var ret = new T [n] [];
        for (int i = 0; i < n; i++) {
            ret [i] = new T [m];
            for (int j = 0; j < m; j++) {
                ret [i] [j] = a [i, j];
            }
        }
        return ret;
    }
}

0