結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 情報学生情報学生
提出日時 2019-08-23 00:12:10
言語 Haskell
(9.8.2)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 897 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 167,932 KB
実行使用メモリ 17,776 KB
最終ジャッジ日時 2023-08-03 09:54:37
合計ジャッジ時間 2,736 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,080 KB
testcase_01 AC 2 ms
6,952 KB
testcase_02 AC 2 ms
7,004 KB
testcase_03 AC 3 ms
6,956 KB
testcase_04 AC 2 ms
6,956 KB
testcase_05 AC 2 ms
6,928 KB
testcase_06 AC 2 ms
7,300 KB
testcase_07 AC 2 ms
7,176 KB
testcase_08 AC 3 ms
7,328 KB
testcase_09 AC 4 ms
7,824 KB
testcase_10 AC 42 ms
10,652 KB
testcase_11 AC 202 ms
17,776 KB
testcase_12 AC 202 ms
17,732 KB
testcase_13 AC 202 ms
17,616 KB
testcase_14 AC 202 ms
17,752 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/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 :: Num a => Matrix a -> Matrix a -> Matrix a
matrixMul (Matrix a b c d) (Matrix a' b' c' d')
    = Matrix (a * a' + b * c') (a * b' + b * d') (c * a' + d * c') (c * b' + d * d')

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

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

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

solve :: [Integer] -> Integer
solve [n, m] = ( fib n ) `mod` m
0