UVa 10183 - How Many Fibs?

/*
数学:Fibonacci数列 + 大数模板
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

#define MAX_N 1000 //最大位数
//大数
struct bign {
    char s[MAX_N];
    int len;
    bign() {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    bign(int num) {
        *this = num;
    }
    bign(const char *num) {
        *this = num;
    }
    bign operator = (const char *num) {
        len = strlen(num);
        for(int i=0; i<len && num[len-1-i]>='0' && num[len-1-i]<='9'; i++) {
            s[i] = num[len-1-i] - '0';
        }
        while(len > 1 && s[len-1] == 0) len--;
        return *this;
    }
    bign operator = (int num) {
        char s[MAX_N];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator + (const bign &b) const
    {
        bign c;
        int m = len > b.len ? len : b.len;
        int i, g, sum;
        for(i=0, g=0; g || i< m; i++) {
            sum = g;
            if(i < len) sum += s[i];
            if(i < b.len) sum += b.s[i];
            c.s[i] = sum%10;
            g = sum / 10;
        }
        c.len = i;
        return c;
    }

    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }

    bign operator * (const bign &b) const
    {
        bign c;
        int i, j, g, sum;
        for(i=0; i<b.len; i++) {
            g = 0;
            for(j=0; g||j<len; j++) {
                sum = c.s[i+j] + g;
                if(j < len) sum += b.s[i]*s[j];
                c.s[j+i] = sum % 10;
                if(c.s[j+i] > 0) c.len = i+j+1;
                g = sum / 10;
            }
        }
        return c;
    }

    bign operator *= (const bign &b)
    {
        *this = *this * b;
        return *this;
    }

    bign operator / (const int b) const
    {
        bign c;
        long long a=0;
        int i=0;
        char r[1000];
        int j = 0;
        bool start = false;
        for(i=len-1; i>=0; i--) {
            a = a*10 + s[i];
            if(!start && (a/b!=0)) start = true;
            if(start) {
                r[j++] = a/b;
            }
            a = a%b;
        }
        if(start) {
            c.len = j;
            for(i=j-1; i>=0; i--) {
                c.s[j-1-i] = r[i];
            }
            return c;
        } else {
            return 0;
        }
    }

    bign operator /= (const int b)
    {
        *this = *this / b;
        return *this;
    }

    int operator % (const int b)
    {
        long long a=0;
        int i=0;
        for(i=len-1; i>=0; i--) {
            a = a*10 + s[i];
            a = a%b;
        }
        return (int)a;
    }


    bool operator < (const bign &b) const
    {
        if(b.len != len) return len < b.len;
        int i;
        for(i=len-1; i>=0; i--) {
            if(s[i] != b.s[i]) return s[i] < b.s[i];
        }
        return false;
    }


    bool operator > (const bign &b) const { return b < *this;}


    bool operator <= (const bign &b) const {return !(*this > b);}


    bool operator >= (const bign &b) const {return !(*this < b);}


    bool operator != (const bign &b) const {return (*this < b) || (*this > b);}


    bool operator == (const bign &b) const {return !(*this < b) && !(*this > b);}

};
ostream& operator << (ostream& outstream, const bign& b)
{
    for(int i=b.len-1; i>=0; i--) {
        outstream << (int)b.s[i] << "";
    }
    return outstream;
}

istream& operator >> (istream& instream, bign& b)
{
    string s;
    instream >> s;
    b = s.c_str();
    return instream;
}

bign a[1000];
int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    a[0] = 1;
    a[1] = 2;
    for(int i=2; i<1000; i++) {
        a[i] = a[i-1] + a[i-2];
    }
    bign n, m;
    while(cin >> n >> m) {
        if(n==0 && m==0) break;
        cout << upper_bound(a, a+1000, m) - lower_bound(a, a+1000, n) << endl;
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值