POJ 2413 How many Fibs?(高精度暴力)

How many Fibs?

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12308Accepted: 4404

Description

Recall the definition of the Fibonacci numbers:
f1 := 1 

f2 := 2
fn := fn-1 + fn-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

题意

给你两个整数l,r,范围在 10100 内,问你在l~r内有多少个斐波那契数,定义Fib[1]=1,Fib[2]=2。

思路

虽然范围看起来十分大,但是由于斐波那契数增长十分快,所以到 10100 也就600个左右的数,所以直接暴力找就行了。(第一次手写高精1A..也算是一个成就吧QAQ)

Code

#pragma GCC optimize(3)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
#include<string>
#include<climits>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
inline void readInt(int &x) {
    x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    x*=f;
}
inline void readLong(ll &x) {
    x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    x*=f;
}
/*================Header Template==============*/
struct BigInt {
    int a[105],len;
    inline void read() {
        memset(a,0,sizeof a);
        char s[105];
        scanf("%s",s+1);
        len=strlen(s+1);
        for(int i=1;i<=len;i++)
            a[i]=s[len-i+1]-'0';
    }
    inline void init(ll x) {
        memset(a,0,sizeof a);
        int pos=0;
        while(x) {
            a[++pos]=x%10;
            x/=10;
        }
        len=pos;
    }
    inline void getlimit() {
        len=101;
        for(int i=1;i<=100;i++)
            a[i]=0;
        a[101]=1;
    }
    inline void print() {
        printf("Len = %d\n",len);
        for(int i=len;i>=1;i--)
            printf("%d",a[i]);
        puts("");
    }
};
inline BigInt operator + (const BigInt &a,const BigInt &b) {
    BigInt c;
    c.init(0LL);
    c.len=max(a.len,b.len);
    for(int i=1;i<=c.len;i++) {
        c.a[i]+=a.a[i]+b.a[i];
        if(c.a[i]>=10) {
            c.a[i]-=10;
            c.a[i+1]+=1;
        }
    }
    if(c.a[c.len+1]!=0)
        c.len++;
    return c;
}
inline bool operator < (const BigInt &a,const BigInt &b) {//其实这里是小于等于
    if(a.len<b.len)
        return 1;
    if(a.len>b.len)
        return 0;
    for(int i=a.len;i>=1;i--)
        if(a.a[i]<b.a[i])
            return 1;
        else if(a.a[i]>b.a[i])
            return 0;
    return 1;
}
BigInt f[1010];
BigInt l,r;
int cnt=0;
int main() {
    f[1].init(1LL);
    f[2].init(2LL);
    for(cnt=3;;cnt++) {
        f[cnt]=f[cnt-1]+f[cnt-2];
        if(f[cnt].len>=101)
            break;
    }
    while(1) {
        l.read();
        r.read();
        if(l.len==1&&l.a[1]==0&&r.len==1&&r.a[1]==0)
            break;
        int ans=0;
        for(int i=1;i<=cnt;i++) {
            if(l<f[i]&&f[i]<r)
                ans++;
            if(r<f[i])
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值