POJ 2413 How many Fibs?(高精度暴力)
2017-10-16 16:38
330 查看
How many Fibs?
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12308 | Accepted: 4404 |
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; }
相关文章推荐
- POJ2413 How many Fibs?【高精度】【二分】
- POJ 2413 How many Fibs?(高精度)
- (高精度运算4.7.31)POJ 2413 How many Fibs?(大数累加)
- POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?
- zoj 1962 || poj 2413 How many Fibs?
- POJ 2413 How many Fibs? 笔记
- poj 2413 How many Fibs?
- POJ 2413 How many Fibs?#二分+大数加法
- hdu 1316 poj 2413 how many Fibs
- poj 2413 How many Fibs?
- POJ 2413 How many Fibs?
- POJ 2413 : How many Fibs? - JAVA大数! / 二分查找+大数加法
- 二分查找+大整数加法——Poj 2413 How many Fibs?
- poj 2413 How many Fibs?
- poj 2413 How many Fibs? 大数累加模板+字符串模拟大数比较大小
- pku 2413 How many Fibs? 【java高精度】
- zoj 1962 || poj 2413 How many Fibs?(大数~)
- UVa 10183/EOJ 1297/POJ 2413/HDU 1316/ZOJ 1962 How many Fibs?【Java大整数类应用】
- How many Fibs?(poj 2413)大数斐波那契
- POJ 2413 How many Fibs? .