uva 10183 - How Many Fibs?(Fibonacci)
来源:互联网 发布:销售管理系统php源码 编辑:程序博客网 时间:2024/06/08 13:23
Problem B: How many Fibs?
Recall the definition of the Fibonacci numbers:
f1 := 1Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
f2 := 2
fn := fn-1 + fn-2 (n>=3)
Input Specification
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a=b=0. Otherwise, a<=b<=10100. The numbers a and b are given with no superfluous leading zeros.
Output Specification
For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.
Sample Input
10 1001234567890 98765432100 0
Sample Output
54
#include <iostream>#include <cstdio>#include <string>#include <algorithm>#include <vector>using namespace std;const int maxn = 1000;string F[maxn] , a , b;string add(string n1 , string n2){ int len1 = n1.length() , len2 = n2.length() , carry = 0; string result; for(int i = len1-1 , j = len2-1; i >= 0 || j >= 0; i-- , j--){ int sum = carry; if(i >= 0) sum += n1[i]-'0'; if(j >= 0) sum += n2[j]-'0'; carry = sum/10; sum = sum%10; result.push_back(char('0'+sum)); } if(carry) result.push_back(char('0'+carry)); reverse(result.begin() , result.end()); return result;}void Fibonacci(){ F[0] = "1"; F[1] = "2"; for(int i = 2; i < maxn; i++){ F[i] = add(F[i-1] , F[i-2]); }}bool is_larger(string n1 , string n2){ int len1 = n1.length() , len2 = n2.length(); if(len1 > len2) return true; if(len1 < len2) return false; for(int i = 0; i < len1; i++){ if(n1[i]-'0' > n2[i]-'0') return true; if(n1[i]-'0' < n2[i]-'0') return false; } return false;}int Binary_search(string num){ int l = 0 , r = 1000; while(l < r){ int mid = (l+r)/2; if(is_larger(F[mid] , num)){ r = mid; }else{ l = mid+1; } } return r;}void computing(){ int l = Binary_search(a), r = Binary_search(b)-1; if(l > 0 && F[l-1] == a){ l--; } printf("%d\n" , r-l+1);}int main(){ Fibonacci(); while(cin >> a >> b && (a != "0" || b != "0")){ computing(); } return 0;}
0 0
- uva 10183 - How Many Fibs?(Fibonacci)
- uva 10183 how many fibs
- uva 10183 How Many Fibs
- UVa 10183 - How Many Fibs?
- UVA 10183 How Many Fibs?
- UVa 10183 - How Many Fibs?
- UVa 10183 - How Many Fibs?
- UVA 10183 How Many Fibs?
- UVa:10183 How Many Fibs?
- uva 10183 How many Fibs?
- UVa Problem Solution: 10183 - How many fibs?
- How Many Fibs? - UVa 10183 大数加法
- hdu 1316 How Many Fibs?(Fibonacci+高精度+二分)
- How many fibs
- PKU2413 how many fibs
- How Many Fibs?
- UVa10183 - How Many Fibs?
- HDU1316:How Many Fibs?
- Tomcat配置之content.xml
- linux常用命令收藏
- 欧拉路径 hdu 3018 Ant Trip
- linux 之cut awk 和批量文件编码转换 shell脚本
- 滚动条下拉DIV固定在头部不动
- uva 10183 - How Many Fibs?(Fibonacci)
- ThinkPHP操作笔记(二)
- spring定时器
- 分页查询发电量评估纵向历史对比数据(项目保留)
- c# asp.net 新建项目与新建网站区别
- Flex xml编辑器(老外写的)
- springMVC传参中会出现中文乱码
- SharePoint Foundation 2010 预安装失败,KB976462安装错误
- linux透明代理可以集成squid的验证吗?(未完结)