結果

問題 No.731 等差数列がだいすき
ユーザー simansiman
提出日時 2022-02-14 03:42:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 85 ms / 1,500 ms
コード長 553 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 11,492 KB
実行使用メモリ 16,056 KB
最終ジャッジ日時 2023-09-11 15:48:31
合計ジャッジ時間 4,312 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,416 KB
testcase_01 AC 78 ms
15,664 KB
testcase_02 AC 78 ms
15,672 KB
testcase_03 AC 80 ms
15,684 KB
testcase_04 AC 85 ms
15,724 KB
testcase_05 AC 80 ms
15,672 KB
testcase_06 AC 85 ms
15,840 KB
testcase_07 AC 81 ms
15,788 KB
testcase_08 AC 82 ms
15,708 KB
testcase_09 AC 84 ms
15,976 KB
testcase_10 AC 84 ms
15,992 KB
testcase_11 AC 78 ms
15,456 KB
testcase_12 AC 80 ms
15,752 KB
testcase_13 AC 82 ms
15,832 KB
testcase_14 AC 83 ms
15,796 KB
testcase_15 AC 84 ms
15,800 KB
testcase_16 AC 82 ms
15,732 KB
testcase_17 AC 83 ms
16,056 KB
testcase_18 AC 85 ms
15,744 KB
testcase_19 AC 84 ms
16,024 KB
testcase_20 AC 84 ms
15,796 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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