How many Fibs?

Description

Recall the definition of the Fibonacci numbers:

f1 := 1

f2 := 2

fn := f

n-1

 + f

n-2

     (n>=3) 

Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].

Input

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

For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4

Mean:

给定两个整数a和b,统计区间[a,b]内有多少个斐波那契数。

analyse:

T由于这题输入的范围达到了10^100,必须要用高精度来做。

我们先把10^100以内的斐波那契数全部求出来,然后输入的sta,en后进行位置查找,最后把sta、en的位置相见就的答案。

Time complexity:O(n)

Source code:

/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.............................................
佛祖镇楼 BUG辟易
佛曰:
写字楼里写字间,写字间里程序员;
程序人员写程序,又拿程序换酒钱。
酒醒只在网上坐,酒醉还来网下眠;
酒醉酒醒日复日,网上网下年复年。
但愿老死电脑间,不愿鞠躬老板前;
奔驰宝马贵者趣,公交自行程序员。
别人笑我忒疯癫,我笑自己命太贱;
不见满街漂亮妹,哪个归得程序员?
*/ //Memory Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std; vector<string> fibs; string fibs_add(string f1,string f2)
{
string buff;
int carry=0,len1=f1.length(),len2=f2.length();
for(int i=0;i<len1;i++)
{
carry=(f1[i]-'0')+(f2[i]-'0')+carry;
char c=carry%10+'0';
carry/=10;
buff.push_back(c);
}
for(int i=len1;i<len2;i++)
{
carry=(f2[i]-'0')+carry;
char c=carry%10+'0';
carry/=10;
buff.push_back(c);
}
if(carry)
buff.push_back('1');
return buff;
} void fill_fibs()
{
string f1,f2;
f1.push_back('1');
f2.push_back('1');
fibs.push_back(f1);
fibs.push_back(f2);
while(f2.length()<105)
{
string tmp=fibs_add(f1,f2);
f1=f2;
f2=tmp;
fibs.push_back(f2);
}
fibs[0]="0";
int Size=fibs.size();
for(int i=0;i<Size;i++)
reverse(fibs[i].begin(),fibs[i].end());
} int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cout.txt","w",stdout);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.txt","w",stdout);
fill_fibs();
string sta,en;
while(cin>>sta>>en,sta[0]-'0'||en[0]-'0')
{
int i=1,ans=0;
while(1)
{
if(fibs[i].length()>sta.length()) break;
else if(fibs[i].length()==sta.length()&&fibs[i]>=sta) break;
i++;
}
while(1)
{
if(fibs[i]==en) {ans++;break;}
if(fibs[i].length() > en.length()) break;
else if(fibs[i].length() == en.length() && fibs[i]>en) break;
i++;
ans++;
}
cout<<ans<<endl;
}
return 0;
}

  

数论 - 高精度Fibonacci数 --- UVa 10183 : How Many Fibs ?的更多相关文章

  1. UVA 10183 How Many Fibs?

    高精度推出大概600项fabi数,就包含了题目的数据范围,对于每组a,b,从1到600枚举res[i]即可 可以直接JAVA大数.我自己时套了C++高精度的版 JAVA 复制别人的 import ja ...

  2. SCAU1143 多少个Fibonacci数--大菲波数【杭电-HDOJ-1715】--高精度加法--Fibonacci数---大数比较

    /*******对读者说(哈哈如果有人看的话23333)哈哈大杰是华农的19级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...

  3. 1143 多少个Fibonacci数

    时间限制:500MS  内存限制:65536K提交次数:270 通过次数:16 题型: 编程题   语言: C++;C Description 给你如下Fibonacci 数的定义: F1 = 1 F ...

  4. Colossal Fibonacci Numbers! UVA 11582 寻找循环节

    /** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...

  5. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  6. java 练手 Fibonacci数

    Problem B Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...

  7. Fibonacci数

    Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递 ...

  8. 每日一小练——高速Fibonacci数算法

    上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:高速Fibonacci数算法 内容:先说说Fibonacci数列,它的定义是数列:f1,f2....fn有例如以下规律: ...

  9. 一个小的日常实践——高速Fibonacci数算法

    上得厅堂.下得厨房.写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:高速Fibonacci数算法 内容:先说说Fibonacci数列,它的定义是数列:f1,f2....fn有例如以下规律: ...

随机推荐

  1. 一个非常棒的html5框架-ionic

    http://ionicframework.com/ 之前用过app.js,相比ionic,真是弱爆了! http://learn.ionicframework.com/formulas/ 上面的链接 ...

  2. ASP.NET MVC学习系列(二)-WebAPI请求

    继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...

  3. [Lua]50行代码的解释器,用来演示lambda calculus

    嗯,来写写经过: 在知乎上看见用Belleve牛用javascript写了一个精简的lisp解释器 => 我也想写一个,用lua写,能多简单呢? => 写了一个阉割的scheme解释器,包 ...

  4. 用Java实现约瑟夫环

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重 ...

  5. BTrace入门教程

    bin版:https://kenai.com/projects/btrace/downloads/directory/releases 源码:https://github.com/btraceio/b ...

  6. 用Jquery load text文本到網頁遇到的問題

    HTML <div id="divText"></div> Javascript $('#divText').load(fileName ,function ...

  7. Haproxy配置支持https获取用户IP地址

    global log 127.0.0.1 local0 chroot /var/lib/haproxy #chroot运行路径 pidfile /var/run/haproxy.pid #haprox ...

  8. 图解 Java IO : 二、FilenameFilter源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  9. C8051 PCA实现红外遥控接收

    这里使用的处理器是C8051F005.红外接收头接处理器引脚,中断方式接收按键数据. 一 PCA介绍 1.1 PCA 可编程计数器阵列(PCA)提供增强的定时器功能,与标准8051计数器/定时器相比, ...

  10. 让mingw gdb支持STL,并自动load .gdbinit

    环境要求:python (2.7版本可以,3.x没测过),mingw官方版(你可能已经有了),gdb2013-02-04(到这里https://code.google.com/p/qp-gcc/dow ...