結果

問題 No.731 等差数列がだいすき
ユーザー simansiman
提出日時 2022-02-14 03:42:36
言語 Ruby
(3.4.1)
結果
AC  
実行時間 98 ms / 1,500 ms
コード長 553 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-06-29 05:54:46
合計ジャッジ時間 3,570 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
A = gets.split.map(&:to_i)

P = []

A.each_with_index do |y, x|
  P << [x, y]
end

a = Rational(
  N * P.map { |x, y| x * y }.sum - P.map(&:first).sum * P.map(&:last).sum,
  N * P.map { |x, _| x ** 2 }.sum - P.map(&:first).sum ** 2
)
b = Rational(
  P.map { |x, _| x ** 2 }.sum * P.map(&:last).sum - P.map { |x, y| x * y }.sum * P.map(&:first).sum,
  N * P.map { |x, _| x ** 2 }.sum - P.map(&:first).sum ** 2
)

puts [b.to_f.round(12), a.to_f.round(12)].join(' ')
puts A.map.with_index { |v, x| ((a * x + b) - v) ** 2 }.sum.to_f.round(12)
0