結果

問題 No.731 等差数列がだいすき
ユーザー ducktailducktail
提出日時 2018-09-09 14:09:58
言語 Haskell
(9.8.2)
結果
AC  
実行時間 4 ms / 1,500 ms
コード長 1,026 bytes
コンパイル時間 6,823 ms
コンパイル使用メモリ 161,284 KB
実行使用メモリ 8,352 KB
最終ジャッジ日時 2023-08-24 21:46:46
合計ジャッジ時間 3,229 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,780 KB
testcase_01 AC 3 ms
7,752 KB
testcase_02 AC 3 ms
7,812 KB
testcase_03 AC 3 ms
7,920 KB
testcase_04 AC 3 ms
8,088 KB
testcase_05 AC 3 ms
7,924 KB
testcase_06 AC 3 ms
8,168 KB
testcase_07 AC 3 ms
8,024 KB
testcase_08 AC 3 ms
8,052 KB
testcase_09 AC 3 ms
8,192 KB
testcase_10 AC 3 ms
8,248 KB
testcase_11 AC 2 ms
7,956 KB
testcase_12 AC 3 ms
8,000 KB
testcase_13 AC 3 ms
8,184 KB
testcase_14 AC 3 ms
8,096 KB
testcase_15 AC 3 ms
8,148 KB
testcase_16 AC 3 ms
8,004 KB
testcase_17 AC 4 ms
8,248 KB
testcase_18 AC 3 ms
8,208 KB
testcase_19 AC 4 ms
8,352 KB
testcase_20 AC 4 ms
8,212 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

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')
0