結果

問題 No.731 等差数列がだいすき
ユーザー むらためむらため
提出日時 2019-01-27 04:11:45
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 782 bytes
コンパイル時間 4,614 ms
コンパイル使用メモリ 70,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 03:03:10
合計ジャッジ時間 4,270 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,math

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

let n = scan()
let A = newSeqWith(n,scan().float)
proc calcCost(b,d:float):float =
  for i,a in A:
    let bi = b + d * i.float
    result += (a - bi)*(a - bi)
let S = toSeq(0..<n)
let d1 = n.float * S.mapIt(it.float * A[it]).sum()
let d2 = S.sum().float * A.sum()
let d3 = n.float * S.mapIt(it * it).sum().float
let d4 = S.sum().float * S.sum().float
let d = (d1 - d2) / (d3 - d4)
let b1 = S.mapIt(it * it).sum().float * A.sum()
let b2 = S.mapIt(it.float * A[it]).sum() * S.sum().float
let b = (b1 - b2) / (d3 - d4)
echo b," ",d
echo calcCost(b,d)
0