結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  かりあげクン | 
| 提出日時 | 2020-09-08 19:51:33 | 
| 言語 | Haskell (9.10.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,399 bytes | 
| コンパイル時間 | 9,516 ms | 
| コンパイル使用メモリ | 199,932 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-30 05:21:46 | 
| 合計ジャッジ時間 | 7,636 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
コンパイルメッセージ
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
ソースコード
import Data.Semigroup
import Data.Monoid
import qualified Control.Arrow         as Arrow
import qualified Data.Vector.Unboxed   as VU
import qualified Data.ByteString.Char8 as BSC8
-- https://blog.miz-ar.info/2019/01/fast-fibonacci/
data FibPair a = FibPair !a !a
  deriving (Eq, Show)
instance (Num a) => Semigroup (FibPair a) where
  FibPair a b <> FibPair a' b'
    = FibPair (a * b' + (b - a) * a') (a * a' + b * b')
  stimes = stimesMonoid
instance (Num a) => Monoid (FibPair a) where
  mempty = FibPair 0 1
fibOne :: (Num a) => FibPair a
fibOne = FibPair 1 1
fibPair :: Int -> FibPair Integer
fibPair i = stimesMonoid i fibOne
fastDoubling :: Int -> Int -> (Integer, Integer)
fastDoubling 0 mo = (0, 1)
fastDoubling 1 mo = (1, 1)
fastDoubling i mo
  = let (a, b) = fastDoubling (i `quot` 2) mo
        moo    = fromIntegral mo
    in if even i
       then (a * (2 * b - a) `mod` moo, (a * a + b * b) `mod` moo)
       else ((a * a + b * b) `mod` moo, b * (2 * a + b) `mod` moo)
fib :: Int -> Int -> Integer
fib i mo = case fastDoubling (i - 1) mo of
          (a, _) -> a
main :: IO ()
main = do
  (n, mo) <- parse2
  print $ fib n mo
type Parser a = BSC8.ByteString -> Maybe (a, BSC8.ByteString)
parseInt :: Parser Int
parseInt = fmap (Arrow.second BSC8.tail) . BSC8.readInt
parse2 :: IO (Int, Int)
parse2 = (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 parseInt <$> BSC8.getLine
            
            
            
        