UVA 11881 Internal Rate of Return(数学+二分)
In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero. Formally, given T, CF0, CF1, ..., CFT, then IRR is the solution to the following equation:
+
+ K +
= 0Your task is to find all valid IRRs. In this problem, the initial cash-flow CF0 < 0, while other cash-flows are all positive (CFi > 0 for all i = 1, 2,...).
Important: IRR can be negative, but it must be satisfied that IRR > - 1.
Input
There will be at most 25 test cases. Each test case contains two lines. The first line contains a single integer T ( 1
T
10), the number of positive cash-flows. The second line contains T + 1 integers: CF0, CF1,CF2, ..., CFT, where CF0 < 0, 0 < CFi < 10000 ( i = 1, 2,..., T). The input terminates by T = 0.
Output
For each test case, print a single line, containing the value of IRR, rounded to two decimal points. If noIRR exists, print ``No" (without quotes); if there are multiple IRRs, print ``Too many"(without quotes).
题目大意:给出CF[0]<0,CF[i]>0,i>0,求IRR(IRR>-1)令NPV = 0.
思路:设f(IRR) = NPV,这就变成了一个函数,稍微观察一下,可以发现当IRR∈(-1, +∞)的时候是单调递减的(好像是吧做完忘了),这样我们就可以二分答案0点了。当IRR无限接近-1的时候,f(IRR)→+∞(好像是吧),当IRR→+∞时,f(IRR)→CF[0]<0,令left = -1、right = 1e5(我也不知道该取什么我随便取的然后AC了),随便二分一下就好。
PS:恩?说完了?那什么时候输出No和Too many啊?关于这个坑爹的问题,看完前面的分析,笔者完全不知道什么时候会出现这两个答案,于是妥妥地没将这两个东西写进代码然后AC了。这里还有一个小技巧,题目的样例完全没有出现No和Too many这两种答案,很可能说明这两种答案都是不存在的。比如很多的题目说如果什么什么得不到答案就输出-1那样,它的样例大多都会有一个是输出-1的,当然这不是绝对的……
代码(15MS):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std; const int MAXN = ;
const double EPS = 1e-; inline int sgn(double x) {
if(fabs(x) < EPS) return ;
return x > ? : -;
} int CF[MAXN];
int T; double f(double IRR) {
double ret = CF[], tmp = + IRR;
for(int i = ; i <= T; ++i) {
ret += CF[i] / tmp;
tmp = tmp * ( + IRR);
}
return ret;
} double solve() {
double ans = -;
double L = -, R = 1e5;
while(R - L > EPS) {
double mid = (R + L) / ;
if(sgn(f(mid)) == ) L = mid;
else R = mid;
}
return ans = L;
} int main() {
while(scanf("%d", &T) != EOF) {
if(T == ) break;
for(int i = ; i <= T; ++i) scanf("%d", &CF[i]);
//double t; while(cin>>t) cout<<f(t)<<endl;
printf("%.2f\n", solve());
}
}
UVA 11881 Internal Rate of Return(数学+二分)的更多相关文章
- UVA 11881 - Internal Rate of Return - [二分]
依然是来自2017/9/17的周赛水题…… 题目链接:https://cn.vjudge.net/problem/UVA-11881 题解: 观察这个函数: 由于CF[i]固定值,因此NPV(IRR) ...
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- UVA 10668 - Expanding Rods(数学+二分)
UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- 【UVA 11865】 Stream My Contest (二分+MDST最小树形图)
[题意] 你需要花费不超过cost元来搭建一个比赛网络.网络中有n台机器,编号0~n-1,其中机器0为服务器,其他机器为客户机.一共有m条可以使用的网线,其中第i条网线的发送端是机器ui,接收端是机器 ...
- AtCoder Express(数学+二分)
D - AtCoder Express Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement In ...
- HDU 6216 A Cubic number and A Cubic Number(数学/二分查找)
题意: 给定一个素数p(p <= 1e12),问是否存在一对立方差等于p. 分析: 根据平方差公式: 因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1. 然后代入a = b + ...
- UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)
题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...
- CF 483B. Friends and Presents 数学 (二分) 难度:1
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- php中的变量作用域
<?php include_once $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php'; function totalJokes() { try{ ...
- html单选框(性别选择)
在写单选框时,如何实现只能同时只能选择一个radio. 将name设置为一样的数值:代码如下: <input class="myforms-3-2" type="r ...
- vue使用v-for循环,动态修改element-ui的el-switch
在使用element-ui的el-switch中,因为要用v-for循环,一直没有成功,后来仔细查看文档,发现可以这样写 <el-switch v-for="(item, key) i ...
- Docker 入坑教程笔记
Docker 入坑教程笔记 视频网址B站:点这里 查询命令 man docker 简单启动和退出 docker run --name [容器名] -i -t ubuntu /bin/bash 交互启动 ...
- Percona XtraDB Cluster 5.7安装配置
优点:1.准同步复制2.多个可同时读写节点,可实现写扩展,较分片方案更进一步3.自动节点管理4.数据严格一致5.服务高可用缺点:1.只支持innodb引擎2.所有表都要有主键3.所有的写操作都将发生在 ...
- HTML中的【块】与【内嵌】
块元素与内嵌元素 块的特征 默认独占一行 没有宽度时默认撑满一行 支持所有的css命令 内嵌的特征 同行可以连续跟同类的标签 内容撑开宽度 不支持宽高 不支持上下的内外边距 代码换行被解析 块与内嵌的 ...
- 二、html篇
1.<br/> 有时css实现换行比较麻烦,可以使用该标签进行换行. 2.<strong></strong> <ins></ins> & ...
- 廖老师的Python教程——Python简介
一直想了解下Python,今儿在外面办事排队的时候,打开了廖老师的官网,找到了Python教程.虽然只是一篇关于Python的简介,但是通过将Python的特性与C进行对比,很生动地归纳了Python ...
- 05 redis(进阶)
redis 阶段一.认识redis 1.什么是redis Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remo ...
- 解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误
解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误 今天安装启动nginx的时候报 ...