結果
| 問題 | 
                            No.891 隣接3項間の漸化式
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-10-16 01:20:49 | 
| 言語 | Haskell  (9.10.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 928 bytes | 
| コンパイル時間 | 2,063 ms | 
| コンパイル使用メモリ | 172,032 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-20 20:13:37 | 
| 合計ジャッジ時間 | 3,447 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 39 | 
コンパイルメッセージ
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
ソースコード
{-# LANGUAGE NumericUnderscores #-}
import Control.Monad
import Control.Monad.Fix
import Data.Bool
import Data.IORef
main :: IO ()
main = do
  [a, b, n] <- map read . words <$> getLine
  let c   = bool ((a + 1_000_000_007) `div` 2) (a `div` 2)  (even a)
      det = (c * c `mod` 1_000_000_007 + b) `mod` 1_000_000_007
      alp = (c, 1)
      xp  = (1, 0)
  x     <- newIORef xp
  cache <- newIORef alp
  flip fix n $ \loop i -> do
    when (odd i) $ do
      ca <- readIORef cache
      modifyIORef' x (\i -> mul i ca det)
    modifyIORef' cache (\i -> mul i i det)
    when (i > 0) $ loop (i `div` 2)
  (_, q) <- readIORef x
  print q
mul :: (Int, Int) -> (Int, Int) -> Int -> (Int, Int)
mul (a, b) (c, d) det =
  let p = (a * c `mod` 1_000_000_007 + det * (b * d `mod` 1_000_000_007) `mod` 1_000_000_007) `mod` 1_000_000_007
      q = (a * d `mod` 1_000_000_007 + b * c `mod` 1_000_000_007) `mod` 1_000_000_007
  in (p, q)