結果

問題 No.731 等差数列がだいすき
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-14 10:19:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 251 ms / 1,500 ms
コード長 775 bytes
コンパイル時間 4,147 ms
コンパイル使用メモリ 81,108 KB
実行使用メモリ 60,948 KB
最終ジャッジ日時 2023-09-19 04:51:22
合計ジャッジ時間 9,699 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 169 ms
56,732 KB
testcase_01 AC 170 ms
56,620 KB
testcase_02 AC 170 ms
56,528 KB
testcase_03 AC 184 ms
56,760 KB
testcase_04 AC 233 ms
57,272 KB
testcase_05 AC 182 ms
56,872 KB
testcase_06 AC 251 ms
58,944 KB
testcase_07 AC 204 ms
57,088 KB
testcase_08 AC 241 ms
58,160 KB
testcase_09 AC 243 ms
57,732 KB
testcase_10 AC 239 ms
57,848 KB
testcase_11 AC 172 ms
54,536 KB
testcase_12 AC 194 ms
56,852 KB
testcase_13 AC 238 ms
57,708 KB
testcase_14 AC 224 ms
57,112 KB
testcase_15 AC 234 ms
58,712 KB
testcase_16 AC 233 ms
58,540 KB
testcase_17 AC 229 ms
59,844 KB
testcase_18 AC 242 ms
60,948 KB
testcase_19 AC 246 ms
60,120 KB
testcase_20 AC 224 ms
59,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] a = new long[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextLong();
    }
    long xy = 0;
    long x = 0;
    long y = 0;
    long x2 = 0;
    for(int i = 0; i < n; i++) {
      xy += (i * a[i]);
      x += i;
      y += a[i];
      x2 += (i * i);
    }
    double A = (double)(n * xy - x * y) / (double)(n * x2 - x * x);
    double B = (double)(x2 * y - xy * x) / (double)(n * x2 - x * x);
    double c = 0;
    for(int i = 0; i < n; i++) {
      double t = A * (double)i + B;
      c += ((double)((a[i] - t) * (a[i] - t))); 
    }
    System.out.println(B + " " + A);
    System.out.println(c);
  }
}
0