bzoj3095--数学题

题目大意:
给定一个长度为n的整数序列x[i],确定一个二元组(b, k)使得S=Σ(k*i+b- x[i])^2(i∈[0,n-1])最小

看Claris大神的题解就行了。实际上就是用2次二次函数的性质。

http://www.cnblogs.com/clrs97/p/4703437.html

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 inline char Nc(){
 6     static char buf[100000],*p1=buf,*p2=buf;
 7     if(p1==p2){
 8         p2=(p1=buf)+fread(buf,1,100000,stdin);
 9         if(p1==p2)return EOF;
10     }
11     return *p1++;
12 }
13 inline void Read(int& x){
14     char c=Nc(),b=1;
15     for(;c<'0'||c>'9';c=Nc())if(c=='-')b=-1;
16     for(x=0;c>='0'&&c<='9';x=x*10+c-48,c=Nc());x*=b;
17 }
18 int i,n,x;
19 double k,b,A,B,C,D;
20 int main()
21 {
22     Read(n);
23     for(i=0;i<n;i++){
24         Read(x);
25         A+=(double)i*i;B+=(double)i;C+=(double)i*x;D+=x;
26     }
27     k=(n*C-B*D)/(n*A-B*B);
28     b=(D-k*B)/n;
29     printf("%.7lf %.7lf",b,k);
30     return 0;
31 }
bzoj3095
原文地址:https://www.cnblogs.com/gjghfd/p/6223994.html