Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
来源:互联网 发布:淘宝自创品牌 编辑:程序博客网 时间:2024/05/25 21:35
这一题一开始想的是求导找单调性,最后没发现什么就呵呵了。把IRR方程左边表达式记为f(r)。之后得知,因为方程f(r)=0在[0,1]有且只有一个解,并且在-1点处是正值,因此可以通过二分查找得出方程的根。
这一题对精度要求很高。之前二分查找中判断f(r)=0的精度设低就WA了==。如果二分查不到f(r)=0的点,也要返回当前mid值。虽然这个值确实是根,可f(mid)就是和0差得很大。并不造原因。Plus,我第一次得知还有long double 类型。o(╯□╰)o
#include<iostream>#include<stdio.h>#include<cstdio>#include<string>#include<cmath>#include<stdlib.h>#include<algorithm>#include<string.h>#include<cstring>#include<vector>#include<queue>#include<map>using namespace std;//2017 RoundA Problem C. Jane's Flower Shopint T;const int maxn=110;const double eps=1e-10;//use 1e-9 leads to WAint M;int C[maxn];double ans;long double fun(double r){ long double power[maxn]; memset(power,0,sizeof(power)); power[0]=1; for(int i=1;i<=M;i++) { power[i]=power[i-1]*(1+r); //cout<<power[i]<<endl; } long double ret=0; for(int i=0;i<M;i++) { //cout<<i<<" "<<C[M-i]<<" "<<power[i]<<" "<<C[M-i]*power[i]<<endl; ret+=C[M-i]*power[i];//pow(1+r,i); } //cout<<ret<<endl; //cout<<0<<" "<<C[0]<<" "<<power[M]<<" "<<C[0]*power[M]<<endl; ret-=C[0]*power[M];//pow(1+r,M); return ret;}double binartsearch(){ double left=-1; double right=1; double mid=(left+right)/2; while(left+eps<right) { mid=(left+right)/2; long double tmp=fun(mid); //cout<<left<<" "<<right<<" "<<mid<<" "<<tmp<<" "<<fun(0.618034)<<endl; if(abs(tmp)<1e-13)//set <eps leads to WA { return mid; } if(tmp>0) { left=mid;//+eps; } else if(tmp<0) { right=mid;//-eps; } } return mid;//there must be a solution, then return mid is fine.}int main(){ freopen("C-small-practice.in","r",stdin);// freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d",&T); for(int ca=1;ca<=T;ca++) { scanf("%d",&M); memset(C,0,sizeof(C)); ans=0; for(int i=0;i<=M;i++) { scanf("%d",&C[i]); } ans=binartsearch(); printf("Case #%d: %.12f\n",ca,ans); } return 0;}
0 0
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Round A APAC Test 2017 Problem C. Jane's Flower Shop 二分、高精度要求
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem C. gCampus Google APAC 2016 University Test Round A
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem B. Rain Google APAC 2017 University Test Round A
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem D. Soldiers Google APAC 2017 University Test Round C
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Google APAC 2017 University Test Round A
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem B. gCube Google APAC 2016 University Test Round A
- Problem D. gMatrix Google APAC 2016 University Test Round C
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem A. Country Leader Google APAC 2017 University Test Round A
- Problem A. Diwali lightings Google APAC 2017 University Test Round E
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- 请问企业要开发官网,能要源代码吗?
- 关于Tarjan(3)——离线LCA
- HDU2006 求奇数的乘积
- 输入一个字符串,计算字符串中子串出现的次数
- C++:布尔类型bool,函数重载、参数缺省值、哑元参数、内联函数、引用
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- 12.2.2
- javascript展开与收起效果
- 小白自总结【pLSA】实验详细记录
- tween.js详解
- 贪心,二分,半平面交(丛林警戒队,LA 4992)
- Jackson操作方法
- 比特币『私钥』『公钥』『钱包地址』间的关系
- 前后端数据交互ajax(Asynchronous Javascript And XML)