結果

問題 No.731 等差数列がだいすき
ユーザー 37zigen37zigen
提出日時 2020-03-23 03:20:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 96 ms / 1,500 ms
コード長 2,936 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 76,644 KB
実行使用メモリ 54,020 KB
最終ジャッジ日時 2023-08-27 01:20:33
合計ジャッジ時間 5,436 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
51,044 KB
testcase_01 AC 77 ms
52,856 KB
testcase_02 AC 77 ms
50,952 KB
testcase_03 AC 84 ms
53,408 KB
testcase_04 AC 84 ms
52,812 KB
testcase_05 AC 81 ms
53,712 KB
testcase_06 AC 89 ms
53,864 KB
testcase_07 AC 82 ms
53,620 KB
testcase_08 AC 86 ms
53,848 KB
testcase_09 AC 88 ms
52,628 KB
testcase_10 AC 86 ms
52,764 KB
testcase_11 AC 87 ms
52,432 KB
testcase_12 AC 81 ms
53,496 KB
testcase_13 AC 90 ms
52,576 KB
testcase_14 AC 89 ms
52,896 KB
testcase_15 AC 88 ms
52,964 KB
testcase_16 AC 83 ms
53,216 KB
testcase_17 AC 87 ms
53,728 KB
testcase_18 AC 96 ms
53,264 KB
testcase_19 AC 93 ms
54,020 KB
testcase_20 AC 94 ms
53,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	void run() {
		FastScanner sc = new FastScanner();
		int n=sc.nextInt();
		double[] A=new double[n];
		for(int i=0;i<n;++i)A[i]=Double.valueOf(sc.next());
		double x1=0,x2=0,y1=0,xy=0;
		for(int i=0;i<n;++i) {
			x1+=i;
			x2+=i*i;
			y1+=A[i];
			xy+=A[i]*i;
		}
		double a=(n*xy-x1*y1)/(n*x2-x1*x1);
		double b=(x2*y1-xy*x1)/(n*x2-x1*x1);
		double ans=0;
		for(int i=0;i<n;++i) {
			ans+=(A[i]-a*i-b)*(A[i]-a*i-b);
		}
		System.out.println(b+" "+a);
		System.out.println(ans);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    
    public int nextInt() {
    	return (int)nextLong();
    }
}
0