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(精确到小数点后两位)。每一个数字占一行。

Output
输出a1 ,其格式与 a0 同样。
Sample Input
1

50.50

25.50

10.15
Sample Output
27.85

解题思路:

一開始想用递归来求,但是数据挺大的,n<=3000,所以便想找找当中的规律,看看能不能列出一个算式让a[1]用a[0],a[n+1]和c[1~n]来表示。

首先能够列出下面算式:
2 * a[1] = a[0] + a[2] - 2 * c[1]

2 * a[2] = a[1] + a[3] - 2 * c[2]
......
2 * a[n] = a[n-1] + a[n+1] - 2 * c[n]

等式两边分别相加可得:
2 * (a[1] + ... + a[n]) = (a[0] + ... + a[n-1]) + (a[2] + ... + a[n+1]) - 2 * (c[1] + ... + c[n])

移项后得:
a[1] + a[n] = a[0] + a[n+1] - 2 * (c[1] + ... + c[n])

让下标n从1~n,列出以上算式:
a[1] + a[1] = a[0] + a[2] - 2 * c[1]

a[1] + a[2] = a[0] + a[3] - 2 * (c[1] + c[2])

a[1] + a[n] = a[0] + a[n+1] - 2 * (c[1] + ... + c[n])

等式两边分别相加后得:
(n + 1) * a[1] = n * a[0] + a[n+1] - 2 * (c[1] + (c[1] + c[2]) + ... + (c[1] + c[2] + ... +c[n])) 

AC代码:

#include<stdio.h>
#define MAX_NUM 3005
int main()
{
double a[MAX_NUM], c[MAX_NUM];
int n;
scanf("%d", &n);
scanf("%lf%lf", &a[0], &a[n+1]);
for(int i = 1; i <= n; i++)
scanf("%lf", &c[i]);
int j = 1;
double sum_1 = 0, sum_2 = 0;
for(int i = 1; i <= n ; i++)
{
for(; j <= i; j++)
{
sum_1 += c[j];
}
sum_2 += sum_1;
}
a[1] = (n * a[0] + a[n + 1] - 2 * sum_2) / (n + 1);
printf("%.2lf\n", a[1]);
return 0;
}

Simple calculations的更多相关文章

  1. 【POJ】【2601】Simple calculations

    推公式/二分法 好题! 题解:http://blog.csdn.net/zck921031/article/details/7690288 这题明显是一个方程组……可以推公式推出来…… 然而这太繁琐了 ...

  2. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  3. poj 2601 Simple calculations

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

  4. uva 10014 Simple calculations

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. UVa10820 Send a Table[欧拉函数]

    Send a TableInput: Standard Input Output: Standard Output When participating in programming contests ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  8. UVA 10820 - Send a Table 数论 (欧拉函数)

    Send a Table Input: Standard Input Output: Standard Output When participating in programming contest ...

  9. uva 10820 (筛法构造欧拉函数)

    send a table When participating in programming contests, you sometimes face the following problem: Y ...

随机推荐

  1. loj2145 「SHOI2017」分手是祝愿

    记 \(f_i\) 是从要做 \(i\) 步好操作变成要做 \(i-1\) 步好操作的期望操作次数. 显然 \(f_i=i/n \times 1 + (1-i/n) \times (1 + f_{i+ ...

  2. 【Ts 3】Nginx的Http(图片)服务器配置+ftp上传使用说明

    在前两篇博客中提到了搭建Nginx和Ftp服务器,在本篇博客,主要是介绍Nginx的配置文件的使用,怎样修改配置文件使其成为一个图片服务器. 一.Nginx图片服务器配置 <span style ...

  3. API错误码设计-资料

    搜索到一篇文章:新浪微博API错误代码说明对照表 可以参考新浪微博的错误码设计思路,设计自己系统的错误码.

  4. 九度oj 题目1340:小A的计算器

    题目描述: 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示. 现在小A要在这个操作系统上实现一个计算器,这个计算器要能实 ...

  5. EasyUI 动态更新列

    function UpdateRow() { var rows = $('#tbpmgridList').datagrid('getChecked'); var productid = ''; for ...

  6. 【bzoj3956】Count 单调栈+可持久化线段树

    题目描述 输入 输出 样例输入 3 2 0 2 1 2 1 1 1 3 样例输出 0 3 题解 单调栈+可持久化线段树 本题是 bzoj4826 的弱化版(我为什么做题总喜欢先挑难的做QAQ) $k$ ...

  7. NIO Channel的学习笔记总结

    摘自:http://blog.csdn.net/tsyj810883979/article/details/6876603 1.1  非阻塞模式 Java  NIO非堵塞应用通常适用用在I/O读写等方 ...

  8. [USACO5.3]Big Barn (动态规划)

    题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N x N 的方格.输入数据中包括有树的 ...

  9. bzoj 5110 Yazid的新生舞会

    题目大意: 一个数列,求有多少个区间$[l,r]$满足该区间的众数出现次数大于$\lceil \frac{r-l}{2} \rceil$ 思路: 对于一个区间满足条件的众数明显是唯一的 所以设该数的前 ...

  10. (转)WaitForSingleObject函数的使用

    WaitForSingleObject 函数 DWORD WaitForSingleObject( HANDLE hObject, DWORD dwMilliseconds ); 第一个参数hObje ...