Gym 100379C Fibonacci number’s ratio (the hard version) (特征方程)
来源:互联网 发布:js 全部替换 编辑:程序博客网 时间:2024/06/16 03:23
Gym 100379C
题意:
定义一个新的fibonacci数列:使G(n) = a * G(n - 1) + b * G(n - 2);
求n→∞时,G(n) / G(n-1) 的值(保留小数点后6位精度)。
思路:
分情况讨论(根据a,b,g0,g1是否为0,共计16种情况):
- a = 0 且 b = 0 时,一定是NO;
- g0 = 0 且 g1 = 0 时,一定是NO;
- a = 0 且 b != 0 时;
3.1. g1 != 0 时 , 答案为a;
3.2.其余情况NO; b = 0 且 a != 0 时;
4.1 g1,g1均不为0 时,数列为可以推出,只有g0,g1,b∗g0,b∗g1,b2∗g0,b2∗g1,b3∗g0...... b∗g0∗g0=g1∗g1 时,答案为−sqrt(b) (g1 * g0 < 0) 或sqrt(b) (g1 * g0 > 0)
4.2. 其余情况NO;其余情况用特征方程解出特征根,若无解则NO,有解则取与近似解相减绝对值较小的一个。
代码:
/** @author FreeWifi_novicer* language : C++/C*/#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<string>#include<map>#include<set>#include<vector>#include<queue>using namespace std;#define clr( x , y ) memset(x,y,sizeof(x))#define cls( x ) memset(x,0,sizeof(x))#define mp make_pair#define pb push_backtypedef long long lint;typedef long long ll;typedef long long LL;double f[105];int main(){ double a,b,g1,g0; cin >> a >> b >> g0 >> g1; if((a == 0 && b == 0) || (g1 == 0 && g0 == 0)){ cout << "NO" << endl; return 0; } if(b == 0 && a != 0){ if(g0 != 0 && g1 == 0){ puts("NO"); } else{ printf("YES\n%.10f\n",1.0 * a); } return 0; } if(a == 0 && b != 0){ if(g1 != 0 && g0 != 0 && b*g0*g0 == g1*g1){ int tmp = (g0*g1 > 0)? 1:-1; printf("YES\n%.10f\n",1.0*sqrt(b)*(double)tmp); } else{ puts("NO"); } return 0; } if(a != 0 && b != 0){ double delta = 1.0*a*a + 4.0*b; if(delta < 0){ puts("NO"); return 0; } puts("YES"); double c1 = ( (double)a + 1.0*sqrt(delta) ) / 2.0; double c2 = ( (double)a - 1.0*sqrt(delta) ) / 2.0; f[1] = g0,f[2] = g1; for(int i = 3 ; i <= 100 ; i++) f[i] = a*f[i-1] + b*f[i-2]; double c = f[30]/f[29]; if( fabs( c1 - c ) > fabs( c2 - c ) ) printf("%.10f\n",c2); else printf("%.10f\n",c1); return 0; } puts("NO"); return 0;}
0 0
- Gym 100379C Fibonacci number’s ratio (the hard version) (特征方程)
- Gym 101503C Twisting the Number(思维+枚举)
- GYM 101149 I.It's the Police(水~)
- GYM 100090 L.Hard Problem(水~)
- hdu 3509 Buge's Fibonacci Number Problem
- What's the difference between “Normal Reload”, “Hard Reload”, and “Empty Cache and Hard Reload” in c
- 287. Find the Duplicate Number #Hard
- Hard 287题 Find the Duplicate Number
- A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi
- Fibonacci Number Established the Environment of Python
- GYM 101147 C.The Wall(二分匹配-hungary)
- 特征方程
- Hard Refactoring Gym
- GYM 100801J Journey to the “The World's Start”
- 习题7-7 埃及分数(Eg[y]ptian Fractions (HARD version), Rujia Liu's Present 6, UVa 12558)
- hdu 5451 Best Solver (特征方程求通项+广义Fibonacci数列找循环节)
- Find the Kth number c++ version
- GYM 100285 E. The Emperor’s plan(组合数学+dp)
- cocos2d-x 3.4 之 文字按钮与图片按钮ZZ
- jdbc连接sqlserver
- 详细剖析asp.Net自定义404页面
- Android 蓝牙低功耗Ble Gatt浅谈
- kphp企业站快速开发之关联字段查询方法:mysql的find_in_set()函数
- Gym 100379C Fibonacci number’s ratio (the hard version) (特征方程)
- 面试之二百度
- 九度oj 1020
- 去除搜狐快站底部版权
- System V IPC
- 排序算法(七)——堆排序
- hdu 5302 Connect the Graph(构造)
- Error Adding New Node To Rac Database
- 好的商业模式就是用技术打开的想象