UVa763 - Fibinary Numbers

Problem B: How many Fibs?

Recall the definition of the Fibonacci numbers:

f 1 := 1 
f 2 := 2 
f n :=  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 Specification

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 Specification

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
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
string operator + (string s1,string s2){
    if(s1.length()<s2.length())
	{
		string temp=s1;
		s1=s2;
		s2=temp;
	}
	int i,j;
	for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
	{
		s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
		if(s1[i]-'0'>=10)
		{
			s1[i]=char((s1[i]-'0')%10+'0');
			if(i) s1[i-1]++;
			else s1='1'+s1;
		}
	}
	return s1;

}

string operator -(string d2,string d1){
    int len_min = d1.size();
    int len_max = d2.size();
    int last_j = 0;
    string out;
    while(len_min > 0)
    {
        int dd1 = d1[len_min - 1] - '0';
        int dd2 = d2[len_max - 1] - '0';
        if (last_j) dd2 = dd2 - 1;
        last_j = dd2 >= dd1 ? 0 : 1;
        dd2 = dd2 >= dd1 ? dd2 : dd2 + 10;
        out += (dd2 - dd1) + '0';
        len_max -- ;
        len_min -- ;
    }
    while(len_max > 0)
    {
        int dd2 = (d2[len_max -1] - '0');
        if (last_j) dd2 = dd2 - 1;
        last_j = dd2 >= 0 ? 0 : 1;
        dd2 = dd2 >= 0 ? dd2 : dd2 + 10;
        out += dd2 + '0';
        len_max --;
    }
    if (last_j)
          out +='1';
    else
          out +='0';
    int i;
    for(i = out.size()-1; i >= 0; i--) if(out[i]!='0') break;
    string s3;
    for(; i >= 0; i--) s3 += out[i];
    if(s3.size()==0) s3 = "0";
    return s3;
}

int  Comp(string a, string b)
{
    int  i;
    if(a.size() != b.size()) return (a.size() > b.size()) ? 1 : -1;
    for(i = 0; i < a.size(); i++)
        if(a[i]-'0' != b[i]-'0') return  (a[i]-'0' > b[i]-'0') ? 1 : -1;
    return  0;
}
vector<string> fibo;
string s1,s2,t1,t2;
void init(){
    fibo.push_back("1");
    fibo.push_back("2");
    for(int i = 2;  ; i++){
        string t = fibo[i-1]+fibo[i-2];
        if(t.size() > 1000) break;
        fibo.push_back(t);
    }
}
int main(){
    init();
    int T= 1;
    while(cin >> s1 >> s2){
            t1.clear();t2.clear();
            if(!T) cout<<endl;
            if(s1.size()==1)  t1 = s1[0];
            else
            for(int i = 0; i < s1.size(); i++)  if(s1[i]=='1')t1 = t1+fibo[s1.size()-i-1];
            if(s2.size()==1)t2 = s2[0];
           else  for(int i = 0; i < s2.size(); i++)  if(s2[i]=='1')t2 = t2+fibo[s2.size()-i-1];
            string ans = t1+t2;
            if(ans=="1"||ans=="0"){
                cout<<ans<<endl;
                T=0;
                continue;
            }
            vector<int> digit;
            int sta = fibo.size()-1;
            while(Comp(ans,fibo[sta]) ==-1) sta--;
            while(sta>0){
                if(Comp(ans,fibo[sta])>-1){
                    ans = ans - fibo[sta];
                    digit.push_back(1);
                    sta--;
                }
                while(Comp(fibo[sta],ans)==1&&sta>0) {sta--;digit.push_back(0);}
            }
            if(fibo[sta]==ans) digit.push_back(1);
            else digit.push_back(0);
            for(int i = 0; i < digit.size(); i++) cout<<digit[i];
            cout<<endl;
            T=0;

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值