URAL 1828. Approximation by a Progression 数学

1828. Approximation by a Progression

Time limit: 0.5 second

Memory limit: 64 MB

Your are given a sequence of integers a1, …, an.
Find an arithmetic progression b1, …, bn for which the value ∑(ai ? bi)2 is
minimal. The elements of the progression can be non-integral.

Input

The first line contains the number n of elements in the sequence (2 ≤ n ≤ 104). In the second line you are
given the integers a1, …, an; their absolute values do not exceed 104.

Output

Output two numbers separated with a space: the first term of the required arithmetic progression and its difference, with an absolute or relative error of at most 10?6.
It is guaranteed that the answer is unique for all input data.

Samples

input output
4
0 6 10 15
0.400 4.900
4
-2 -2 -2 -2
-2 0

题意 : 给定整数数组 ai。求一个等差数列bi。 等差数列bi要能使∑(ai ? bi)2 
最小的。输出 b1,d。两者可以是小数。

做法:推公式比较麻烦。步骤如下:

为方便计算 设 第零为b0,方差为d。  那么 bi=b0+i*d 。b1=b0+d。

然后带入∑(ai ? bi)2

带入 bi=b0+i*d 。得到
∑(b0^2+(i*d)^2+2*b0*i*d-2*b0*ai-2*i*d*ai+ai^2)

把b0提出来  得到∑ (b0^2+b0*(2*i*d-2*ai)+(i*d)^2-2*i*d*ai+ai^2)

把d提出来   得到∑ ( i^2*d^2+d*(2*b0*i-2*i*ai)+b0^2-2*b0*ai+ai^2)

可以发现 两个 分别关于 b0 和d 的二元一次函数都是开口向上的。所以极值落在 -b/2a上。

把∑化到括号里面。

设常数 c1 等于∑bi   ,c2 =∑(i*bi)  c3= ∑i   c4=∑i^2   ∑中i都是从1到n的累加

最后可以化简得到 b0的极值点 b0=-b/2a=(-d*c3+c1)/n,d的极值点  d==-b/2a=(-b0*c3+c2)/c4。

这两个二元一次方程。 两个式子,可以解出 d =((c1*c3)/n-c2)/(c3*c3/n-c4)   b0=(-d*c3+c1)/n

方程里a1就是等于上面的b0.

#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 he
		double c1=0;
		for(int i=1;i<=n;i++)
		{
			c1+=b[i];
		}
		//c2 ibi he
		double c2=0;
		for(int i=1;i<=n;i++)
		{
			c2+=b[i]*i;
		}

		//c3 i he
		double c3=0;
		for(int i=1;i<=n;i++)
		{
			c3+=i;
		}

		//c4 i^2 he
		double c4=0;
		for(int i=1;i<=n;i++)
		{
			c4+=i*i;
		}

		double d=1.0*((c1*c3)/n-c2)/((c3*c3)/n-c4);
		double 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;
}

时间: 2024-09-28 20:51:22

URAL 1828. Approximation by a Progression 数学的相关文章

URAL - 1828 Approximation by a Progression(最小二乘法)

Approximation by a Progression Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Your are given a sequence of integers a1, -, an. Find an arithmetic progression b1, -, bn for which the value ∑( ai ? 

URAL 1572. Yekaterinozavodsk Great Well(数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1572 About a month ago two teams of the Ural State University returned from the Yekaterinozavodsk training camp. This northern city impressed them so much that they decided to return there by any mean

URAL 1762 Search for a Hiding-Place(数学&#183;模拟)

题意  你在一个n*m个白色正方形格子组成的矩形的某个顶点格子  你沿着45度角的方向走  到了边界就改变方向90度  每次经过一个格子都改变他原来的颜(白或灰)  求你走到另一个顶点格子时矩形中有多少格子是灰色的 这题可以用公式也可以直接模拟  模拟就是一直向右移n-1位 每次超过边界就可以把边界向右移m-1位  用cnt记录超过边界的次数 那么每次都会经过cnt个已经走过的格子 (每个格子最多经过2次)  答案也就减去2*cnt    直到刚好到达边界 #include <bits/stdc

URAL 1294. Mars Satellites(余弦定理 数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1294 1294. Mars Satellites Time limit: 1.0 second Memory limit: 64 MB Four artificial satellites travel in one plane along the areostationary orbit around Mars. They have code names A, B, C and D and

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

CodeForces 567C. Geometric Progression(map 数学啊)

题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarp loves geometric progressions very much. Since he was on

URAL 2003. Simple Magic(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2003 2003. Simple Magic Time limit: 1.0 second Memory limit: 64 MB Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful garden