数论 - 高精度Fibonacci数 --- UVa 10183 : How Many Fibs ?

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:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
                   _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;
}

  

posted @   北岛知寒  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩