Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2086
I think it's good to know that mathematics is not good on the Internet ....
Because: AI = (Ai-1 + AI + 1)/2-ci,
- A1 = (A0 + A2)/2-C1;
- A2 = (A1 + A3)/2-C2 ,...
- => A1 + A2 = (A0 + A2 + A1 + A3)/2-(C1 + C2)
- 2 [(A1 + A2) + (C1 + C2)] = A0 + A2 + A1 + A3;
- A1 + A2 = A0 + A3-2 (C1 + C2 );
- => A1 + A2 = A0 + A3-2 (C1 + C2)
- Likewise:
- A1 + A1 = A0 + A2-2 (C1)
- A1 + A2 = A0 + A3-2 (C1 + C2)
- A1 + A3 = A0 + A4-2 (C1 + C2 + C3)
- A1 + A4 = A0 + A5-2 (C1 + C2 + C3 + C4)
- ...
- A1 + an = A0 + An + 1-2 (C1 + C2 +... + CN)
- ----------------------------------------------------- Sum of left and right
- (N + 1) A1 + (A2 + A3 +... + an) = na0 + (A2 + A3 +... + an) + An + 1-2 (NC1 + (n-1) C2 +... + 2cn-1 + CN)
- => (N + 1) a1 = na0 + An + 1-2 (NC1 + (n-1) C2 +... + 2cn-1 + CN)
- => A1 = [na0 + An + 1-2 (NC1 + (n-1) C2 +... + 2cn-1 + CN)]/(n + 1)
Code Attached
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 int main () 6 { 7 int i,j,n; 8 double sum; 9 while (~scanf("%d",&n))10 {11 sum=0;12 double a[3500],c[3500],d;13 scanf("%lf%lf",&a[0],&a[n+1]);14 for (i=1; i<=n; i++)15 scanf("%lf",&c[i]);16 a[1]=n*a[0]+a[n+1];17 for (i = n, j = 1; i >=1&&j <= n; j++,i--)18 sum += i*c[j];19 a[1] = (a[1] - 2*sum)/(n+1);20 //sum+=c[i];21 //cout<<a[1]<<endl;22 printf ("%.2lf\n",a[1]);23 }24 return 0;25 }