Round A APAC Test 2017 Problem C. Jane's Flower Shop 二分、高精度要求
来源:互联网 发布:ipad淘宝在哪里看微淘 编辑:程序博客网 时间:2024/05/16 15:58
Problem C. Jane's Flower Shop
6 points
19 points
Problem
Jane plans to open a flower shop in the local flower market. The initial cost includes the booth license, furnishings and decorations, a truck to transport flowers from the greenhouse to the shop, and so on. Jane will have to recoup these costs by earning income. She has estimated how much net income she will earn in each of the following Mmonths.
Jane wants to predict how successful her flower shop will be by calculating the IRR (Internal Rate of Return) for the M-month period. Given a series of (time, cash flow) pairs (i, Ci), the IRR is the compound interest rate that would make total cash exactly 0 at the end of the last month. The higher the IRR is, the more successful the business is. If the IRR is lower than the inflation rate, it would be wise not to start the business in the first place.
For example, suppose the initial cost is $10,000 and the shop runs for 3 months, with net incomes of $3,000, $4,000, and $5,000, respectively. Then the IRR r is given by:
In this case, there is only one rate (~=8.8963%) that satisfies the equation.
Help Jane to calculate the IRR for her business. It is guaranteed that -1 < r < 1, and there is exactly one solution in each test case.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a positive integer M: the number of months that the flower shop will be open. The next line contains M + 1 non-negative integers Ci (0 ≤ i ≤ M). Note that C0represents the initial cost, all the remaining Cis are profits, the shop will always either make a positive net profit or zero net profit in each month, and will never have negative profits.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is a floating-point number: the IRR of Jane's business. y
will be considered correct if it is within an absolute or relative error of 10-9 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 100.
C0 > 0.
0 ≤ Ci ≤ 1,000,000,000.
Small dataset
1 ≤ M ≤ 2.
Large dataset
1 ≤ M ≤ 100.
Sample
In sample case #1, the IRR is 0, Jane just paid back all the money and no interest.Sample case #2 and #3 would only appear in Large dataset.
这题精度特别高。。。题目里给的样例用double根本都得不出0来。。。要开long double
但eps只用开1e-10,原先开了1e-14wa了好久。。。
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <algorithm>#include <map>using namespace std;const long double eps = 1e-10;const int maxn = 110;int n;long double abs(long double x){ return x<0?-x:x;}long double save[maxn];long double Count(long double r){ int i,j; long double res = 0.0; for(i=0;i<=n;i++){ long double now = 1.0; for(j=1;j<=n-i;j++){ now *= (1.0+r); } res += now*save[i]; } return res;}int main(){ freopen("C-large-practice.in", "r", stdin); freopen("out.txt", "w", stdout); int t,i; scanf("%d",&t); int rnd = 1; while(t--){ scanf("%d",&n); for(i=0;i<=n;i++){ scanf("%Lf",save+i); } save[0] = -save[0]; long double left = -1.0,right = 1.0; long double middle,now; while(abs(left-right)>eps){ middle = (left+right)/2; now = Count(middle); if(now>0.0){ left = middle; }else{ right = middle; } } printf("Case #%d: %.12Lf\n",rnd++,middle); } return 0;}
- Round A APAC Test 2017 Problem C. Jane's Flower Shop 二分、高精度要求
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Round C APAC Test 2017 Problem A. Monster Path (C++)
- Round C APAC Test 2017 Problem A. Monster Path
- Round E APAC Test 2017 Problem A. Diwali lightings (C++)
- Round D APAC Test 2017 Problem A. Vote (C++)
- Round A APAC Test 2016 Problem C. gCampus 最短路
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem C. gCampus Google APAC 2016 University Test Round A
- Round B APAC Test 2017 Problem A. Sherlock and Parentheses
- Practice Round APAC test 2017--Problem A. Lazy Spelling Bee
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem B. Rain Google APAC 2017 University Test Round A
- Round A APAC Test 2017 Problem B.Rain
- Round C APAC Test 2017 Problem C. Evaluation
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Round C APAC Test 2017 Problem B. Safe Squares (C++)
- Problem A. Monster Path Google APAC 2017 University Test Round C
- eclipse 使用maven构建 springboot +swagger
- 对 面向对象 的理解
- 132. Palindrome Partitioning II
- Maven环境的配置
- 133. Clone Graph
- Round A APAC Test 2017 Problem C. Jane's Flower Shop 二分、高精度要求
- 安卓Android单表通用数据库、万能数据库的设计,(sqlite,java反射,泛型),使用非常方便
- 关于2017开始的新工作想法与一些学习进步方向还有些阶段目标
- 常用的线程池
- 编码(三):Java Web编码问题
- Android 内存泄漏之handler
- 134. Gas Station
- SLIC超像素分割算法研究(代码可下载)
- 史上最详细最全面的ss-panel部署教程(V3版本)