UVA Simple calculations (数学推导)


 Simple
calculations
 

The Problem

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 – ci   for
each i=1, 2, ..., n. You are given a0, an+1, c1, ... , cn. Write a program which calculates a1.

The Input

The first line is the number of test cases, followed by a blank line.

For each test case, the first line of an input file 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.

Each test case will be separated by a single line.

The Output

For each test case, the output file should contain a1 in the same format
as a0 and an+1.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

1150.5025.5010.15

Sample Output

27.85



题意:给出a[0],和a[n+1],以及c[1]~c[n]的值,有公式
a[i] = (a[i-1] + a[i+1])/2 - c[i];
然后求a[1]的值。

根据公式推导:
a[1] = n*a[0] + a[n+1] -2*n*c[1] - 2*(n-1)*c[2] --->>> - 2*1*c[n];

a[1]就可以了.
代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

using namespace std;

double c[100010];

int main()
{
    int T;
    double a0,an;
    scanf("%d",&T);
    while(T--)
    {
        double n;
        scanf("%lf",&n);
        scanf("%lf",&a0);
        scanf("%lf",&an);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&c[i]);
        }
        double sum = 0;
        sum = n*a0 + an;
        for(int i=1;i<=n;i++)
        {
            sum = sum - 2.0 *(n-i+1) * c[i];
        }
        sum = sum / (n+1);
        printf("%.2lf\n",sum);
        if(T!=0)
            cout<<endl;
    }
    return 0;
}




时间: 2024-10-25 13:00:34

UVA Simple calculations (数学推导)的相关文章

leetcode 343. Integer Break(dp或数学推导)

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

HDU1719 Friend (数学推导)

friend numbers = 2^x + 3^y -1 1 #include<stdio.h> 2 int main() 3 { 4 __int64 a; 5 while(scanf("%I64d",&a)!=EOF) 6 { 7 if(!a) 8 { 9 printf("NO!\n"); 10 continue; 11 } 12 a+=1; 13 while(a%2==0||a%3==0) 14 { 15 if(a%2==0) a/=2;

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

【POJ】【2601】Simple calculations

推公式/二分法 好题! 题解:http://blog.csdn.net/zck921031/article/details/7690288 这题明显是一个方程组……可以推公式推出来…… 然而这太繁琐了!发现a[i]是满足单调性的话,我们就可以二分a[1],递推出a[n+1],进行验证…… 思维复杂度比推公式低到不知哪里去了,真是一种优秀的算法(然而我想不到,并没有什么*用……) 1 Source Code 2 Problem: 2601 User: sdfzyhy 3 Memory: 736K

poj 2601 Simple calculations

Simple calculations Time Limit: 1000MS   Memory Limit: 65536K 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 - ci f

借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5

上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.pdf https://inst.eecs.berkeley.edu/~ee227a/fa10/login/l_dual_strong.html https://inst.eecs.berkeley.edu/~ee127a/book/login/l_sdual_slater.html http://w

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪心)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

最大熵模型中的数学推导

最大熵模型中的数学推导 查看原文,点击这里 0 引言 写完SVM之后,一直想继续写机器学习的系列,无奈一直时间不稳定且对各个模型算法的理解尚不够,所以导致迟迟未动笔.无独有偶,重写KMP得益于今年4月个人组织的算法班,而动笔继续写这个机器学习系列,正得益于今年10月组织的机器学习班. 10月26日机器学习班第6次课,身为讲师之一的邹博讲最大熵模型,他从熵的概念,讲到为何要最大熵.最大熵的推导,以及求解参数的IIS方法,整个过程讲得非常流畅,特别是其中的数学推导.晚上我把他的PPT 在微博上公开分

Simple calculations

Description 有一个包含n+2个元素的数列a0, a1, ..., an+1 (n <= 3000, -1000 <= ai <=1000).它们之间满足ai = (ai-1 + ai+1)/2 - ci (i=1, 2, ..., n).现在给出a0, an+1, c1, ... , cn..请编写程序计算出a1. Input 输入的第一行是一个整数n.接下去的两行分别是a0和an+1,(精确到小数点后两位)再接下去的n行是ci(精确到小数点后两位),每个数字占一行. Out