HDU-2086(推一下公式)

HDU-2086 A1 = ?
A1 = ?

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3244 Accepted Submission(s): 2054

Problem Description
有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, …. n).
若给出A0, An+1, 和 C1, C2, …..Cn.
请编程计算A1 = ?

Input
输入包括多个测试实例。
对于每个实例,首先是一个正整数n,(n <= 3000); 然后是2个数a0, an+1.接下来的n行每行有一个数ci(i = 1, ….n);输入以文件结束符结束。

Output
对于每个测试实例,用一行输出所求得的a1(保留2位小数).

Sample Input
1 50.00 25.00 10.00 2 50.00 25.00 10.00 20.00

Sample Output
27.50 15.00

这题是今天,ZW学弟问的一道题,很好啊,当时想了10分钟没找出来公式,就觉得是公式,这种类型题目几乎没坐过,弱….
补一下
由题意: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)
=> A1+A2 = A0+A3 - 2(C1+C2)
同理可得:
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)
—————————————————– 左右求和
(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)

#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define eps 1e-8
#define M_PI 3.141592653589793

typedef long long ll;
const ll mod=1000000007;
const int inf=99999999;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

using namespace std;
double c[5110],a,b;
int main()
{
    int n;
    while(~scanf("%d",&n)){
        scanf("%lf%lf",&a,&b);
        for(int i=0;i<n;i++) scanf("%lf",&c[i]);
        double sum=0.0;
        for(int i=0;i<n;i++)
            sum+=(n-i)*1.0*c[i];
        sum*=2.0;
        printf("%.2f\n",(n*1.0*a+b-sum)/((n+1)*1.0));
    }
}

你可能感兴趣的:(HDU-2086(推一下公式))