結果
| 問題 |
No.891 隣接3項間の漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-16 01:21:50 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 897 bytes |
| コンパイル時間 | 1,333 ms |
| コンパイル使用メモリ | 172,416 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-20 20:13:51 |
| 合計ジャッジ時間 | 2,363 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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
cache <- newIORef (c, 1)
x <- newIORef (1, 0)
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)