結果

問題 No.12 限定された素数
ユーザー pocaristpocarist
提出日時 2015-09-25 18:49:04
言語 F#
(F# 4.0)
結果
AC  
実行時間 1,131 ms / 5,000 ms
コード長 1,517 bytes
コンパイル時間 5,762 ms
コンパイル使用メモリ 167,352 KB
実行使用メモリ 33,936 KB
最終ジャッジ日時 2023-08-15 23:09:15
合計ジャッジ時間 32,031 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 775 ms
31,796 KB
testcase_01 AC 923 ms
31,828 KB
testcase_02 AC 844 ms
31,884 KB
testcase_03 AC 858 ms
33,884 KB
testcase_04 AC 841 ms
31,904 KB
testcase_05 AC 977 ms
31,852 KB
testcase_06 AC 973 ms
29,852 KB
testcase_07 AC 979 ms
33,816 KB
testcase_08 AC 963 ms
31,796 KB
testcase_09 AC 920 ms
31,972 KB
testcase_10 AC 958 ms
33,836 KB
testcase_11 AC 989 ms
31,920 KB
testcase_12 AC 976 ms
33,784 KB
testcase_13 AC 940 ms
33,820 KB
testcase_14 AC 962 ms
31,800 KB
testcase_15 AC 967 ms
33,804 KB
testcase_16 AC 1,059 ms
33,896 KB
testcase_17 AC 823 ms
31,864 KB
testcase_18 AC 773 ms
31,740 KB
testcase_19 AC 794 ms
33,800 KB
testcase_20 AC 885 ms
33,864 KB
testcase_21 AC 909 ms
33,936 KB
testcase_22 AC 803 ms
31,772 KB
testcase_23 AC 789 ms
31,880 KB
testcase_24 AC 908 ms
31,792 KB
testcase_25 AC 1,131 ms
33,908 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

diff #

// http://yukicoder.me/problems/34
open System

let dprintfn fmt = Printf.kprintf Diagnostics.Debug.WriteLine fmt

let primes n =
    let mid = float n |> sqrt |> floor |> int
    let table = Array.init (n+1) (fun _ -> true)
    seq {
        for i in 2 .. mid do
            if table.[i] then
                yield i
                for j in (i+i) .. i .. n do
                    table.[j] <- false
        for i in (mid+1) .. n do
            if table.[i] then
                yield i
    }

let encode x =
    let rec loop acc n =
        let m = n % 10
        let acc = Set.add m acc
        let d = n / 10
        if d = 0 then acc 
        else loop acc d
    loop Set.empty x

[<EntryPoint>]
let main argv = 
    let max_N = 5000000
    let N = Console.ReadLine().Trim() |> int
    let A = Console.ReadLine().Trim().Split([|' '|]) |> Array.map int |> Set.ofArray
    primes max_N
    |> Seq.fold (fun (start, rest, ans) x ->
                 let X = encode x
                 if Set.difference X A = Set.empty then (start, Set.difference rest X, ans)
                 else 
                    let ans =
                        if rest = Set.empty && ans < x-1-start then x-1-start else ans
                    let start = x+1
                    let rest = A
                    (start, rest, ans)
                ) (1, A ,-1)
    |> fun (start, rest, ans) ->
        let ans = 
            if rest = Set.empty && ans < max_N - start then max_N - start 
            else ans
        printfn "%d" ans
    0 
0