結果

問題 No.731 等差数列がだいすき
ユーザー GrenacheGrenache
提出日時 2018-09-17 12:04:47
言語 Java19
(openjdk 21)
結果
AC  
実行時間 203 ms / 1,500 ms
コード長 1,253 bytes
コンパイル時間 4,195 ms
コンパイル使用メモリ 74,624 KB
実行使用メモリ 56,908 KB
最終ジャッジ日時 2023-09-25 09:19:59
合計ジャッジ時間 8,516 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,060 KB
testcase_01 AC 123 ms
56,140 KB
testcase_02 AC 125 ms
55,828 KB
testcase_03 AC 144 ms
56,188 KB
testcase_04 AC 184 ms
54,180 KB
testcase_05 AC 143 ms
56,148 KB
testcase_06 AC 203 ms
56,272 KB
testcase_07 AC 179 ms
56,180 KB
testcase_08 AC 191 ms
56,420 KB
testcase_09 AC 188 ms
55,984 KB
testcase_10 AC 190 ms
56,908 KB
testcase_11 AC 130 ms
56,044 KB
testcase_12 AC 157 ms
55,952 KB
testcase_13 AC 184 ms
56,048 KB
testcase_14 AC 189 ms
56,080 KB
testcase_15 AC 192 ms
55,724 KB
testcase_16 AC 183 ms
56,328 KB
testcase_17 AC 191 ms
56,420 KB
testcase_18 AC 196 ms
56,188 KB
testcase_19 AC 195 ms
56,488 KB
testcase_20 AC 194 ms
56,808 KB
権限があれば一括ダウンロードができます

ソースコード

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