Poj 2601 Simple calculations

Source: Internet
Author: User

Poj 2601 Simple calculations

Simple calculations
Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:6559 Accepted:3291

Description

There is a sequence of n + 2 elements a0, a1,..., an + 1 (n <= 3000,-1000 <= ai <= 1000). It is known that Ai = (ai-1 + ai + 1)/2-ciFor each I = 1, 2,..., n.
You are given a0, an + 1, c1,..., cn. Write a program which calculates a1.

Input

The first line of an input contains an integer n. the next two lines consist of numbers a0 and an + 1 each having two digits after decimal point, and the next n lines contain numbers ci (also with two digits after decimal point ), one number per line.

Output

The output file shoshould contain a1 in the same format as a0 and an + 1.

Sample Input

150.5025.5010.15

Sample Output

27.85

Solution:

General process: a [0] + a [2]-2a [1]-2c [1] = 0a [1] + a [3]-2a [2]-2c [2] = 0 ...... A [n-1] + a [n + 1]-2a [n]-2c [n] = 0 accumulate: a [0] + a [n + 1]-a [1]-a [n]-2c [1]-2c [2]-... -2c [n] = 0 based on a [n-1] + a [n + 1]-2a [n]-2c [n] = 0 => a [n + 1]- 2c [n]-a [n] = a [n] + 2c [n]-a [n-1] simplification: a [0] + a [n]-a [1]-a [n-1]-2c [1]-2c [2]-... -Similarly, 2c [n-1] = 0: a [0] + a [n-1]-a [1]-a [N-2]-2c [1]-2c [2]-... -2c [N-2] = 0 ...... A [0] + a [2]-a [1]-a [1]-2c [1] = 0 add all the above types to obtain n * a [0] + a [n + 1]-(n + 1) * a [1]-2 * n * c [1]-2 * (n-1) * c [2]-... -2 * c [n] = 0: a [1] = (n * a [0] + a [n + 1]-2 * n * c [1]-2 * (n-1) * c [2]-... -2 * c [n])/(n + 1)
#include 
 
  #include 
  
   using namespace std;#define MAX 3005int main(){int n;double a0,an;double c[MAX];while (cin>>n){cin>>a0>>an;double ans=0;for (int i=0;i
   
    >c[i];ans+=2*(n-i)*c[i];}ans=(n*a0+an-ans)/(n+1);cout<
    
     


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.