数论 : 高精度 --- 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:

/*
                   _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;
}

  

时间: 2024-10-29 19:06:12

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

UVA - 10183 - How Many Fibs? (斐波那契 + 高精度)

题目传送:UVA - 10183 思路:高精度就可以了,因为10^100以内的斐波那契数不多,根据公式来看,估计就500多,开个1000的数组足够啦,实现的话是用的java,注意这里的斐波那契是从1开始的,我一开始是从0开始的,wa了一下 AC代码: import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String args[]) { Scanner

UVA 10183 How Many Fibs?

高精度推出大概600项fabi数,就包含了题目的数据范围,对于每组a,b,从1到600枚举res[i]即可 可以直接JAVA大数.我自己时套了C++高精度的版 JAVA 复制别人的 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); BigIn

uva 10844 - Bloques(数论+高精度)

题目链接:uva 10844 - Bloques 题目大意:给出一个n,表示有1~n这n个数,问有多少种划分子集的方法. 解题思路:递推+高精度. 1 1 2 2 3 5 5 7 10 15 15 20 27 37 52 dp[i][j]=dp[i?1][j?1]+dp[i][j?1] dp[i][0]=dp[i?1][i?1] ans[i]=dp[i][i] 需要用到高精度,并且缩进. #include <cstdio> #include <cstring> #include &

UVA10519 - !! Really Strange !!(数论+高精度)

10519 - !! Really Strange !!(数论+高精度) 题目链接 题目大意:给你n个圆,每两个圆都有相交的部分,并且相交的两个点都唯一的,不能再和别的圆交于这点.问这样在一个矩形里的相交的n个圆可以产生多少个新的封闭图形.看图会明白的. 解题思路:规律:f(n) = f(n - 1) + 2 ?(n - 1) 最后推的 f(n) = n ? (n - 1) + 2; (n >= 1), 0的时候要特判.n本身就是个大数,结果也是个大数. 代码: import java.util

递推+高精度 UVA 10497 Sweet Child Makes Trouble(可爱的孩子惹麻烦)

题目链接 题意: n个物品全部乱序排列(都不在原来的位置)的方案数. 思路: dp[i]表示i个物品都乱序排序的方案数,所以状态转移方程.考虑i-1个物品乱序,放入第i个物品一定要和i-1个的其中一个交换位置,即:考虑i-2个物品乱序,第i-1个和第i个首先在原来的位置,两种方法使得乱序,一种和第i个交换(不能和前i-2个交换,那样成dp[i-1]),还有一种是第i个先和第i-1个交换,再和前i-2个其中一个交换,即,仔细想想,这个和dp[i-1]是不同的交换方法. 另外: 还有二维的dp写法,

【数论】UVa 11526 - H(n)

What is the value this simple C++ function will return? 1 long long H(int n) 2 3 { 4 long long res = 0; 5 for( int i = 1; i <= n; i=i+1 ) 6 7 { 8 res = (res + n/i); 9 } 10 return res; 11 } Input The first line of input is an integer T (T ≤ 1000) that

【数论】UVa 10586 - Polynomial Remains

Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remainder r(x) when a(x) is divided by xk+1. The input consists of a number of cases. The first line of each case specifies the two integers n and k (0 ≤ n

【数论】UVa 11076 - Add Again

Add AgainInput: Standard Input Output: Standard Output Summation of sequence of integers is always a common problem in Computer Science. Rather than computing blindly, some intelligent techniques make the task simpler. Here you have to find the summa

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes