URAL 1828. Approximation by a Progression 数学
来源:互联网 发布:手机淘宝店招怎么设置 编辑:程序博客网 时间:2024/06/05 23:46
1828. Approximation by a Progression
Time limit: 0.5 second
Memory limit: 64 MB
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
40 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 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);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;}
0 0
- URAL 1828. Approximation by a Progression 数学
- URAL - 1828 Approximation by a Progression(最小二乘法)
- Gym 100490A-A - Approximation-数学
- URAL 1762 Search for a Hiding-Place(数学·模拟)
- URAL 1113(数学)
- URAL 2067-数学
- The Approximation of One Matrix by Another of Lower Rank
- Codeforces 256A Almost Arithmetical Progression【dp】
- URAL 1820. Ural Steaks(数学啊 )
- A. Divisible by Seven----打表暴力/数学思维
- CodeForces 567C. Geometric Progression(map 数学啊)
- Codeforces Round #392 (Div. 2) F. Geometrical Progression(数学)
- 【数学】 URAL 1294 Mars Satellites
- URAL 2070 Interesting Numbers(数学)
- URAL 1032 A
- URAL 1200. Horns and Hoofs 枚举+数学
- URAL 1515. Cashmaster (数学啊 )
- URAL 2003. Simple Magic(数学啊 )
- 64位BASM学习随笔(一)
- 【三层】简单登陆
- JMS(java消息服务)
- 我要分
- 【C++11】新特性——常量表达式constexpr
- URAL 1828. Approximation by a Progression 数学
- while (cin >> word) demo
- ios-day12-01(ios开发中的数据存储的常用方式有哪些、应用沙盒目录结构、应用沙盒目录路径的常见获取方式)
- Java基础知识
- 关于网购二手电脑防骗经验分享
- maven创建可运行jar包
- string concatenation demo
- 10000 pcs free gift of chinese top grade brown film faced plywood
- 找素数