uva 763 - Fibinary Numbers(Fibonacci)

The concept of Fibinary numbers is described, which is a representation using Fibonacci sequence digits. The sequence starts with '1010' and follows the rule of adding the two preceding terms. To ensure uniqueness, adjacent '1's are avoided and larger Fibonacci terms are preferred." 120219018,10460140,设计模式详解与实战,"['java', 'idea', '设计模式', 'github', 'git']
摘要由CSDN通过智能技术生成

 Fibinary Numbers 

The standard interpretation of the binary number  1010  is 8 + 2 = 10. An alternate way to view the sequence `` 1010 '' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence are: 

\begin{displaymath}1, 2, 3, 5, 8, 13, 21 , \dots\end{displaymath}

Where each term is the sum of the two preceding terms (note that there is only one 1 in the sequence as defined here). Using this scheme, the sequence ``1010'' could be interpreted as $1 \cdot 5 + 0 \bullet 3 + 1 \bullet 2 + 0 \bullet 1 = 7$. This representation is called a Fibinary number.


Note that there is not always a unique Fibinary representation of every number. For example the number 10 could be represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary representations unique, larger Fibonacci terms must always be used whenever possible (i.e. disallow 2 adjacent 1's). Applying this rule to the number 10, means that 10 would be represented as 8+2 (10010).

Input and Output 

Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form. These numbers will have at most 100 digits.

In case that two or more test cases had to be solved, it must be a blank line between two consecutive, both in input and output files.

Sample Input 

10010
1

10000
1000

10000
10000

Sample Output 

10100

100000

100100
 
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


const int maxn = 155;
string F[maxn] , num1 , num2 , sum , tsum;
vector<int> ans;


string add(string n1 , string n2){
    int len1 = n1.length() , len2 = n2.length() , carry = 0;
    string result;
    for(int i = len1-1 , j = len2-1; i >= 0 || j >= 0; i-- , j--){
        int sum = carry;
        if(i >= 0) sum += n1[i]-'0';
        if(j >= 0) sum += n2[j]-'0';
        carry = sum/10;
        sum = sum%10;
        result.push_back(char('0'+sum));
    }
    if(carry) result.push_back(char('0'+carry));
    reverse(result.begin() , result.end());
    return result;
}


void Fibonacci(){
    F[0] = "1";
    F[1] = "2";
    for(int i = 2; i < maxn; i++){
        F[i] = add(F[i-1] , F[i-2]);
    }
}


string get_sum(string num){
    int len = num.length();
    string tem = "0";
    for(int i = 0; i < len; i++){
        if(num[len-1-i] == '1') tem = add(tem , F[i]);
    }
    return tem;
}


void ini(){
    sum = "0";
    tsum = "0";
    ans.clear();
}


bool is_larger(string n1 , string n2){
    int len1 = n1.length() , len2 = n2.length();
    if(len1 > len2) return true;
    if(len1 < len2) return false;
    for(int i = 0; i < len1; i++){
        if(n1[i]-'0' > n2[i]-'0') return true;
        if(n1[i]-'0' < n2[i]-'0') return false;
    }
    return false;
}


int Binary_search(int l , int r){
    while(l < r){
        int mid = (l+r)/2;
        if(is_larger(add(tsum , F[mid]) , sum)){
            r = mid;
        }else{
            l = mid+1;
        }
    }
    r--;
    tsum = add(tsum , F[r]);
    return r;
}


void computing(){
    sum = add(get_sum(num1) , get_sum(num2));
    int l = 0 , r = 150;
    while(is_larger(sum , tsum)){
        r = Binary_search(l , r);
        ans.push_back(r);
    }
    ans.push_back(-1);
    for(int i = 0; i < ans.size()-1; i++){
        printf("1");
        for(int j = 1; j < ans[i]-ans[i+1]; j++){
            printf("0");
        }
    }
    if(ans.size() == 1) printf("0");
    printf("\n");
}


/*void test(){
    string t = "0";
    for(int i = 0; i < 100; i++){
        t = add(t , F[i]);
    }
    cout << add(t , t) << endl << F[100] << endl << F[110] << endl;
}
*/
int main(){
    Fibonacci();
    //test();
    int t = 0;
    while(cin >> num1 >> num2){
        ini();
        if(t) printf("\n");
        t++;
        computing();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值