結果

問題 No.731 等差数列がだいすき
ユーザー Coffee_drunkCoffee_drunk
提出日時 2018-12-07 23:59:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 1,500 ms
コード長 877 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 8,388 KB
最終ジャッジ日時 2023-10-12 04:05:38
合計ジャッジ時間 1,680 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,780 KB
testcase_01 AC 17 ms
7,780 KB
testcase_02 AC 15 ms
7,928 KB
testcase_03 AC 15 ms
7,792 KB
testcase_04 AC 17 ms
7,948 KB
testcase_05 AC 15 ms
7,740 KB
testcase_06 AC 16 ms
8,364 KB
testcase_07 AC 15 ms
7,816 KB
testcase_08 AC 16 ms
8,388 KB
testcase_09 AC 16 ms
8,288 KB
testcase_10 AC 17 ms
8,220 KB
testcase_11 AC 16 ms
7,792 KB
testcase_12 AC 16 ms
7,808 KB
testcase_13 AC 16 ms
7,856 KB
testcase_14 AC 16 ms
7,832 KB
testcase_15 AC 17 ms
7,740 KB
testcase_16 AC 17 ms
7,780 KB
testcase_17 AC 17 ms
8,212 KB
testcase_18 AC 17 ms
8,176 KB
testcase_19 AC 17 ms
8,240 KB
testcase_20 AC 16 ms
8,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def linier_fitting(permulation):

    n = len(permulation)
    x=y=xy=xx=0

    for i in range(n):
        xy+=(i+1) * permulation[i]
        x+=(i+1)
        y+=permulation[i]
        xx+=(i+1)**2

    d1=n*xy-x*y
    n1=n*xx-x**2
    d2=xx*y-xy*x
    n2=n*xx-x**2

    cross=d1/n1
    intercept=d2/n2
    #傾きがcross,切片がintercept = 等差数列の交差がcross,初項がintercept+cross
    return (cross,intercept)

def abs_diff_pow(permulation,cross_diff,intercept):
    diff_sum=0.0
    for i in range(len(permulation)):
        diff_sum+=( permulation[i] - ((i+1)*cross_diff+intercept) )**2
    return diff_sum

num = int(input())
permulation=list(map(float,input().split()))

coeff=linier_fitting(permulation)
cross=coeff[0]
intercept=coeff[1]
init_term=cross+intercept

print(init_term,cross)
diff_sum=abs_diff_pow(permulation,cross,intercept)
print(diff_sum)
0