結果
| 問題 | No.731 等差数列がだいすき |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-09-09 14:09:58 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,500 ms |
| コード長 | 1,026 bytes |
| 記録 | |
| コンパイル時間 | 5,310 ms |
| コンパイル使用メモリ | 180,844 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-23 07:55:22 |
| 合計ジャッジ時間 | 6,395 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 |
コンパイルメッセージ
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 Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.List
import Data.Char (isSpace)
main :: IO ()
main = solve <$> readLn <*> f >>= prt
where
f = readil B.readInt <$> B.getLine
solve :: Int -> [Int] -> (Double, Double, Double)
solve n as = (b, a, c)
where
xb = fromIntegral (n - 1) / 2.0
yb = fromIntegral (sum as) / fromIntegral n
dxs = map (\x -> fromIntegral x - xb) [0 .. n-1]
dys = map (\y -> fromIntegral y - yb) as
a = (sum $ zipWith (*) dxs dys) / (sum $ map (^2) dxs)
b = yb - a * xb
f x = a * x + b
c = sum $ map (^2) $ zipWith (\u v -> u - fromIntegral v) (map (\x -> f (fromIntegral x)) [0 .. n-1]) as
prt :: (Double, Double, Double) -> IO ()
prt (a, b, c) = do
putStrLn $ show a ++ " " ++ show b
print c
readil :: Integral a => (ByteString -> Maybe (a, ByteString)) -> ByteString -> [a]
readil f = unfoldr g
where
g s = do
(n, s') <- f s
return (n, B.dropWhile isSpace s')