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:
Fibinary Numbers |
![\begin{displaymath}1, 2, 3, 5, 8, 13, 21 , \dots\end{displaymath}](http://uva.onlinejudge.org/external/7/763img1.gif)
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 . 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; }