您的位置:首页 > 其它

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

2017-10-16 16:38 330 查看

How many Fibs?

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12308Accepted: 4404
DescriptionRecall 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]. InputThe 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.
c9d9
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj