結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 情報学生情報学生
提出日時 2019-08-23 00:21:20
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 990 bytes
コンパイル時間 7,779 ms
コンパイル使用メモリ 168,960 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 08:40:38
合計ジャッジ時間 5,522 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns #-}
data Matrix a = Matrix !a !a !a !a

matrixMul ::  Matrix Integer -> Matrix Integer -> Integer -> Matrix Integer
matrixMul (Matrix a b c d) (Matrix a' b' c' d') n
    = Matrix ((a * a' + b * c') `mod` n) ((a * b' + b * d') `mod` n) ((c * a' + d * c') `mod` n) ((c * b' + d * d') `mod` n)

matrixPow :: Matrix Integer -> Integer -> Integer -> Matrix Integer
matrixPow _ 0 _ = Matrix 1 0 0 1
matrixPow m i n = loop m m (i - 1)
    where
        loop acc !_ 0 = acc
        loop acc m 1  = matrixMul acc m n
        loop acc m i  = case i `quotRem` 2 of
                            (j, 0) -> loop acc (matrixMul m m n) j
                            (j, _) -> loop (matrixMul acc m n) (matrixMul m m n) j

fib :: Integer -> Integer -> Integer
fib i m = let Matrix a b c d = matrixPow (Matrix 0 1 1 1) (i - 1) m
        in b

main :: IO ()
main = interact $ show . solve . map (read :: String -> Integer) . words

solve :: [Integer] -> Integer
solve [n, m] = fib n m
0