結果

問題 No.731 等差数列がだいすき
ユーザー minamiminami
提出日時 2019-04-04 17:08:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 134 ms / 1,500 ms
コード長 421 bytes
コンパイル時間 69 ms
コンパイル使用メモリ 10,900 KB
実行使用メモリ 29,832 KB
最終ジャッジ日時 2023-08-26 21:28:02
合計ジャッジ時間 3,705 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
29,756 KB
testcase_01 AC 126 ms
29,668 KB
testcase_02 AC 126 ms
29,652 KB
testcase_03 AC 127 ms
29,736 KB
testcase_04 AC 126 ms
29,716 KB
testcase_05 AC 126 ms
29,768 KB
testcase_06 AC 127 ms
29,760 KB
testcase_07 AC 125 ms
29,740 KB
testcase_08 AC 127 ms
29,720 KB
testcase_09 AC 126 ms
29,832 KB
testcase_10 AC 126 ms
29,780 KB
testcase_11 AC 126 ms
29,708 KB
testcase_12 AC 126 ms
29,608 KB
testcase_13 AC 125 ms
29,668 KB
testcase_14 AC 125 ms
29,724 KB
testcase_15 AC 127 ms
29,748 KB
testcase_16 AC 125 ms
29,756 KB
testcase_17 AC 125 ms
29,824 KB
testcase_18 AC 127 ms
29,804 KB
testcase_19 AC 125 ms
29,788 KB
testcase_20 AC 125 ms
29,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
N = int(input())
y = list(map(int, input().split()))
x = [i for i in range(N)]


def least_squares(x, y):
    n = len(x)
    var_x = np.var(x)
    mu_x = np.mean(x)
    mu_y = np.mean(y)
    cov = sum((x-mu_x)*(y-mu_y)) / n
    a = cov / var_x
    b = mu_y - a*mu_x
    cost = sum((_y - a*_x - b) ** 2 for _x, _y in zip(x, y))
    return a, b, cost


a, b, c = least_squares(x, y)
print(b, a)
print(c)
0