結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー guriceringuricerin
提出日時 2020-02-10 15:31:29
言語 F#
(F# 4.0)
結果
AC  
実行時間 249 ms / 1,000 ms
コード長 3,790 bytes
コンパイル時間 8,059 ms
コンパイル使用メモリ 212,296 KB
実行使用メモリ 40,880 KB
最終ジャッジ日時 2024-04-08 13:02:05
合計ジャッジ時間 12,696 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
31,028 KB
testcase_01 AC 63 ms
31,028 KB
testcase_02 AC 64 ms
31,028 KB
testcase_03 AC 71 ms
32,052 KB
testcase_04 AC 62 ms
31,028 KB
testcase_05 AC 200 ms
40,488 KB
testcase_06 AC 107 ms
36,276 KB
testcase_07 AC 63 ms
30,900 KB
testcase_08 AC 63 ms
30,900 KB
testcase_09 AC 62 ms
30,900 KB
testcase_10 AC 63 ms
30,900 KB
testcase_11 AC 62 ms
30,900 KB
testcase_12 AC 63 ms
31,284 KB
testcase_13 AC 63 ms
31,156 KB
testcase_14 AC 67 ms
31,156 KB
testcase_15 AC 63 ms
30,900 KB
testcase_16 AC 65 ms
31,028 KB
testcase_17 AC 63 ms
31,028 KB
testcase_18 AC 62 ms
31,028 KB
testcase_19 AC 71 ms
32,180 KB
testcase_20 AC 92 ms
34,052 KB
testcase_21 AC 79 ms
32,436 KB
testcase_22 AC 71 ms
32,180 KB
testcase_23 AC 82 ms
33,076 KB
testcase_24 AC 88 ms
34,060 KB
testcase_25 AC 113 ms
36,016 KB
testcase_26 AC 104 ms
36,016 KB
testcase_27 AC 71 ms
31,924 KB
testcase_28 AC 85 ms
33,292 KB
testcase_29 AC 108 ms
35,888 KB
testcase_30 AC 72 ms
32,052 KB
testcase_31 AC 104 ms
35,084 KB
testcase_32 AC 114 ms
35,888 KB
testcase_33 AC 242 ms
40,880 KB
testcase_34 AC 249 ms
40,880 KB
testcase_35 AC 169 ms
39,984 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (246 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
/home/judge/data/code/Main.fs(110,9): warning FS0025: この式のパターン マッチが不完全です たとえば、値 '[|_; _; _|]' はパターンに含まれないケースを示す可能性があります。 [/home/judge/data/code/main.fsproj]
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

open System
open System.Collections.Generic

[<AutoOpen>]
module Cin =
    let read f = stdin.ReadLine() |> f
    let reada f = stdin.ReadLine().Split() |> Array.map f
    let readChars() = read string |> Seq.toArray
    let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString()))

[<AutoOpen>]
module Cout =
    let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput()))
    let print (s: string) = writer.Write s
    let println (s: string) = writer.WriteLine s
    let inline puts (s: ^a) = string s |> println

// -----------------------------------------------------------------------------------------------------

module Prime =

    let inline isPrime (n: int) =
        let limit =
            n
            |> float
            |> sqrt
            |> int
        seq {
            for p in 2 .. limit do
                if n % p = 0 then yield ()
        }
        |> Seq.isEmpty

    /// nを素因数分解した結果を返す
    let primeFactors (n: int64): Map<int64, int64> =
        let limit =
            n
            |> float
            |> sqrt
            |> int64
        let rec count x p acc =
            if x % p = 0L then count (x / p) p (acc + 1L) else acc

        let mutable n = n

        let res =
            seq {
                for p in 2L .. limit + 1L do
                    let c = count n p 0L
                    if c <> 0L then
                        let div = (float p) ** (float c) |> int64
                        n <- n / div
                        yield (p, c)
            }
            |> Map.ofSeq
        if n = 1L then res else res.Add(n, 1L)

    /// nの約数の個数
    let divisorsCount (n: int64): int64 = primeFactors n |> Map.fold (fun acc k v -> acc * (v + 1L)) 1L

    /// upper以下の素数を列挙
    let sieveToUpper upper =
        seq {
            yield 2
            let knownComposites = System.Collections.Generic.SortedSet<int>()
            for i in 3 .. 2 .. upper do
                let found = knownComposites.Contains(i)
                if not found then yield i
                for j in i .. i .. upper do
                    knownComposites.Add(j) |> ignore
        }

    /// 添え字が素数かどうかを表す配列を返す
    let eraSieve upper =
        let upper = int upper + 1
        let res = Array.init upper (fun _ -> true)
        res.[0] <- false
        res.[1] <- false
        let rec loop a b =
            let c = a * b
            if c >= upper then
                ()
            else
                res.[c] <- false
                loop a (b + 1)
        for i in 2 .. upper - 1 do
            if res.[i] then loop i 2
        res

    /// nの約数を列挙(n自身を含む)
    /// O(log n)
    let divisors (n: int64): int64 array =
        let lim =
            n
            |> float
            |> sqrt
            |> int64
        seq {
            for i in 1L .. lim do
                if n % i = 0L then
                    yield i
                    if i * i <> n then yield n / i
        }
        |> Array.ofSeq
        |> Array.sort

// -----------------------------------------------------------------------------------------------------

let main() =
    let [| n; L |] = reada int
    let sieve = Prime.eraSieve L

    let rec loop i (acc: int64) =
        if i > L then
            acc
        else if sieve.[i] then
            let r = (n - 1) * i
            if r > L then
                acc
            else
                let t = int64 L - int64 r + 1L
                loop (i + 1) (acc + t)
        else
            loop (i + 1) acc

    loop 0 0L |> puts

    ()

// -----------------------------------------------------------------------------------------------------
main()
writer.Dispose()
0