結果

問題 No.573 a^2[i] = a[i]
ユーザー pekempey
提出日時 2017-10-07 01:55:35
言語 Haskell
(9.10.1)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 579 bytes
コンパイル時間 8,206 ms
コンパイル使用メモリ 172,416 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-11-17 03:16:26
合計ジャッジ時間 8,605 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 47
権限があれば一括ダウンロードができます
コンパイルメッセージ
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

main = readLn >>= print . solve

solve :: Int -> Int
solve n = fst . foldl' f (0, n) $ [1..n]
  where
    f :: (Int, Int) -> Int -> (Int, Int)
    f (s, c) i = ((s + c * (modpow i (n - i))) `mod` modulus, c `modmul` (n - i) `moddiv` (i + 1))

modulus :: Int
modulus = 10^9 + 7

modmul :: Int -> Int -> Int
modmul a b = (a * b) `mod` modulus

moddiv :: Int -> Int -> Int
moddiv a b = modmul a (modpow b $ modulus - 2)

modpow :: Int -> Int -> Int
modpow a b
  | b == 0 = 1
  | even b = modpow (modmul a a) (b `div` 2)
  | otherwise = modmul a $ modpow a (b - 1)
0