結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー tottoripapertottoripaper
提出日時 2017-08-04 21:59:44
言語 Haskell
(9.10.1)
結果
WA  
実行時間 -
コード長 812 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 171,776 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-11 20:57:16
合計ジャッジ時間 3,377 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26 WA * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

import Data.List (transpose)

modulo = 10 ^ 9 + 7

period = 2 * (modulo + 1) `div` 3

fib_mat :: [[Int]]
fib_mat = [[1, 1], [1, 0]]

initial_vector :: [[Int]]
initial_vector = [[0], [1]]

multiply :: (Integral a) => [[a]] -> [[a]] -> a -> [[a]]
multiply a b mo = map (\r -> map ((`mod` mo) . sum . zipWith (*) r) b') a
  where
    b' = transpose b

expt :: [[Int]] -> Int -> Int -> [[Int]]
expt = expt' [[1, 0], [0, 1]]
  where
    expt' a _ 0 _ = a
    expt' a b n mo
      | n `mod` 2 == 1 = expt' (multiply a b mo) b' n' mo
      | otherwise = expt' a b' n' mo
      where
        n' = n `div` 2
        b' = multiply b b mo

main = do
  n <- readLn
  let ([an]:_) = multiply (expt fib_mat n period) initial_vector period
      ([bn]:_) = multiply (expt fib_mat an modulo) initial_vector modulo
  print bn
  
0