結果

問題 No.731 等差数列がだいすき
ユーザー Grenache
提出日時 2018-09-17 12:04:47
言語 Java
(openjdk 23)
結果
AC  
実行時間 188 ms / 1,500 ms
コード長 1,253 bytes
コンパイル時間 3,359 ms
コンパイル使用メモリ 78,776 KB
実行使用メモリ 43,228 KB
最終ジャッジ日時 2024-07-18 07:37:53
合計ジャッジ時間 7,601 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder731 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}

		long aa = 0, bb = 0, cc = 0, dd = 0, ee = 0, ff = 0;
		for (int i = 0; i < n; i++) {
			aa += i * i;
			bb += i;
			cc += i;
			dd += 1;
			ee += i * a[i];
			ff += a[i];
		}
		
		if (aa * dd - bb * cc == 0) {
			
		} else {
			double xx = (double)(ee * dd - bb * ff) / (aa * dd - bb * cc);
			double yy = (double)(aa * ff - cc * ee) / (aa * dd - bb * cc);
			
			double cost = 0;
			for (int i = 0; i < n; i++) {
				double tmp = xx * i + yy - a[i];
				cost += tmp * tmp;
			}
			
			pr.printf("%.7f %.7f\n", yy, xx);
			pr.printf("%.7f\n", cost);
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0