URAL 1828. Approximation by a progression math

Source: Internet
Author: User

1828. Approximation by a progressiontime limit:0.5 second
Memory limit:64 MB
Your is given a sequence of integersa1, ...,an . Find an arithmetic progressionb1, ...,bN for which the Value∑ (ai ?bI )2is minimal. The elements of the progression can be non-integral. Inputthe First line contains the numberNof elements in the sequence (2≤N≤104). The second line is given the integersa1, ...,an ; Their absolute values do not exceed 104. Outputoutput-numbers separated with a space:the first term of the required arithmetic progression and its difference, With an absolute or relative error of in most 10? 6. It is guaranteed, the answer is unique for all input data. Samples
input Output
40 6 10 15
0.400 4.900
4-2-2-2-2
-2 0





Test instructions: Given an array of integers AI. Ask for a arithmetic progression bi. Arithmetic progression bi to be able to make∑ (ai  ? bI )2the smallest. Output b1,d. The two can be decimals.
Practice: Push formula is more troublesome. The steps are as follows:
for convenient calculation, the 0th is B0 and the variance is D. So bi=b0+i*d. B1=b0+d.
and bring in∑(ai  ? bI )2
brought into the bi=b0+i*d. Get ∑ (b0^2+ (i*d) ^2+2*b0*i*d-2*b0*ai-2*i*d*ai+ai^2)
Put B0 forward to get∑(b0^2+b0* (2*i*d-2*ai)+ (i*d) ^2-2*i*d*ai+ai^2)
Put d forward to get ∑ (i^2*d^2+d* (2*b0*i-2*i*ai)+b0^2-2*b0*ai+ai^2)
It can be found that two of the two elements, respectively, about B0 and D are open-ended functions. So the Extremum falls on the-b/2a.

Put the ∑ inside the brackets.
Set constant C1 equals ∑bi, C2 =∑ (i*bi) c3=∑i c4=∑i^2∑ I are cumulative from 1 to n


Finally, we can get the extremum point of the B0 b0=-b/2a= (-D*C3+C1)/n,d. d==-b/2a=(-B0*C3+C2)/c4. These two two-yuan-a-time equations. Two formulas, can be solved d = ((C1*C3)/n-c2)/(C3*C3/N-C4) b0= (-D*C3+C1)/n

The A1 in the equation is equal to the b0 above.




#include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include < Iostream> #include <algorithm>using namespace std; #include <vector> int B[11000];int main () {double n;while (scanf ("%lf", &n)!=eof) {for (int i=1;i<=n;i++) { Cin>>b[i];} C1 bi hedouble c1=0;for (int i=1;i<=n;i++) {c1+=b[i];} C2 ibi hedouble c2=0;for (int i=1;i<=n;i++) {c2+=b[i]*i;} C3 I hedouble c3=0;for (int i=1;i<=n;i++) {c3+=i;} C4 i^2 hedouble c4=0;for (int i=1;i<=n;i++) {c4+=i*i;} Double d=1.0* ((C1*C3)/N-C2)/((C3*C3)/n-c4);d ouble a=1.0* (-D*C3+C1)/n;a+=d;if (d<0.000001&&d>- 0.000001) d=0;printf ("%lf%lf\n", a,d);} return 0;}











URAL 1828. Approximation by a progression math

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.